2019独角兽企业重金招聘Python工程师标准>>>
基于SpringBoot,继承Mybatis后,对其进行简化,尽量减少开发工作量!
实现步骤如下:
一、引入TKMybatis依赖
<!-- 引入TkMybatis的Maven依赖 --><dependency><groupId>tk.mybatis</groupId><artifactId>mapper</artifactId><version>4.1.5</version></dependency><dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>2.1.5</version></dependency>
二、配置实体类
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<User> {/****************************************** 以下为自定义Mapper *****************************************/List<User> queryAllByLimit(Map<String, Object> 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.xmlmapper-locations: classpath:mybatis/mappers/*.xmltype-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 {@Autowiredprivate IUserMapper userMapper;@Testpublic void testUserMapper() throws Exception {//example1 mapper.selectUser userExample1 = new User();userExample1.setRealName("超级管理员");List<User> userListReturn1 = userMapper.select(userExample1);System.out.println("example1 :" + userListReturn1.size());//example2 mapper.selectAllList<User> 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<String, Object> paramExample6 = new HashMap<String, Object>();paramExample6.put("offset",1);paramExample6.put("limit",5);List<User> userExample6 = userMapper.queryAllByLimit(paramExample6);System.out.println(userExample6.size());}}
注意:自定义的mapper如下
<?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.yuq.sunrise.mapper.IUserMapper"><!-- 自定义业务sql, begin --><resultMap id="dataMap" type="com.yuq.sunrise.model.User"><result column="ROW_ID" property="rowId"/><result column="LOGIN_NAME" property="loginName"/><result column="PASSWORD" property="password"/><result column="REAL_NAME" property="realName"/><result column="TEL_PHONE" property="telPhone"/><result column="EMAIL_ADDRESS" property="emailAddress"/><result column="CREATE_DATE" property="createDate"/><result column="LOGIN_TIME" property="loginTime"/><result column="LOGIN_COUNT" property="loginCount"/><result column="IS_VAL" property="isVal"/></resultMap><sql id="Base_Column_List">`ROW_ID`,`LOGIN_NAME`,`PASSWORD`,`REAL_NAME`,`TEL_PHONE`,`EMAIL_ADDRESS`,`CREATE_DATE`,`LOGIN_TIME`,`LOGIN_COUNT`,`IS_VAL`</sql><select id="queryAllByLimit" resultMap="dataMap">select<include refid="Base_Column_List" />from USER_INFO<if test="offset != null and limit != null">limit #{offset}, #{limit}</if></select><!-- 自定义业务sql, end --></mapper>