The two days have started a new project because the project team members have been using mybatis, although individuals prefer the minimalist model of JPA, but for the project to maintain the unity of technology selection or set a mybatis. To the Internet to find a bit about the spring boot and mybatis combination of relevant information, a variety of forms have, look at the people tired, combined with MyBatis's official demo and documents finally found the simplest two models, spent a day to summarize after sharing out.
ORM Framework is the essence of simplifying the operation of the database coding, the development to now basically left two, one is declared can not write a SQL hibernate, one can flexibly debug dynamic SQL MyBatis, both have characteristics, It can be flexibly used in enterprise-level system development according to requirements. Discover an interesting phenomenon: most traditional businesses prefer hibernate, and the internet industry usually uses mybatis.
Hibernate feature is that all SQL is generated using Java code, do not jump out of the program to write (see) SQL, with programming integrity, the development to the top is the spring data JPA this mode, basically according to the method name can generate the corresponding SQL, There's not much to see in my last article Springboot (v): Use of spring data JPA.
MyBatis Initial use is cumbersome, requiring a variety of configuration files, entity classes, DAO layer mapping associations, and a large push other configurations. Of course, MyBatis also found this disadvantage, the initial development of generator can be based on table results automatically produce entity classes, configuration files and DAO layer code, you can reduce the amount of development, the latter also carried out a lot of optimization can use annotations, automatic management of DAO layer and configuration files, etc. Development to the top is today to talk about this mode, Mybatis-spring-boot-starter is Springboot+mybatis can be fully annotated without configuration files, can also be simple configuration easy to get started.
Now think of spring boot is a good thing, anything as long as the link to the spring boot is simplified. Mybatis-spring-boot-starter
Official note: MyBatis Spring-boot-starter would help you to use the MyBatis with Spring Boot
In fact, mybatis see Spring boot so hot also developed a set of solutions to join in the fun, but this really solves a lot of problems, the use of a lot of really smooth. Mybatis-spring-boot-starter There are two main solutions, one is to use annotations to solve all problems, one is a simplified old tradition.
Of course, any mode will need to first introduce the Mybatis-spring-boot-starter Pom file, now the latest version is 1.1.1 (just as fast as the Double 11:))
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId> mybatis-spring-boot-starter</artifactid>
<version>1.1.1</version>
</dependency >
All right, here we are. Two development modes no configuration file annotation version
Is that everything is done with annotations. 1 Adding related maven files
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <ar tifactid>spring-boot-starter</artifactid> </dependency> <dependency> <groupid>o Rg.springframework.boot</groupid> <artifactId>spring-boot-starter-test</artifactId> < scope>test</scope> </dependency> <dependency> <groupid>org.springframework.boot </groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <depe Ndency> <groupId>org.mybatis.spring.boot</groupId> <artifactid>mybatis-spring-boot-st
arter</artifactid> <version>1.1.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </depend Ency> <dependEncy> <groupId>org.springframework.boot</groupId> <artifactid>spring-boot-devtools<
;/artifactid> <optional>true</optional> </dependency> </dependencies>
Complete POM package is not posted here, we directly see the source code 2, application.properties Add the relevant configuration
mybatis.type-aliases-package=com.neo.entity
spring.datasource.driverClassName = Com.mysql.jdbc.Driver
Spring.datasource.url = Jdbc:mysql://localhost:3306/test1?useunicode=true&characterencoding=utf-8
Spring.datasource.username = root
Spring.datasource.password = root
Springboot will automatically load spring.datasource.* related configuration, the data source will be automatically injected into the sqlsessionfactory, Sqlsessionfactory will be automatically injected into the mapper, you do not have to worry about everything, Just pick it up and use it right away.
Add a scan to the mapper package in the Startup class @mapperscan
@SpringBootApplication
@MapperScan ("Com.neo.mapper") public
class Application {public
static void main ( String[] args) {
springapplication.run (application.class, args);
}
}
or directly on the Mapper class to add annotations @mapper, recommended to use the above, or each Mapper add an annotation is also very troublesome 3, development Mapper
The third step is the most critical piece, and SQL production is here
Public interface Usermapper {
@Select (' SELECT * from users ')
@Results ({
@Result (property = "Usersex", Column = "User_sex", Javatype = Usersexenum.class),
@Result (property = "nickname", column = "Nick_name")
})
List<userentity> getAll ();
@Select ("SELECT * from users WHERE id = #{id}")
@Results ({