前言:
本項目基於maven構建,使用mybatis-spring-boot作為spring-boot項目的持久層架構
spring-boot中使用mybatis持久層架構與原spring項目使用方式和註解都不相同,需要依賴mybatis-spring-boot包 1、引入mybatis和資料庫及其他項目依賴 1.1、引入mybatis依賴
<!-- mybatis-spring-boot --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency>
1.2、引入mysql 驅動
<!-- mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency>
1.3、項目pom.xml一覽
<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><artifactId>spring-boot-starter-parent</artifactId><version>1.4.0.RELEASE</version></parent><dependencies><!-- spring-boot web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><!--<exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> --></dependency><!-- spring aop --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-aop</artifactId></dependency><!-- mysql--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-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></dependency><!-- mybatis-spring-boot --><dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>1.1.1</version></dependency><!-- fastjson --><dependency><groupId>com.alibaba</groupId><artifactId>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 is a test for the autoconfig when commons-pool is *absent*. In production it would be useful to enable pooling by using this dependency. --><dependency><groupId>commons-pool</groupId><artifactId>commons-pool</artifactId><type>pom.lastUpdated</type></dependency></dependencies></profile></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><url>https://repo.spring.io/libs-release</url></pluginRepository></pluginRepositories></project>
2、設定資料庫串連參數、設定mybatis的mappers所在包以及spring-boot服務參數配置
在項目根目錄下建立一個application.properties,該檔案用於定義spring-boot的相關參數及資料庫參數,以及配置mybatis的mappers掃描路徑
如果是maven項目,application.properties放在src/main/resource/目錄下
配置如下:
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介面及mapper.xml實現 3.1、定義mybatis的dao介面
該介面與mybatis-spring方式不同,需要加上一個@Mapper註解
@Mapper註解用於聲明該介面為mybatis的dao介面
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;//使用Mapper註解聲明該介面為mybatis的dao介面@Mapperpublic 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介面對應的mapper.xml
mapper.xml與原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>
補充: 做完以上步驟,就可以在service中直接通過spring的IOC註解注入mybatis的dao實現,我這裡的dao介面是GetInfoDao,所以是注入‘getInfoDao’就可以正確引用該持久層;
注意:必須在spring-boot的入口類中開啟@ComponentScan註解才能掃描到項目中所有註解
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//開啟通用註解掃描@ComponentScanpublic class Application extends org.springframework.boot.web.support.SpringBootServletInitializer {/** * 實現SpringBootServletInitializer可以讓spring-boot項目在web容器中運行 */@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {builder.sources(this.getClass());return super.configure(builder);}public static void main(String[] args) {SpringApplication.run(Application.class, args);}}
5、總結:
1、spring-boot項目中使用mabatis需要依賴mybatis-spring-boot
2、需要在application.xml中定義資料庫連接參數以及mybatis的mappers檔案掃描路徑
3、mybatis的dao介面需要加上@Mapper註解才能被spring-boot正確掃描到
4、spring-boot開啟註解掃描的註解是@ComponentScan