• MyBatis基础之注解与SQL 语句构建器


    在这里插入图片描述

    注解实现简单增删改查

    MyBatis 的核心配置文件中,你需要配置的不是 mapper 映射文件,而是 Mapper 接口所在的包路径。

    
    <mappers>
        <package name="com.example.dao"/>
    mappers>
    
    • 1
    • 2
    • 3
    • 4

    另外,我们也不再需要 mapper 映射文件。对于 DAO 中的方法所对应的 SQL 语句,我们直接以注解的形式标注在方法上。

    public interface DepartmentMapper {
    
        @Select("select * from dept where deptno = #{id}")
        Department selectByPK(int id);
    
        @Select("select * from dept")
        List<Department> select();
    
        @Delete("delete from dept where deptno = #{id}")
        int delete(int id);
    
        @Insert("insert into dept values(NULL, #{name}, #{location})")
        @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "deptno")
        int insert(Department dept);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    上述代码中的注解很好理解。唯一需要注意的是,如果在执行 insert 语句时,需要启用 MyBatis 的「主键回填」功能,需要多使用一个 @Options 注解。

    其实上面的接口的定义中所使用的注解的作用显而易见,本质上就是把你曾经写在 XML 配置文件中的 SQL 语句「搬」到了 Java 代码中。

    SQL 语句构建器

    @SelectProvider举例

    @SelectProvider 功能就是用来单独写一个类与方法,用来提供一些 XML 或者注解中不好写的 SQL 。

    写一个简单的 @SelectProvider 的用法:

    先在DepartmentMapper/DepartmentDao 中写一个接口,配上注解。

    @SelectProvider(type = MySelectSqlProvider.class, method = "selectByPK")
    Department selectDepartmentByPK(long id);
    
    • 1
    • 2

    新建MySelectSqlProvider类,添加selectByPK方法,这个方法返回你「心里想要」执行的 SQL 语句:

    public class MySelectSqlProvider {
    
        public String selectByPK(Long id) {
            return "SELECT * FROM department where id = " + id;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    我们在这里直接返回了一个 String,当然你开可以用 StringBuffer 对象来拼接一个 SQL 语句,这样可读性更好一些。

    public class MySelectSqlProvider {
    	public String selectByPK(@Param("id") Long id, @Param("name") String name){
    	        StringBuffer sql = new StringBuffer();
    	        sql.append(" SELECT * FROM department where id = #{id}");
    	        sql.append(" and name = #{name} ");
    	        return sql.toString();
    	 }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    更多、更优雅的写法是:通过 MyBatis 中的 SQL 工具类(SQL Builder 工具类已被淘汰)的拼接一个 SQL 语句。SQL 工具类的写法在Mybatis官网-SQL 语句构建器

    最后在Service中调用Mapper方法即可

    [!attention] 注意
    在使用 @SelectProvider 时,我们在 XML 中没有对应的 SQL(甚至 myabtis 甚至都不知道有 XML 配置文件的存在,因为我们在核心配置文件中映射的是 dao 接口所在的包,而不是 Mapper.xml 配置文件),而且我们在接口的查询方法上也没有 @Select 注解修饰,只有 @SelectProvider 注解,@SelectProvider 中两个属性:type 为提供 SQL 语句(字符串)的类,method 指定提供 SQL 语句(字符串)的具体方法。

    另外,除了有 @SelectProvider 之外,还有 @InsertProvider@UpdateProvider@DeleteProvider

  • 相关阅读:
    面试碰到分布式技术面试题该怎么解答?
    Qt QMultiMap
    Golang中的GMP调度模型
    Kafka系列之:深入理解Kafka 主题、分区、副本、LEO、ISR、HW、Kafka的主写主读和分区leader选举
    苍穹外卖 -- day10- Spring Task- 订单状态定时处理- WebSocket- 来单提醒- 客户催单
    【计算机视觉 | 目标检测】目标检测常用数据集及其介绍(十五)
    自学SAP是学习ECC版本还是S4版本?
    Kubernetes Scheduler全解析
    【Presto Profile系列】Timeline使用
    多快好省,低门槛AI部署工具FastDeploy测试版来了!
  • 原文地址:https://blog.csdn.net/m0_73393501/article/details/133783476