前言:
Mybatis sql 常用配置方法:
- 最常用: resources 目录下编写 *Mapper.xml 在其中编写 sql
- Select 注解: 在 *Dao.java 方法上使用注解 @Select 、@Insert、 @Update、 @Delete 完成sql 注入。
- Java方法返回sql: 在 *Dao.java 方法上使用注解 @SelectProvider、@InsertProvider、 @UpdateProvider、 @DeleteProvider 调用其配置的 Java类中指定的方法完成sql 注入
本篇博文讲解的是 通过 Java方法返回sql , 其上两种sql 注入方式在常用的CRUD开发中是非常常见的。但在一些特定的场景,如根据参数来决定需要查询那张表,查询那些字段时,就需要第三种方式来处理这种复杂的生成逻辑。
官方文档:
Mybatis3 Java API : https://mybatis.org/mybatis-3/zh/java-api.html
官方文档内容截图:
简单实例:
- Maven 配置: org.mybatis.mybatis 版本要在 3.X以上
- 表sql :
CREATE TABLE `user` (
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键ID',
`name` varchar(30) COLLATE utf8_bin DEFAULT NULL COMMENT '姓名',
`age` int(11) DEFAULT NULL COMMENT '年龄',
`email` varchar(50) COLLATE utf8_bin DEFAULT NULL COMMENT '邮箱',
`version` int(11) DEFAULT '0' COMMENT '数据版本号',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
INSERT INTO `work`.`user`(`id`, `name`, `age`, `email`, `version`) VALUES (1, 'tom', 18, 'zhangyong01245@163.com', 1);
- UserMapper.Java
import com.lot.learn.entity.User;
import com.lot.learn.sqlprovider.UserSqlProvider;
import org.apache.ibatis.annotations.*;
import java.util.List;
public interface UserMapper {
@Results(id = "userMap", value = {
@Result(column = "id", property = "id"),
@Result(column = "name", property = "name"),
@Result(column = "age", property = "age"),
@Result(column = "email", property = "email"),
@Result(column = "version", property = "version"),
})
@SelectProvider(type= UserSqlProvider.class,method="selectById")
User selectById(@Param("id") Long id);
}
- User.java
public class User {
private Long id;
private String name;
private Integer age;
private String email;
private Integer version;
// get set 省略........
}
- UserSqlProvider.java
import com.lot.learn.entity.User;
import org.apache.ibatis.jdbc.SQL;
public class UserSqlProvider {
public String selectById(Long id){
return new SQL().SELECT("*").FROM("user").WHERE(" id = #{id}" ).toString();
}
}
UserTableTest.java
package com.lot.learn;
import com.google.gson.Gson;
import com.lot.learn.dao.UserMapper;
import com.lot.learn.entity.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
/**
* @Des: -
* @author: Zhangyong
* @date: 2020-12-11
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MybatisDynamicSqlApplication.class)
public class UserTableTest {
@Resource
private UserMapper userMapper;
@Test
public void testSelect(){
User user = userMapper.selectById(1L);
System.out.println("selectById:" + new Gson().toJson(user));
}
}
- 执行结果:
实例解析:
- 项目是由SpringBoot 搭建的一个简单的系统,其中关键的代码是 UserMapper.selectById 方法上的注解, @SelectProvider 包含两个参数, type:表示要去执行的类, method: 执行的方法,其必须是 public ,返回String 类型。
- 则 UserSqlProvider 利用 org.apache.ibatis.jdbc.SQL 来动态生成要执行的Sql, 该方法生成SQL的主要用途是,我们可以利用 SelectProvider 注解将生成sql的过程放置到我们的Java代码中,程序员可以根据方法里的参数完成sql 的组装
其他:
除了 @SelectProvider 注解、 还有: @InsertProvider、@UpdateProvider、@DeleteProvider 分别对应的是新增、修改、删除, 由于篇幅有限,有兴趣的朋友可以下载一下博主上传的免费资源看一下. https://download.csdn.net/download/zhangyong01245/13689659