spring boot中使用mybatis有很多坑,特別是不一樣版本之間的兼容性等。這裏使用spring boot 1.5.9.版本和mybatis-spring-boot-starter 1.3.0版本。java
mavne文件:mysql
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <modelVersion>4.0.0 </modelVersion> <groupId>rfid</groupId> <artifactId>rfidData</artifactId> <version>0.0.1-SNAPSHOT</version> <name>rfidData Maven Webapp</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <java.version>1.8</java.version> <tomcat-version>8.0.35</tomcat-version> <commons.pool.version>2.4.2</commons.pool.version> <maven.antrun.plugin.version>1.7</maven.antrun.plugin.version> <maven.assembly.plugin.version>2.3</maven.assembly.plugin.version> <ST4.version>4.0.4</ST4.version> <maven.compiler.plugin.version>3.6.1</maven.compiler.plugin.version> <powermock.version>1.7.1</powermock.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-freemarker</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.30</version> </dependency> <!-- database --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency> </dependencies> <build> <finalName>rfidData</finalName> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <mainClass>rfidData.RfidDataApplication</mainClass> </configuration> </plugin> </plugins> </build> </project>
下面接着定義mybatis的配置文件,在src/main/resource目錄下:web
mybatis/config.xml:spring
<?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> <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>
mybatis/mapper/devdata.xml:sql
<?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="rfidData.mapper.DevDataMapper" > <resultMap id="DevDataMap" type="rfidData.entity.DevDataEntity" > <result column="devId" property="devId" jdbcType="VARCHAR" /> <result column="assetId" property="assetId" jdbcType="VARCHAR" /> <result column="name" property="name" jdbcType="VARCHAR" /> <result column="kind" property="kind" jdbcType="VARCHAR" /> </resultMap> <select id="getDevDataList" parameterType="java.lang.String" resultMap="DevDataMap" > SELECT devId,assetId,name,kind FROM t_rfid_dev limit 100 </select> </mapper>
application.properties:apache
mybatis.config-locations=classpath:mybatis/config.xml mybatis.mapper-locations=classpath:mybatis/mapper/*.xml mybatis.type-aliases-package=rfidData.entity # DATABASE spring.datasource.url=jdbc:mysql://127.0.0.1:3306/zkMonitor?characterEncoding=utf8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driverClassName=com.mysql.jdbc.Driver
而後定義mapper類:json
public interface DevDataMapper {
List<DevDataEntity> getDevDataList(String id);
}
在service中使用mapper類:tomcat
@Service public class DevDataService { @Autowired DevDataMapper devDataMapper; public List<DevDataEntity> getDevDataList() { return devDataMapper.getDevDataList("a"); } }
最後,在啓動類中添加@MapperScan註解:mybatis
@MapperScan("rfidData.mapper")app
成功運行!!