先看一下和MyBatis 不同点说明:
@GetMapping("/select_sql") public Object getUserBySql() { User user=new User(1L); User selectUser=user.selectById(); user.deleteById(user); return userService.selectListBySQL(); //普通走法 }
最大的不同点在entity层直接可以调用superEntity,直接实现默认的一些增删改查
/** * 用户表 */ @SuppressWarnings("serial") public class User extends SuperEntity<User> { /** * 名称 */ private String name; /** * 年龄 */ private AgeEnum age; /** * 这里故意演示注解可无 */ @TableField("test_type") @TableLogic private Integer testType; /** * 测试插入填充 */ @TableField(fill = FieldFill.INSERT) private Date testDate; private Long role; private PhoneEnum phone; public User() { } public User(Long id){ this.setId(id); } public User(Long id, String name, AgeEnum age, Integer testType) { this.setId(id); this.name = name; this.age = age; this.testType = testType; } public User(String name, AgeEnum age, Integer testType) { this.name = name; this.age = age; this.testType = testType; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public AgeEnum getAge() { return this.age; } public void setAge(AgeEnum age) { this.age = age; } public Integer getTestType() { return this.testType; } public void setTestType(Integer testType) { this.testType = testType; } public Long getRole() { return this.role; } public void setRole(Long role) { this.role = role; } public PhoneEnum getPhone() { return this.phone; } public void setPhone(PhoneEnum phone) { this.phone = phone; } public Date getTestDate() { return testDate; } public void setTestDate(Date testDate) { this.testDate = testDate; } @Override public String toString() { return "User [id=" + this.getId() + ", name=" + name + ", age=" + age + ", testType=" + testType + ", testDate=" + testDate + ", role=" + role + ", phone=" + phone + "]"; }
SuperEntity 继承Model ,Model默认实现了简单的增删改查,不需要再去实现mapper层。
/** * 演示实体父类 */ public class SuperEntity<T extends Model> extends Model<T> { /** * 主键ID , 这里故意演示注解可以无 */ @TableId("test_id") private Long id; private Long tenantId; public Long getId() { return this.id; } public void setId(Long id) { this.id = id; } public Long getTenantId() { return tenantId; } public SuperEntity setTenantId(Long tenantId) { this.tenantId = tenantId; return this; } @Override protected Serializable pkVal() { return this.id; } }
2.对于原生service层改造实现,默认实现baseMapper, baseMapper 默认实现了mapper的常规使用写法方式。
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService { @Override public boolean deleteAll() { return retBool(baseMapper.deleteAll()); } @Override public List<User> selectListBySQL() { return baseMapper.selectListBySQL(); } @Override public List<User> selectListByWrapper(Wrapper wrapper) { return baseMapper.selectListByWrapper(wrapper); //baseMapper.selectma } }
3.UserMapper 继承 superMapper,而superMapper继承baseMapper,你可以把一些公共的方法放在superMapper里,从而实现公用。
/** * User 表数据库控制层接口 */ public interface UserMapper extends SuperMapper<User> { /** * 自定义注入方法 */ int deleteAll(); @Select("select test_id as id, name, age, test_type from user") List<User> selectListBySQL(); List<User> selectListByWrapper(@Param("ew") Wrapper wrapper); }
最后的XML是一些不常用的sql可以写在这里给予调用。
<?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.baomidou.springboot.mapper.UserMapper"> <!-- 通用查询结果列 --> <sql id="Base_Column_List"> test_id AS testId, name, age, test_type AS testType, role, phone </sql> <delete id="deleteAll"> DELETE FROM USER </delete> <select id="selectListByWrapper" resultType="com.baomidou.springboot.entity.User"> SELECT * FROM USER <!-- 判断 wrapper 是否为空 emptyOfWhere --> <where> ${ew.sqlSegment} </where> </select> </mapper>
可以看到他们的关系是一层一层过滤设计的,用了公共代码方便了公用,也达到了解耦合。