MyBatis integration into Spring boot

Source: Internet
Author: User
Tags generator

1, MyBatis Introduction

MyBatis is an excellent persistence layer framework that supports the customization of SQL, stored procedures, and advanced mapping. MyBatis avoids almost all JDBC code and manually sets parameters and gets the result set. MyBatis can use simple XML or annotations for configuration and native maps to map interfaces and Java POJOs (Plain old Java Objects, ordinary Java objects) to records in a database.

2, MyBatis Installation

Place the following dependency code in the Pom.xml file:

<dependency>

<groupId>org.mybatis.spring.boot</groupId>

<artifactId>mybatis-spring-boot-starter</artifactId>

<version>1.3.0</version>

</dependency>

3, MyBatis Configuration

/**

* Data sources, transactions, Sqlsessionfactory,sqlsessiontemplate needed to build MyBatis

*

* @author

*/

@Configuration

@EnableTransactionManagement

public class Mybatisconfig implements Transactionmanagementconfigurer

{

@Override

Public Platformtransactionmanager Annotationdriventransactionmanager ()

{

return new Datasourcetransactionmanager (DataSource ());

}

/**

* Set the data source for the database

*

* @return DataSource

*/

@Bean (name = "DataSource")

Public DataSource DataSource ()

{

Druiddatasource Druiddatasource = new Druiddatasource ();

Druiddatasource.setdriverclassname (Datasourceproperties.getdriverclassname ());

Druiddatasource.seturl (Datasourceproperties.geturl ());

Druiddatasource.setusername (Datasourceproperties.getusername ());

Druiddatasource.setpassword (Datasourceproperties.getpassword ());

return druiddatasource;

}

/**

* for building Sqlsessionfactory

*

* @return Sqlsessionfactory

* @throws Exception

*/

@Bean (name = "Sqlsessionfactory")

Public Sqlsessionfactory Sqlsessionfactorybean () throws Exception

{

Pathmatchingresourcepatternresolver resolver = new Pathmatchingresourcepatternresolver ();

Sqlsessionfactorybean bean = new Sqlsessionfactorybean ();

Bean.setdatasource (DataSource ());

Bean.setmapperlocations (Resolver.getresources ("Classpath*:com/eversec/**/*.xml"));

return Bean.getobject ();

}

/**

* Build sqlsessiontemplate with Sqlsessionfactory

*

* @param sqlsessionfactory

* @return Sqlsessiontemplate

*/

@Bean (name = "Sqlsessiontemplate")

Public sqlsessiontemplate sqlsessiontemplate (sqlsessionfactory sqlsessionfactory)

{

return new Sqlsessiontemplate (sqlsessionfactory);

}

@Resource (name = "Datasourceproperties")

Private Datasourceproperties datasourceproperties;

}

/**

* Description: Configuration mapper

*

* @author

*

*/

@Configuration

@AutoConfigureAfter (Mybatisconfig.class)

public class Mybatismapperscannerconfig

{

@Bean

Public Mapperscannerconfigurer Mapperscannerconfigurer ()

{

Mapperscannerconfigurer mapperscannerconfigurer = new Mapperscannerconfigurer ();

Mapperscannerconfigurer.setsqlsessionfactorybeanname ("Sqlsessionfactory");

Mapperscannerconfigurer.setbasepackage ("Com.eversec.**.sqlmapper");

return mapperscannerconfigurer;

}

}

4, Mybatis Reverse code generation

4.1 Place the following plugin code in the Pom.xml file:

<plugin>

<groupId>org.mybatis.generator</groupId>

<artifactId>mybatis-generator-maven-plugin</artifactId>

<version>1.3.2</version>

<configuration>

<verbose>true</verbose>

<overwrite>true</overwrite>

</configuration>

</plugin>

4.2 Place Generatorconfig.xml in the Src/main/resource folder

The contents of Generatorconfig.xml are as follows:

<?xml version= "1.0" encoding= "UTF-8"?>

<! DOCTYPE generatorconfiguration Public "-//mybatis.org//dtd mybatis Generator Configuration 1.0//en" "/http Mybatis.org/dtd/mybatis-generator-config_1_0.dtd ">

<generatorConfiguration>

<classpathentry location= "D:\mysql-connector-java-5.1.39.jar"/>

<context id= "Context1" targetruntime= "MyBatis3" >

<jdbcconnection driverclass= "Com.mysql.jdbc.Driver"

Connectionurl= "Jdbc:mysql://ip:3306/database?useunicode=true&amp;characterencoding=utf-8"

Userid= "root" password= "123456"/>

<javamodelgenerator targetpackage= "Com.entity"

targetproject= "D:\workspace\xxxx\src\main\java"/>

<sqlmapgenerator targetpackage= "Com.sqlmapper"

targetproject= "D:\workspace\xxxx\src\main\java"/>

<javaclientgenerator targetpackage= "Com.sqlmapper"

targetproject= "D:\workspace\xxxx\src\main\java" type= "Xmlmapper"/>

<table tablename= "Table" enablecountbyexample= "false" enableupdatebyexample= "false" enabledeletebyexample= " False "enableselectbyexample=" false "selectbyexamplequeryid=" false "/>

</context>

</generatorConfiguration>

4.3 execution

Right-Pom.xml, select Run as >> Maven build ..., then enter mybatis-generator:generate in goals, preferably click the Run button.

5, MyBatis's frame diagram

See MyBatis frame diagram, you can clearly see the overall core object of MyBatis, I prefer to use their own diagram to express the mybatis of the entire implementation process. As shown in the following:

Explanation of principle:

The MyBatis application is created from an XML configuration file based on the configuration, the configuration is from two places, one is the configuration file, one is the Java code annotation, gets a sqlsession. Sqlsession contains all the methods required to execute SQL, you can run the mapped SQL statements directly through the Sqlsession instance, complete the additions and deletions of the data and transaction commits, etc., and then close the sqlsession.

Website address: http://www.mybatis.org/mybatis-3/zh/index.html

MyBatis integration into Spring boot

Related Article

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.