微服务,微架构[六]之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