For software systems, persisting data to a database is a critical part. In the Java world, there are many tools and frameworks (ORM) that implement the data persistence layer. In Java applications, the nature of ORM framework is to simplify the operation of the database in the programming of the tedious, such as can be generated by the SQL hibernate, Hibernate also implemented the JPA specification, using JPA only a few lines of code to achieve the data access and operation; MyBatis, formerly known as IBATIS, is an open-source framework that simplifies and implements the Java data persistence layer, with the flexibility to debug SQL, the main reason for MyBatis popularity is its simplicity and ease of use.
Integrated Mybatis-spring-boot-starter
Before configuring the Spring MVC integration MyBatis requires configuration files, entity classes, DAO Layer Mapping Association, and other cumbersome configuration, followed by the development of generator, you can automatically produce the entity class, configuration file and data layer code according to the table, a certain degree of simple coding work, Of course, you can use annotations to configure, simplified to now Spring boot integration Spring-boot-starter can be annotated directly to write the SQL statement, the principle is the Convention to greater than the configuration, Spring Boot to do is to simplify everything.
MAVEN Configuration
<!--MySQL - <Dependency> <groupId>Mysql</groupId> <Artifactid>Mysql-connector-java</Artifactid> </Dependency> <Dependency> <groupId>Com.alibaba</groupId> <Artifactid>Druid-spring-boot-starter</Artifactid> <version>1.1.2</version> </Dependency> <!-jdbc - <Dependency> <groupId>Org.springframework.boot</groupId> <Artifactid>Spring-boot-starter-jdbc</Artifactid> </Dependency> <!--MyBatis - <Dependency> <groupId>Org.mybatis.spring.boot</groupId> <Artifactid>Mybatis-spring-boot-starter</Artifactid> <version>1.3.1</version> </Dependency> <Dependency> <groupId>Com.github.pagehelper</groupId> <Artifactid>Pagehelper-spring-boot-starter</Artifactid> <version>1.2.1</version> </Dependency>
Application.properties Configuration
#MySQL config spring. DataSource. type=com.alibaba.druid.pool. druiddatasourcespring. Datasource.driver-class-name=com.mysql.jdbc. driverspring. Datasource.url=jdbc: MySQL://172.0.0.1:3306/test?useunicode=true& Characterencoding=utf-8&autoreconnect=true&usessl=falsespring. datasource.username = Rootspring. Datasource.password = 123456spring. datasource.max-active=30Spring. Datasource.max-idle=8Springdatasource.min-idle=8spring. datasource.initial-size=10
The above configuration Spring Boot will automatically load the spring.datasource.*-related configuration, initializing the database connection pool (where Ali's Druidis used).
Annotation-based approach
# # Not completed
Configuring multiple data Sources Https://github.com/alibaba/druid/tree/master/druid-spring-boot-starter
Spring Boot data Access Integration MyBatis