2019独角兽企业重金招聘Python工程师标准>>>
基于SpringBoot,继承Mybatis后,对其进行简化,尽量减少开发工作量!
实现步骤如下:
一、引入TKMybatis依赖
tk.mybatis
mapper
4.1.5
tk.mybatis
mapper-spring-boot-starter
2.1.5
二、配置实体类
package com.yuq.sunrise.model;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
/**
* 用户信息
* @author Administrator
*
*/
@Table(name = "USER_INFO")
public class User implements Serializable {
private static final long serialVersionUID = 1L;
// @Id表示该字段对应数据库表的主键id
// @GeneratedValue中strategy表示使用数据库自带的主键生成策略.
// @GeneratedValue中generator配置为"JDBC",在数据插入完毕之后,会自动将主键id填充到实体类中.类似普通mapper.xml中配置的selectKey标签
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC")
private Long rowId;
private String loginName;
private String password;
private String realName;
private String telPhone;
private String emailAddress;
private Date createDate;
private Date loginTime;
private Integer loginCount;
private String isVal;
/**
* create by: yuq
* description: 默认构造
* create time: 2019-06-08 19:57
*
* @param
* @return
*/
public User() {
super();
}
/**
* create by: yuq
* description: 构造函数,不包含主键字段
* create time: 2019-06-08 19:57
*
* @param
* @return
*/
public User(String loginName, String password, String realName, String telPhone, String emailAddress, Date createDate, Date loginTime, Integer loginCount, String isVal) {
this.loginName = loginName;
this.password = password;
this.realName = realName;
this.telPhone = telPhone;
this.emailAddress = emailAddress;
this.createDate = createDate;
this.loginTime = loginTime;
this.loginCount = loginCount;
this.isVal = isVal;
}
/**
* create by: yuq
* description: 构造函数,所有字段
* create time: 2019-06-08 19:57
*
* @param
* @return
*/
public User(Long rowId, String loginName, String password, String realName, String telPhone, String emailAddress, Date createDate, Date loginTime, Integer loginCount, String isVal) {
this.rowId = rowId;
this.loginName = loginName;
this.password = password;
this.realName = realName;
this.telPhone = telPhone;
this.emailAddress = emailAddress;
this.createDate = createDate;
this.loginTime = loginTime;
this.loginCount = loginCount;
this.isVal = isVal;
}
public Long getRowId() {
return rowId;
}
public void setRowId(Long rowId) {
this.rowId = rowId;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getRealName() {
return realName;
}
public void setRealName(String realName) {
this.realName = realName;
}
public String getTelPhone() {
return telPhone;
}
public void setTelPhone(String telPhone) {
this.telPhone = telPhone;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
public Date getCreateDate() {
return createDate;
}
public void setCreateDate(Date createDate) {
this.createDate = createDate;
}
public Date getLoginTime() {
return loginTime;
}
public void setLoginTime(Date loginTime) {
this.loginTime = loginTime;
}
public Integer getLoginCount() {
return loginCount;
}
public void setLoginCount(Integer loginCount) {
this.loginCount = loginCount;
}
public String getIsVal() {
return isVal;
}
public void setIsVal(String isVal) {
this.isVal = isVal;
}
}
三、继承TKMybatis的Mapper接口
package com.yuq.sunrise.mapper;
import com.yuq.sunrise.model.User;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
import java.util.Map;
/**
* create by: yuq
* description: TODO(数据库操作层)
* create time: 2019-06-09 14:30
*/
public interface IUserMapper extends Mapper {
/****************************************** 以下为自定义Mapper *****************************************/
List queryAllByLimit(Map params);
}
注:这里的 queryAllByLimit 方法是我的自定义方法,该方法不能与Mapper重复,否则会报错。
四、在Application类上配置@MapperScan
package com.yuq.sunrise;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@MapperScan("com.yuq.sunrise.mapper")
public class SunriseApplication {
public static void main(String[] args) {
SpringApplication.run(SunriseApplication.class, args);
}
}
五、在application.yml或properties中配置mapper.xml
########################################################
### mybatis配置
########################################################
mybatis:
config-location: classpath:mybatis/mybatis-config.xml
mapper-locations: classpath:mybatis/mappers/*.xml
type-aliases-package: com.yuq.sunrise.model
六、测试
package com.yuq.sunrise.mapper;
import com.yuq.sunrise.model.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private IUserMapper userMapper;
@Test
public void testUserMapper() throws Exception {
//example1 mapper.select
User userExample1 = new User();
userExample1.setRealName("超级管理员");
List userListReturn1 = userMapper.select(userExample1);
System.out.println("example1 :" + userListReturn1.size());
//example2 mapper.selectAll
List userListReturn2 = userMapper.selectAll();
System.out.println("example2 :" + userListReturn2.size());
User userExample3 = new User();
userExample3.setRowId(8L);
Integer numReturn3 = userMapper.delete(userExample3);
System.out.println("example3 :" + numReturn3);
User userExample4 = new User();
userExample4.setLoginName("abab");
userExample4.setPassword("aaa");
userExample4.setIsVal("0");
Integer numReturn4 = userMapper.insert(userExample4);
System.out.println("example4 :" + numReturn4);
User userExample5 = new User();
userExample5.setLoginName("abab");
userExample5.setPassword("aaa");
userExample5.setIsVal("0");
userExample5.setRealName("呵呵");
userExample5.setRowId(15L);
Integer numReturn5 = userMapper.updateByPrimaryKey(userExample5);
System.out.println("example5 :" + numReturn5);
/********************以上为Mapper中默认的接口**********************/
/********************以下是自定义接口**********************/
Map paramExample6 = new HashMap();
paramExample6.put("offset",1);
paramExample6.put("limit",5);
List userExample6 = userMapper.queryAllByLimit(paramExample6);
System.out.println(userExample6.size());
}
}
注意:自定义的mapper如下
`ROW_ID`,
`LOGIN_NAME`,
`PASSWORD`,
`REAL_NAME`,
`TEL_PHONE`,
`EMAIL_ADDRESS`,
`CREATE_DATE`,
`LOGIN_TIME`,
`LOGIN_COUNT`,
`IS_VAL`