Spring-boot+mybatis Development Combat: How to use the Myabtis persistence layer framework in Spring-boot

Source: Internet
Author: User
Tags aop redis

Objective:

This project is built on Maven and uses mybatis-spring-boot as the persistence layer framework for SPRING-BOOT projects

The use of the MyBatis Persistence layer framework in Spring-boot differs from the original spring project usage and annotations, depending on the Mybatis-spring-boot package 1, the introduction of MyBatis and database and other project dependencies 1.1 , the introduction of MyBatis dependence

<!--mybatis-spring-boot-->
		<dependency>
			<groupid>org.mybatis.spring.boot</groupid >
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>1.1.1</version >
		</dependency>

1.2, the introduction of MySQL driver

<!--mysql-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId> Mysql-connector-java</artifactid>
		</dependency>

1.3, Project Pom.xml list

<project xmlns= "http://maven.apache.org/POM/4.0.0" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi: schemalocation= "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd" > <modelversion >4.0.0</modelVersion> <groupId>cn.eguid.carDeviceInfoSys</groupId> <artifactId> Carsys-web</artifactid> <packaging>war</packaging> <version>1.4.0-snapshot</version > <name>carSys-web</name> <parent> <groupId>org.springframework.boot</groupId> &l T;artifactid>spring-boot-starter-parent</artifactid> <version>1.4.0.RELEASE</version> </ Parent> <dependencies> <!--spring-boot web--> <dependency> <groupid>org.springframew Ork.boot</groupid> <artifactId>spring-boot-starter-web</artifactId> <!--<exclusions> & 
	Lt;exclusion> <groupId>org.springframework.boot</groupId>			<artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions>--> < /dependency> <!--spring AOP--> <dependency> <groupid>org.springframework.boot</groupid&gt
			; <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--mysql--> < Dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> ;/dependency> <dependency> <groupId>org.springframework.boot</groupId> &LT;ARTIFACTID&GT;SPR Ing-boot-starter-jdbc</artifactid> </dependency> <!--https://mvnrepository.com/artifact/ Redis.clients/jedis--> <dependency> <groupId>redis.clients</groupId> <artifactid>jedis </artifactId> </dependency> <dependency> <groupid>org.springframework.boot</groupid > <artifactId>spring-boot-starter-test</artifactid> <scope>test</scope> </dependency> <dependency> <groupid>com.jayway .jsonpath</groupid> <artifactId>json-path</artifactId> <scope>test</scope> &LT;/DEP endency> <!--mybatis-spring-boot--> <dependency> <groupid>org.mybatis.spring.boot</groupi d> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.1.1</version> </ dependency> <!--fastjson--> <dependency> <groupId>com.alibaba</groupId> <artifa 
			ctid>fastjson</artifactid> <version>1.2.17</version> </dependency> <dependency> <groupId>dom4j</groupId> <artifactId>dom4j</artifactId> <version>1.6.1</version 
			> </dependency> </dependencies> <profiles> <profile> <id>production</id>
	<dependencies>			<!--This sample are a test for the AutoConfig Commons-pool is *absent*. In production it would is useful to enable pooling by using this dependency. --> <dependency> <groupId>commons-pool</groupId> <artifactid>commons-pool</art ifactid> <type>pom.lastUpdated</type> </dependency> </dependencies> </profile&
	Gt </profiles> <!--Package as an executable jar--> <build> <plugins> <plugin> ;groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid > </plugin> </plugins> </build> <repositories> <repository> <id>spring- releases</id> <url>https://repo.spring.io/libs-release</url> </repository> </ repositories> <pluginRepositories> <pluginRepository> <id>spring-releases</id> &LT;url>https://repo.spring.io/libs-release</url> </pluginRepository> </pluginRepositories>
 </project>

2. Configure database connection parameters, set MyBatis mappers package and Spring-boot service parameter configuration

Create a application.properties in the project root that defines the Spring-boot parameters and database parameters, and configures the MyBatis mappers scan path

If it is a MAVEN project, Application.properties is placed in the src/main/resource/directory

