04_通用mapper
张创琦 2022.03.11
避免重复书写CRUD映射的框架有两个
通用mybatis(tk mybatis)
mybatis plus,通能更加强大,后面实战项目中讲解
1 tk mybatis
- pom.xml 配置文件
<!-- 通用mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.2</version>
</dependency>
- 实体类配置
@Table(name = "tb_user") // 表名相关注解
@Column(name="fieldName") // 属性名和字段名的相关注解
package com.lxs.domain;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.io.Serializable;
import java.util.Date;
@Table(name = "tb_user") // 如果不声明则默认一样的表名
// 支持驼峰转下划线的命名
public class User implements Serializable {
// private static final long serialVersionUID = 1L;
// 主键的映射一定要进行
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY) // 逐渐策略是自增
private Long id;
// 用户名
//自动转换下换线到驼峰命名 user_name -> userName
private String userName;
// 密码
private String password;
// 姓名
private String name;
// 年龄
private Integer age;
// 性别,1男性,2女性
private Integer sex;
// 出生日期
private Date birthday;
// 创建时间
private Date created;
// 更新时间
private Date updated;
// 备注
private String note;
@Override
public String toString() {
return "User{" +
"id=" + id +
", userName='" + userName + '\'' +
", password='" + password + '\'' +
", name='" + name + '\'' +
", age=" + age +
", sex=" + sex +
", birthday=" + birthday +
", created=" + created +
", updated=" + updated +
", note='" + note + '\'' +
'}';
}
// 省略 getter 和 setter 方法
}
- UserMapper
一旦继承了Mapper,继承的Mapper就拥有了Mapper所有的通用方法。
具体方法见 pdf 文档
package com.lxs.dao;
import com.lxs.domain.User;
import tk.mybatis.mapper.common.Mapper;
import java.util.List;
// import org.apache.ibatis.annotations.Mapper;
// 注意:在mapper导包时,导入的是tk.mybatis的
public interface UserMapper extends Mapper<User> {
public List<User> findByUser(User user);
}
注意以下的写法:
package com.lxs.dao;
import com.lxs.domain.User;
//import tk.mybatis.mapper.common.Mapper;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface UserMapper extends tk.mybatis.mapper.common.Mapper<User> {
public List<User> findByUser(User user);
}
- 映射复杂文件:
resources/mappers/UserMapper.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.lxs.dao.UserMapper">
<select id="findByUser" resultType="user">
SELECT * FROM tb_user
<where>
<if test="name != null"> name like '%${name}%' </if>
<if test="note != null"> and note like '%${note}%' </if>
</where>
</select>
</mapper>
- 注意Application的导包问题
一个是 mybatis 的,一个是 tk mybatis 的
// import org.mybatis.spring.annotation.MapperScan; // myBatis的
import org.springframework.boot.SpringApplication; // tk myBatis的
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import tk.mybatis.spring.annotation.MapperScan;
@SpringBootApplication
@EnableConfigurationProperties
@MapperScan("com.lxs.dao")
public class Application {
- 测试
package com.lxs.dao;
import com.lxs.domain.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 tk.mybatis.mapper.entity.Example;
import java.util.Date;
import java.util.List;
import static org.junit.Assert.*;
@RunWith(SpringRunner.class)
@SpringBootTest
public class UserMapperTest {
@Autowired
private UserMapper userMapper;
@Autowired
private UserDao userDao;
/* chenggong */
@Test
public void testFindByUser() {
User condition = new User();
condition.setName("a");
List<User> list = userMapper.findByUser(condition);
for (User user : list) {
System.out.println(user);
}
}
/* chenggong
@Test
public void testFindAll() {
userMapper.selectAll().forEach(user -> {
System.out.println(user);
});
}*/
/* 查询不到userName
@Test
public void testFindAll1() {
List<User> list = userDao.findAll();
for (User user : list) {
System.out.println(user);
}
}*/
/* 未测试
@Test
public void testFindById() {
User user = userDao.selectByPrimaryKey(4);
System.out.println(user);
}
*/
/* 测试成功 */
@Test
public void testFindByExample() {
Example example = new Example(User.class);
example.createCriteria().andLike("name", "%a%");
userMapper.selectByExample(example).forEach(user -> {
System.out.println(user);
});
}
/* 未测试
@Test
public void testInsert() {
User user = new User();
user.setAge(18);
user.setBirthday(new Date());
user.setCreated(new Date());
user.setName("周星驰");
userDao.insert(user);
}
*/
}