用的好好的Hibernate,关键时刻,连不上SqlServer,总是报缺少对象或列名,还没有解决,只能暂时换成MyBatis了。
1.pom.xml添加Mybatis支持
1 2 3 4 5 6 7 8
| <dependencies> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> </dependencies>
|
2.application.yml添加支持
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| spring: datasource: hikari: connection-timeout: 20000 maximum-pool-size: 5 username: \****** password: \***** url: jdbc:sqlserver://xxxxx;databasename=xxx driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver mybatis: configuration: map-underscore-to-camel-case: true type-aliases-package: com.proheng.gis.Entity mapper-locations: classpath:mapper/*.xml
|
3.resources下新建mybatis-config.xml文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <settings> <setting name="mapUnderscoreToCamelCase" value="true"/> </settings> <typeAliases> <typeAlias alias="Integer" type="java.lang.Integer" /> <typeAlias alias="Long" type="java.lang.Long" /> <typeAlias alias="HashMap" type="java.util.HashMap" /> <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap" /> <typeAlias alias="ArrayList" type="java.util.ArrayList" /> <typeAlias alias="LinkedList" type="java.util.LinkedList" /> </typeAliases> </configuration>
|
4.新建实体类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| package com.proheng.gis.Entity;
import javax.persistence.*;
@Entity @Table(name = "V_client",catalog = "ztzn",schema = "dbo") public class VClient {
@Id @GeneratedValue private int SiteNo=0;
public int getSiteNo() { return SiteNo; } }
|
4.resources文件夹先新建mapper文件夹
在resources文件夹下新建mapper文件夹,然后新建Vclient映射文件Vclient.xml
1 2 3 4 5 6 7 8 9 10 11
| <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.proheng.gis.mapper.VClientMapper"> <select id="searchAll" resultType="com.proheng.gis.Entity.VClient"> select * from v_client; </select>
</mapper>
|
5.新建映射
1 2 3 4 5 6 7 8 9 10
| package com.proheng.gis.mapper;
import com.proheng.gis.Entity.VClient;
import java.util.List;
public interface VClientMapper { List<VClient> searchAll(); }
|
6.在主类上添加MapperScan
@MapperScan 要添加到main类上,添加需要扫描的类,否则,就会找不到映射的bean。
1 2
| @SpringBootApplication @MapperScan("com.proheng.gis.mapper")
|
6.使用VClientMapper
剩下的可以多添加一层Service层,也可以不添加,根据需要使用VClientMapper就好了。可以直接在collector上注入。
1 2
| @Autowired private VClientMapper vClientMapper;
|
在idea中还是会报没有找到注入的bean的问题,这个不要紧,不会影响执行的。
7.配置多数据库
8.Mybatis注解