springboot整合mybatis
一套开源框架:https://github.com/abel533/MyBatis-Spring-Boot
使用mybatis+分页插件pagehelper整合;
pom
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
<!--mapper层通用方法-->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>1.2.4</version>
</dependency>
<!--pagehelper-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-dbcp2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
application配置:
#热部署--为mybatis设置,生产环境可删除
restart.include.mapper=/mapper-[\\w-\\.]+jar
restart.include.pagehelper=/pagehelper-[\\w-\\.]+jar
#mybatis
mybatis.type-aliases-package=tk.mybatis.springboot.model ##pojo/entity(与数据库对应)层位置
mybatis.mapper-locations=classpath:mapper/*.xml ##xml位置
#mapper
#mappers 多个接口时逗号隔开
mapper.mappers=tk.mybatis.springboot.util.MyMapper ##mapper通用接口
mapper.not-empty=false
mapper.identity=MYSQL
#pagehelper
pagehelper.helperDialect=mysql
pagehelper.reasonable=true
pagehelper.supportMethodsArguments=true
pagehelper.params=count=countSql
mapper通用工具utils
package com.imooc.utils;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.common.MySqlMapper;
/**
* 继承自己的MyMapper
*使用了依赖的mapper-spring-boot-starter,此类具有基本法的增删改查以及example方法,其他mapper只需要继承即可使用
* @author liuzh
* @since 2015-09-06 21:53
*/
public interface MyMapper<T> extends Mapper<T>, MySqlMapper<T> {
//TODO
//FIXME 特别注意,该接口不能被扫描到,否则会出错
}
逆向工程
- 逆向工程所需xml(没有设置生成SQL语句)
<?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>
<context id="MysqlContext" targetRuntime="MyBatis3Simple" defaultModelType="flat">
<property name="beginningDelimiter" value="`"/>
<property name="endingDelimiter" value="`"/>
<plugin type="tk.mybatis.mapper.generator.MapperPlugin">
<property name="mappers" value="com.imooc.utils.MyMapper"/>
</plugin>
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/leecx"
userId="root"
password="root">
</jdbcConnection>
<!-- 对于生成的pojo所在包 -->
<javaModelGenerator targetPackage="com.imooc.pojo" targetProject="src/main/java"/>
<!-- 对于生成的mapper所在目录 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources"/>
<!-- 配置mapper对应的java映射 -->
<javaClientGenerator targetPackage="com.imooc.mapper" targetProject="src/main/java"
type="XMLMAPPER"/>
<table tableName="sys_user"></table>
</context>
</generatorConfiguration>
- 主类
public class GeneratorDisplay {
public void generator() throws Exception{
List<String> warnings = new ArrayList<String>();
boolean overwrite = true;
//指定 逆向工程配置文件
File configFile = new File("generatorConfig.xml");
ConfigurationParser cp = new ConfigurationParser(warnings);
Configuration config = cp.parseConfiguration(configFile);
DefaultShellCallback callback = new DefaultShellCallback(overwrite);
MyBatisGenerator myBatisGenerator = new MyBatisGenerator(config,
callback, warnings);
myBatisGenerator.generate(null);
}
public static void main(String[] args) throws Exception {
try {
GeneratorDisplay generatorSqlmap = new GeneratorDisplay();
generatorSqlmap.generator();
} catch (Exception e) {
e.printStackTrace();
}
}
}
- 生成entity
package com.imooc.pojo;
import java.util.Date;
import javax.persistence.*;
@Table(name = "sys_user")
public class SysUser {
@Id
private String id;
/**
* 用户名,登录名
*/
private String username;
/**
* 密码
*/
private String password;
/**
* 昵称
*/
private String nickname;
/**
* 年龄
*/
private Integer age;
/**
* 性别
0:女
1:男
2:保密
*/
private Integer sex;
/**
* 职业类型:
1:Java开发
2:前端开发
3:大数据开发
4:ios开发
5:Android开发
6:Linux系统工程师
7:PHP开发
8:.net开发
9:C/C++
10:学生
11:其它
*/
private Integer job;
/**
* 头像地址
*/
@Column(name = "face_image")
private String faceImage;
/**
* 省
*/
private String province;
/**
* 市
*/
private String city;
/**
* 区
*/
private String district;
/**
* 详细地址
*/
private String address;
/**
* 用于权限的“盐”
*/
@Column(name = "auth_salt")
private String authSalt;
/**
* 最后一次登录IP
*/
@Column(name = "last_login_ip")
private String lastLoginIp;
/**
* 最后一次登录时间
*/
@Column(name = "last_login_time")
private Date lastLoginTime;
@Column(name = "is_delete")
private Integer isDelete;
@Column(name = "regist_time")
private Date registTime;
/**
* @return id
*/
public String getId() {
return id;
}
/**
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取用户名,登录名
*
* @return username - 用户名,登录名
*/
public String getUsername() {
return username;
}
/**
* 设置用户名,登录名
*
* @param username 用户名,登录名
*/
public void setUsername(String username) {
this.username = username;
}
/**
* 获取密码
*
* @return password - 密码
*/
public String getPassword() {
return password;
}
/**
* 设置密码
*
* @param password 密码
*/
public void setPassword(String password) {
this.password = password;
}
/**
* 获取昵称
*
* @return nickname - 昵称
*/
public String getNickname() {
return nickname;
}
/**
* 设置昵称
*
* @param nickname 昵称
*/
public void setNickname(String nickname) {
this.nickname = nickname;
}
/**
* 获取年龄
*
* @return age - 年龄
*/
public Integer getAge() {
return age;
}
/**
* 设置年龄
*
* @param age 年龄
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* 获取性别
0:女
1:男
2:保密
*
* @return sex - 性别
0:女
1:男
2:保密
*/
public Integer getSex() {
return sex;
}
/**
* 设置性别
0:女
1:男
2:保密
*
* @param sex 性别
0:女
1:男
2:保密
*/
public void setSex(Integer sex) {
this.sex = sex;
}
/**
* 获取职业类型:
1:Java开发
2:前端开发
3:大数据开发
4:ios开发
5:Android开发
6:Linux系统工程师
7:PHP开发
8:.net开发
9:C/C++
10:学生
11:其它
*
* @return job - 职业类型:
1:Java开发
2:前端开发
3:大数据开发
4:ios开发
5:Android开发
6:Linux系统工程师
7:PHP开发
8:.net开发
9:C/C++
10:学生
11:其它
*/
public Integer getJob() {
return job;
}
/**
* 设置职业类型:
1:Java开发
2:前端开发
3:大数据开发
4:ios开发
5:Android开发
6:Linux系统工程师
7:PHP开发
8:.net开发
9:C/C++
10:学生
11:其它
*
* @param job 职业类型:
1:Java开发
2:前端开发
3:大数据开发
4:ios开发
5:Android开发
6:Linux系统工程师
7:PHP开发
8:.net开发
9:C/C++
10:学生
11:其它
*/
public void setJob(Integer job) {
this.job = job;
}
/**
* 获取头像地址
*
* @return face_image - 头像地址
*/
public String getFaceImage() {
return faceImage;
}
/**
* 设置头像地址
*
* @param faceImage 头像地址
*/
public void setFaceImage(String faceImage) {
this.faceImage = faceImage;
}
}
- 生成mapper
import com.imooc.pojo.SysUser;
import com.imooc.utils.MyMapper;
//可以添加注解@Mapper
public interface SysUserMapper extends MyMapper<SysUser> {
}
- 生成xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
<mapper namespace="com.imooc.mapper.SysUserMapper" >
<resultMap id="BaseResultMap" type="com.imooc.pojo.SysUser" >
<!--
WARNING - @mbg.generated
-->
<id column="id" property="id" jdbcType="VARCHAR" />
<result column="username" property="username" jdbcType="VARCHAR" />
<result column="password" property="password" jdbcType="VARCHAR" />
<result column="nickname" property="nickname" jdbcType="VARCHAR" />
<result column="age" property="age" jdbcType="INTEGER" />
<result column="sex" property="sex" jdbcType="INTEGER" />
<result column="job" property="job" jdbcType="INTEGER" />
<result column="face_image" property="faceImage" jdbcType="VARCHAR" />
<result column="province" property="province" jdbcType="VARCHAR" />
<result column="city" property="city" jdbcType="VARCHAR" />
<result column="district" property="district" jdbcType="VARCHAR" />
<result column="address" property="address" jdbcType="VARCHAR" />
<result column="auth_salt" property="authSalt" jdbcType="VARCHAR" />
<result column="last_login_ip" property="lastLoginIp" jdbcType="VARCHAR" />
<result column="last_login_time" property="lastLoginTime" jdbcType="TIMESTAMP" />
<result column="is_delete" property="isDelete" jdbcType="INTEGER" />
<result column="regist_time" property="registTime" jdbcType="TIMESTAMP" />
</resultMap>
</mapper>
- 将mapper进行扫描
//扫描 mybatis mapper 包路径
@MapperScan(basePackages = "com.imooc.mapper")
使用
- service层使用
@Autowired
private SysUserMapper userMapper;
userMapper.updateByPrimaryKey(user);
分页
//在调用mapper的上一行进行分页设置即可
PageHelper.startPage(page, pageSize);
mybatis整合事务
事务是在service层添加
- 增删改操作所需事务:
@Transactional(propagation = Propagation.REQUIRED)
- 查事务:
@Transactional(propagation = Propagation.SUPPORTS)