The configuration is as follows:

Spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test
Spring.datasource.username=root
Spring.datasource.password=eguid
Spring.datasource.driver-class-name=com.mysql.jdbc.driver
Spring.datasource.max-idle=5
spring.datasource.max-wait=10000
Spring.datasource.min-idle=1
Spring.datasource.initial-size=3

server.port=8088
server.session.timeout=10
server.tomcat.max-threads=800
Server.tomcat.uri-encoding=utf-8

Mybatis.mapperlocations=classpath:cn/eguid/carsysweb/mappers/*.xml


3, MyBatis DAO interface and Mapper.xml implementation 3.1, define the DAO interface of MyBatis

This interface is different from the Mybatis-spring method and requires a @mapper annotation

@Mapper annotations are used to declare a DAO interface that is mybatis for this interface

Package Cn.eguid.carSysWeb.dao;

Import java.util.List;

Import Org.apache.ibatis.annotations.Mapper;
Import Org.apache.ibatis.annotations.Param;

Import Cn.eguid.carSysWeb.entity.DepInfo;
Use the Mapper note to declare that the interface is MyBatis DAO interface
@Mapper public
interface Getinfodao {public

	list<depinfo> Getrootinfo ();

	Public list<depinfo> Getdepinfo (@Param (value = "parentcoding") String org_parent_coding);
	
	Public list<depinfo> Getdepinfobyid (@Param (value= "dep_id") String dep_id);


3.2, DAO interface corresponding to the Mapper.xml

Mapper.xml the same as the original MyBatis

<! DOCTYPE Mapper Public "-//mybatis.org//dtd mapper 3.0//en" "Http://mybatis.org/dtd/mybatis-3-mapper.dtd" > < Mapper namespace= "Cn.eguid.carSysWeb.dao.GetInfoDao" > <resultmap type= "cn.eguid.carSysWeb.entity.DepInfo" id = "Depmap" > <id column= "org_coding" property= "dep_id"/> "<result" column= "Org_name" property= "Dep_name"/ > <result column= "org_parent_coding" property= "dep_parent_coding"/> <result column= "Last_time" property= "Last_time"/> </resultMap> <select id= "Getrootinfo" resultmap= "Depmap" > select * from Car_organization Where org_parent_coding is null </select> <select id= "Getdepinfo" parametertype= "string" resultmap= "Depmap" > SELECT * from Car_organization where Org_parent_coding=#{parentcoding,jdbctype=varchar} </select> < Select Id= "Getdepinfobyid" parametertype= "string" resultmap= "Depmap" > select * from Car_organization where org_ CODING=#{DEP_ID} </select> </mapper>


Add: After this step, you can inject MyBatis DAO implementation directly through spring's IOC annotation in service, my DAO interface here is Getinfodao, so inject ' getinfodao ' to correctly reference the persistence layer;

Note: You must open the @componentscan annotation in the Spring-boot entry class to scan all annotations in the project

Package Cn.eguid.carSysWeb;

Import org.springframework.boot.SpringApplication;
Import org.springframework.boot.autoconfigure.SpringBootApplication;
Import Org.springframework.boot.builder.SpringApplicationBuilder;

Import Org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
//Open general annotation scan
@ComponentScan public
class application extends Org.springframework.boot.web.support.SpringBootServletInitializer {
	/**
	 * Implement Springbootservletinitializer to allow Spring-boot projects to run in the Web container/
	@Override
	protected Springapplicationbuilder Configure (Springapplicationbuilder builder) {
		builder.sources (This.getclass ());
		return super.configure (builder);
	}

	public static void Main (string[] args) {
		springapplication.run (application.class, args);
	}




5. Summary:

1, Spring-boot project use Mabatis need to rely on mybatis-spring-boot

2, need to define the database connection parameter in the Application.xml and mybatis mappers file scan path

3, MyBatis DAO interface needs to add @mapper annotation to be spring-boot correctly scanned to

4, Spring-boot open annotation scan annotation is @componentscan


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.