通用Mapper

快速入门:

  1. 导入集成SpringBoot所需的依赖
<dependency>
    <groupId>tk.mybatis</groupId>
    <artifactId>mapper-spring-boot-starter</artifactId>
    <version>2.1.5</version>
</dependency>
  1. 实现Mapper接口,接口泛型为操作的JavaBean( User )
@Mapper
public interface UserDao extends tk.mybatis.mapper.common.Mapper<User> {
}
  1. 通用Mapper的简单方法

通用 Mapper 提供了大量的通用接口,这里以最常用的 Mapper 接口为例

该接口默认继承的方法如下:

  • selectOne
  • select
  • selectAll
  • selectCount
  • selectByPrimaryKey
//从 MyBatis 或者 Spring 中获取 UserDao,然后调用 selectAll 方法
List<User> users = userDao.selectAll();

//根据主键查询
User user = userDao.selectByPrimaryKey(1);

//或者使用对象传参,适用于1个字段或者多个字段联合主键使用
User user1 = new User();
user1.setId(1);
user2 = userDao.selectByPrimaryKey(user1);

通用Mapper的常用注解

@Table 注解
  • 作用:建立实体类和数据库表之间的对应关系。
  • 默认规则:实体类类名首字母小写作为表名。User 类→user 表。
  • 用法:在@Table 注解的 name 属性中指定目标数据库表的表名
@Column 注解
  • 作用:建立实体类字段和数据库表字段之间的对应关系。
  • 默认规则:
    实体类字段:驼峰式命名
    数据库表字段:使用“_”区分各个单词
  • 用法:在@Column 注解的 name 属性中指定目标字段的字段名
@Id 注解

通用 Mapper 在执行 xxxByPrimaryKey(key)方法时,有两种情况。

情况 1:没有使用@Id 注解明确指定主键字段


之所以会生成上面这样的 WHERE 子句是因为通用 Mapper 将实体类中的所有字段都拿来放在一起作为联合主键。

情况 2:使用@Id 主键明确标记和数据库表中主键字段对应的实体类字段

@GeneratedValue 注解

作用:让通用 Mapper 在执行 insert 操作之后将数据库自动生成的主键值回写到实体类对象中。

  • 自增主键用法:
  • 序列主键用法:
@Transient 注解

用于标记不与数据库表字段对应的实体类字段。

@Transient
private String otherThings; //非数据库表中字段



通用Mapper的常用方法

selectOne 方法
  • 通用 Mapper 替我们自动生成的 SQL 语句情况
  • 实体类封装查询条件生成 WHERE 子句的规则
    • 使用非空的值生成 WHERE 子句
    • 在条件表达式中使用“=”进行比较

注意:要求必须返回一个实体类结果,如果有多个,则会抛出异常

xxxByPrimaryKey 方法 方法

需要使用@Id 主键明确标记和数据库表主键字段对应的实体类字段,否则通用Mapper 会将所有实体类字段作为联合主键。

xxxSelective 方法 方法

非主键字段如果为 null 值,则不加入到 SQL 语句中。



QBC 查询

概念:
Query By Criteria
Criteria 是 Criterion 的复数形式。意思是:规则、标准、准则。在 SQL 语句中相当于查询条件。
QBC 查询是将查询条件通过 Java 对象进行模块化封装。

示例代码 :

//目标:WHERE (emp_salary>? AND emp_age<?) OR (emp_salary<? AND emp_age>?)
//1.创建 Example 对象
Example example = new Example(Employee.class);
//***********************
//i.设置排序信息
example.orderBy("empSalary").asc().orderBy("empAge").desc();
//ii.设置“去重”
example.setDistinct(true);
//iii.设置 select 字段
example.selectProperties("empName","empSalary");
//***********************

//2.通过 Example 对象创建 Criteria 对象
Criteria criteria01 = example.createCriteria();
Criteria criteria02 = example.createCriteria();

//目标:WHERE (emp_salary>? AND emp_age<?) OR (emp_salary<? AND emp_age>?)
//3.在两个 Criteria 对象中分别设置查询条件
	//property 参数:实体类的属性名
	//value 参数:实体类的属性值
criteria01.andGreaterThan("empSalary", 3000)
		  .andLessThan("empAge", 25);
criteria02.andLessThan("empSalary", 5000)
		  .andGreaterThan("empAge", 30);

//4.使用 OR 关键词组装两个 Criteria 对象
example.or(criteria02);

//5.执行查询
List<Employee> empList = employeeService.getEmpListByExample(example);

for (Employee employee : empList) {
	System.out.println(employee);
}
  • 5
    点赞
  • 34
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot可以很方便地整合通用Mapper,只需要在pom.xml中添加通用Mapper的依赖,然后在配置文件中配置数据源和通用Mapper的相关属性即可。 具体步骤如下: 1. 在pom.xml中添加通用Mapper的依赖: ```xml <dependency> <groupId>tk.mybatis</groupId> <artifactId>mapper-spring-boot-starter</artifactId> <version>2.1.5</version> </dependency> ``` 2. 在配置文件中配置数据源和通用Mapper的相关属性: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false username: root password: root driver-class-name: com.mysql.jdbc.Driver mybatis: mapper-locations: classpath:mapper/*.xml configuration: map-underscore-to-camel-case: true mapper: mappers: - tk.mybatis.mapper.common.Mapper not-empty: false identity: MYSQL ``` 其中,mapper.mappers指定了要使用的Mapper接口,这里使用了通用MapperMapper接口;mapper.identity指定了主键生成策略,这里使用了MySQL的自增长主键。 3. 在Mapper接口中继承通用MapperMapper接口即可使用通用Mapper提供的方法: ```java public interface UserMapper extends Mapper<User> { } ``` 这样就可以使用通用Mapper提供的方法来进行数据库操作了,例如: ```java @Autowired private UserMapper userMapper; public void addUser(User user) { userMapper.insert(user); } public void updateUser(User user) { userMapper.updateByPrimaryKeySelective(user); } public void deleteUser(Long id) { userMapper.deleteByPrimaryKey(id); } public User getUser(Long id) { return userMapper.selectByPrimaryKey(id); } public List<User> getUsers() { return userMapper.selectAll(); } ``` 以上就是Spring Boot整合通用Mapper的基本步骤,希望能对你有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值