Spring Boot data Access Integration MyBatis

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.