tk.mybatis
连接池
pom
在 pom.xml 文件中引入 druid-spring-boot-starter 依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.10</version>
</dependency>
引入数据库连接依赖
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
配置 application.yml
spring:
datasource:
druid:
url: jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: 123456
initial-size: 1
min-idle: 1
max-active: 20
test-on-borrow: true
driver-class-name: com.mysql.jdbc.Driver
使用 MyBatis 的 Maven 插件生成代码
pom
<build>
<plugins>
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
<overwrite>true</overwrite>
<verbose>true</verbose>
</configuration>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper</artifactId>
<version>3.4.4</version>
</dependency>
</dependencies>
</plugin>
</plugins>
</build>
在 src/main/resources/generator/ 目录下创建 generatorConfig.xml 配置文件:
<?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>
<!-- 引入数据库连接配置 -->
<properties resource="jdbc.properties"/>
<context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<!-- 配置 tk.mybatis 插件 -->
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.funtl.utils.MyMapper"/>
</plugin>
<!-- 配置数据库连接 -->
<jdbcConnection
driverClass="${jdbc.driverClass}"
connectionURL="${jdbc.connectionURL}"
userId="${jdbc.username}"
password="${jdbc.password}">
</jdbcConnection>
<!-- 配置实体类存放路径 -->
<javaModelGenerator targetPackage="com.funtl.hello.spring.boot.entity" targetProject="src/main/java"/>
<!-- 配置 XML 存放路径 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置 DAO 存放路径 -->
<javaClientGenerator
targetPackage="com.funtl.hello.spring.boot.mapper"
targetProject="src/main/java"
type="XMLMAPPER"/>
<!-- 配置需要生成的表,% 代表所有 -->
<table tableName="%">
<!-- mysql 配置 -->
<generatedKey column="id" sqlStatement="Mysql" identity="true"/>
</table>
</context>
</generatorConfiguration>
在 src/main/resources 目录下创建 jdbc.properties 数据源配置:
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.connectionURL=jdbc:mysql://ip:port/dbname?useUnicode=true&characterEncoding=utf-8&useSSL=false
jdbc.username=root
jdbc.password=123456
Spring Boot 整合 tk.mybatis
pom
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
配置 application.yml
mybatis:
type-aliases-package: 实体类的存放路径,如:com.funtl.hello.spring.boot.entity
mapper-locations: classpath:mapper/*.xml
重构
重构mapper
创建一个通用的父级接口
主要作用是让 DAO 层的接口继承该接口,以达到使用 tk.mybatis 的目的,这里mapper抽取出来放到通用的项目中
package tk.mybatis.mapper;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 自己的 Mapper
* 特别注意,该接口不能被扫描到,否则会出错
* <p>Title: MyMapper</p>
* <p>Description: </p>
*
* @author Lusifer
* @version 1.0.0
* @date 2018/5/29 0:57
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
}
Mapper继承之前的通用父级mapper
import org.faramita.itoken.common.domain.TbSysUser;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.MyMapper;
@Repository
public interface TbSysUserExtendMapper extends MyMapper<TbSysUser> {
}
重构BaseService
常用的crud等service抽取出来,放到commons工程中
这里的BaseDomain是领域模型的基类,就是entity实体类把通用的字段抽取出来,然后entity就可以继承该类,省略重复的类的书写
package org.faramita.itoken.common.service;
import com.github.pagehelper.PageInfo;
import org.faramita.itoken.common.domain.BaseDomain;
public interface BaseService<T extends BaseDomain> {
int insert(T t, String createBy);
int delete(T t);
int update(T t, String updateBy);
int count(T t);
T selectOne(T t);
PageInfo<T> page(int pageNum, int pageSize, T t);
}
实际service继承之前的通用父级service,有特殊的就这里添加就行了。
package org.faramita.itoken.service.admin.service;
import org.faramita.itoken.common.domain.BaseDomain;
import org.faramita.itoken.common.service.BaseService;
public interface AdminService extends BaseService<TbSysUser> {
}
重构BaseServiceImpl
package org.faramita.itoken.common.service.impl;
import org.faramita.itoken.common.domain.BaseDomain;
import org.faramita.itoken.common.service.BaseService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.MyMapper;
import java.util.Date;
@Service
@Transactional(readOnly = true)
public abstract class BaseServiceImpl<T extends BaseDomain, D extends MyMapper<T>> implements BaseService<T> {
@Autowired
private D dao;
@Override
@Transactional(readOnly = false)
public int insert(T t, String createBy) {
t.setCreateBy(createBy);
t.setCreateDate(new Date());
t.setUpdateBy(createBy);
t.setUpdateDate(new Date());
return dao.insert(t);
}
@Override
@Transactional(readOnly = false)
public int delete(T t) {
return dao.delete(t);
}
@Override
@Transactional(readOnly = false)
public int update(T t, String updateBy) {
t.setUpdateBy(updateBy);
t.setUpdateDate(new Date());
return dao.updateByPrimaryKey(t);
}
@Override
public int count(T t) {
return dao.selectCount(t);
}
@Override
public T selectOne(T t) {
return dao.selectOne(t);
}
@Override
public PageInfo<T> page(int pageNum, int pageSize, T t) {
PageHelper pageHelper = new PageHelper();
pageHelper.startPage(pageNum, pageSize);
PageInfo<T> pageInfo = new PageInfo<>(dao.select(t));
return pageInfo;
}
}
实际serviceImpl继承之前的通用父级service,有特殊的就这里添加就行了。
implements AdminService ,这里泛型用TbSysUser说明service中操作的是TbSysUser类
extends BaseServiceImpl<TbSysUser,TbSysUserMapper>,这里泛型用TbSysUser和TbSysUserMapper,说明serviceImpl中操作的是TbSysUser类和TbSysUserMapper
package org.faramita.itoken.service.admin.service.impl;
import org.faramita.itoken.common.domain.TbSysUser;
import org.faramita.itoken.common.mapper.TbSysUserMapper;
import org.faramita.itoken.common.service.impl.BaseServiceImpl;
import org.faramita.itoken.service.admin.service.AdminService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
@Transactional(readOnly = true)
public class AdminServiceImpl extends BaseServiceImpl<TbSysUser,TbSysUserMapper> implements AdminService
}