微服務,微架構[六]之springboot集成mybatis
介紹
mybatis在現在市場占有率相對比較高,使用較多的一個框架,所以本文主要介紹如何將mybatis集成到springboot中進行使用,在之前使用springmvc中集成mybatis非常麻煩,而且有很多xml配置文件,配置起來非常臃腫,而且維護起來複雜,往往整理這些配置文件就需要花很長時間才能基礎搭建起來,springboot集成mybatis完全拋棄之前的xml文件,經過幾個簡單的配置就可以完成對mybaties的集成。
一、配置數據源與mybatis掃描路徑 application.properties
#1.jdbc config spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.url=jdbc:mysql://localhost:3306/boot?characterEncoding=utf-8&generateSimpleParameterMetadata=true spring.datasource.username=root spring.datasource.password=123456 spring.datasource.driver-class-name=com.mysql.jdbc.Driver #2.extend config spring.datasource.initialSize=5 spring.datasource.minIdle=5 spring.datasource.maxActive=20 spring.datasource.maxWait=60000 spring.datasource.timeBetweenEvictionRunsMillis=60000 spring.datasource.minEvictableIdleTimeMillis=300000 spring.datasource.validationQuery=SELECT 1 FROM DUAL spring.datasource.testWhileIdle=true spring.datasource.testOnBorrow=false spring.datasource.testOnReturn=false spring.datasource.poolPreparedStatements=true spring.datasource.maxPoolPreparedStatementPerConnectionSize=20 spring.datasource.filters=stat,wall,log4j spring.datasource.connectionProperties=druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000 #mybaties config mybatis.typeAliasesPackage=com.eshengtai.dao.model mybatis.mapperLocations=classpath:**/mapper/*.xml
1、mybatis.typeAliasesPackage 掃描java基礎實體類
2、mybatis.mapperLocations 掃描dao層mybatis接口實現*mapper.xml對應SQL語句文件
二、定義實體類與接口dao類,mapper.xml文件
package com.eshengtai.dao.model; public class EShengTai { private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
@Mapper public interface EShengTaiMapper { List<EShengTai> selectAll(); void insert(EShengTai est); }
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "https://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.eshengtai.dao.mapper.EShengTaiMapper" > <resultMap type="com.eshengtai.dao.model.EShengTai" > <id column="id" property="id" jdbcType="BIGINT" /> <result column="Name" property="name" jdbcType="VARCHAR" /> </resultMap> <sql > id, name </sql> <select resultMap="BaseResultMap"> select <include ref /> from eshengtai </select> <insert parameterType="com.eshengtai.dao.model.EShengTai" > insert into eshengtai (id,name) values (#{id,jdbcType=BIGINT}, #{name,jdbcType=VARCHAR}) </insert> </mapper>
三、啟動springboot掃描mybatis接口文件
package com.eshengtai; import org.mybatis.spring.annotation.MapperScan; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @MapperScan("com.eshengtai.dao.mapper") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
@MapperScan("com.eshengtai.dao.mapper") 在springboot啟動時候會將接口注入到spring掃描的容器中,在dao層調用時候會自行依賴關聯實現
以上所有的代碼就是springboot集成mybatis最精簡的配置,其他的調用方式和以來注入方式都和以前一樣,以圖片的方式展示其他代碼,springboot事務詳情文章我會在後麵的文章中獨立詳解
最後更新:2017-05-18 10:32:24