mavne file:
<?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>
Next, define the configuration file of mybatis in the src/main/resource Directory:
mybatis/config.xml:
<?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:
<?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:
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
Then define the mapper class:
public interface DevDataMapper {
List<DevDataEntity> getDevDataList(String id);
}
Use the mapper class in service:
@Service public class DevDataService { @Autowired DevDataMapper devDataMapper; public List<DevDataEntity> getDevDataList() { return devDataMapper.getDevDataList("a"); } }
Finally, add @ MapperScan annotation to the startup class:
@MapperScan("rfidData.mapper")
Run successfully!!
Analysis:
Through the configuration of application.properties, springboot autoconfiguration initializes sqlsessionfactorbean when it is started.
Configuration location and maperlocation are loaded.
Through @ mapperscan ("RFID data. Mapper") annotation, all interface definitions under the package will be automatically scanned, and then a dynamic proxy class will be generated and injected into the service.
In this way, when the service is called, the proxy class of the dao interface starts to execute. Internally, an execution statement is found by using the interface + method as the key, and then an executor is found to execute. The internal of the executor is executed by jdbc.