Mybatis-spring-boot-starter There are two main solutions, one is to use annotations to solve all problems, one is a simplified old tradition. Either way, the first step is essential.
No configuration file Annotations version
First step: Add dependencies
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId> mybatis-spring-boot-starter</artifactid> <version>1.3.2</version> </dependency > <dependency> <groupId>mysql</groupId> <artifactId> Mysql-connector-java</artifactid> </dependency>
Step two: Add the MySQL attribute to the Application.properties
spring.datasource.driverClassName == =
Note: The springboot automatically loads the spring.datasource.*-related configuration, and the data source is automatically injected into the sqlsessionfactory, and sqlsessionfactory is automatically injected into the mapper.
Add a scan to the mapper package in the Startup class @MapperScan
, or add annotations directly above the Mapper class @Mapper
.
Step Three: Develop mapper
Public Interface Turbinedao { @Select ("select * from m_turbine WHERE turbine_code = #{turbinecode}") @Results ({ = "Turbinecode", column = "Turbine_code"), = "Devicecode", column = "Device_code") }) Public List<turbine> FindOne (String turbinecode); }
Note 1:
- @Select is the annotation of the query class, all queries use this
- The @Result adornment returns the result set, the associated entity class attribute and the database field one by one corresponding, if the entity class attribute and the database property name are consistent, this property is not required to be decorated.
- @Insert inserted into the database, the direct incoming entity class automatically resolves the attribute to the corresponding value
- @Update is responsible for modifying or passing directly to the object
- @delete is responsible for deleting
Note 2: Use the difference between the # symbol and the $ symbol
Minimalist XML version
1. Configuration
The pom file is the same as the previous version, just application.properties
add the following configuration
mybatis.config-location=classpath:mybatis/mybatis-config.xmlmybatis.mapper-locations=classpath: Mybatis/mapper/*. XML
Specifies the address of the MyBatis base profile and entity class mapping file
Mybatis-config.xml Configuration
<configuration> <typeAliases> <typealias alias= "Integer" type= "Java.lang.Integer"/> <typealias alias= "Long" type= "Java.lang.Long"/>
<typealias alias= "String" type= "java.lang.String"/> <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>
2. Add User's mapping file
<mapper namespace= "Com.example.myproject.dao.TurbineDao" > <resultmap id= "Baseresultmap" type= " Com.example.myproject.model.Turbine "> <result column=" Turbine_code "property=" Turbinecode "jdbctype=" varchar "/> <result column=" Device_code "property=" Devicecode "jdbctype=" VARCHAR "/> </ Resultmap>
<sql id="Base_Column_List" > turbineCode, deviceCode
</sql>
<select id= "FindOne" resultmap= "Baseresultmap" parametertype= "java.lang.String" > Select <include refid= "Base_column_list"/> from m_turbine WHERE turbine_code = #{turbinecode}
</select> </mapper>
3. Writing DAO Layer Code
Public Interface Turbinedao { public list<turbine> findOne (String turbinecode); }
A pure Smile
Source: www.ityouknow.com
All rights reserved, welcome to keep the original link to reprint:)
Spring Boot MyBatis