Mybatis构建SQL——有码有依靠,有图有真相

Posted 流楚丶格念

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis构建SQL——有码有依靠,有图有真相相关的知识,希望对你有一定的参考价值。

文章目录

1. 构建sql

1.1 SQL 构建对象介绍

1.1.1 介绍

  • 我们之前通过注解开发时,相关 SQL 语句都是自己直接拼写的。一些关键字写起来比较麻烦、而且容易出错。
  • MyBatis 给我们提供了 org.apache.ibatis.jdbc.SQL 功能类,专门用于构建 SQL 语句

1.1.2 实现准备

编写指定调用方法

package com.yyl.sql;

import com.yyl.bean.Student;
import org.apache.ibatis.jdbc.SQL;

public class ReturnSql 
    //定义方法,返回查询的sql语句
    public String getSelectAll() 
        return new SQL() 
            
                SELECT("*");
                FROM("student");
            
        .toString();
    

    //定义方法,返回新增的sql语句
    public String getInsert(Student stu) 
        return new SQL() 
            
                INSERT_INTO("student");
                INTO_VALUES("#id,#name,#age");
            
        .toString();
    

    //定义方法,返回修改的sql语句
    public String getUpdate(Student stu) 
        return new SQL() 
            
                UPDATE("student");
                SET("name=#name","age=#age");
                WHERE("id=#id");
            
        .toString();
    

    //定义方法,返回删除的sql语句
    public String getDelete(Integer id) 
        return new SQL() 
            
                DELETE_FROM("student");
                WHERE("id=#id");
            
        .toString();
    


这是嘛啊???

这就是Mybatis进行构建时候给你的构建SQL对象,就例如下面咱对new 处理的这个SQL对象进行测试:

public static void main(String[] args) 
    String sql = getSql();
    System.out.println(sql);


//定义方法,获取查询student表的sql语句
/*public static String getSql() 
    String sql = "SELECT * FROM student";
    return sql;
*/

public static String getSql() 
    String sql = new SQL()
        
            SELECT("*");
            FROM("student");
        
    .toString();

    return sql;

运行结果如下:

就是返回sql语句

1.2 查询功能的实现

定义功能类并提供获取查询的 SQL 语句的方法。

例如下面的语句把之前的@Select换为了现在的@SelectProvider

//查询全部
//@Select("SELECT * FROM student")
@SelectProvider(type = ReturnSql.class , method = "getSelectAll")
public abstract List<Student> selectAll();

属性说明:

属性说明
@SelectProvider生成查询用的 SQL 语句注解。
type 属性生成 SQL 语句功能类对象
method 属性指定调用方法

编写测试函数

@Test
public void selectAll() throws Exception
    //1.加载核心配置文件
    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    //2.获取SqlSession工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().b
    //3.通过工厂对象获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //4.获取StudentMapper接口的实现类对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //5.调用实现类对象中的方法,接收结果
    List<Student> list = mapper.selectAll();
    //6.处理结果
    for (Student student : list) 
        System.out.println(student);
    
    //7.释放资源
    sqlSession.close();
    is.close();

运行结果如下:

1.3 新增功能的实现

定义功能类并提供获取新增的 SQL 语句的方法。

//新增功能
//@Insert("INSERT INTO student VALUES (#id,#name,#age)")
@InsertProvider(type = ReturnSql.class , method = "getInsert")
public abstract Integer insert(Student stu);

属性说明:

属性说明
@InsertProvider生成新增用的 SQL 语句注解。
type 属性生成 SQL 语句功能类对象
method 属性指定调用方法

编写测试函数:

@Test
public void insert() throws Exception
    //1.加载核心配置文件
    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    //2.获取SqlSession工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    //3.通过工厂对象获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //4.获取StudentMapper接口的实现类对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //5.调用实现类对象中的方法,接收结果
    Student stu = new Student(4,"赵六",26);
    Integer result = mapper.insert(stu);
    //6.处理结果
    System.out.println(result);
    //7.释放资源
    sqlSession.close();
    is.close();

运行效果如下:

1.4 修改功能的实现

定义功能类并提供获取修改的 SQL 语句的方法。

//修改功能
//@Update("UPDATE student SET name=#name,ag
@UpdateProvider(type = ReturnSql.class , meth
public abstract Integer update(Student stu);

属性说明:

属性说明
@UpdateProvider生成修改用的 SQL 语句注解。
type 属性生成 SQL 语句功能类对象
method 属性指定调用方法

编写测试函数:

@Test
public void update() throws Exception
    //1.加载核心配置文件
    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    //2.获取SqlSession工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    //3.通过工厂对象获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //4.获取StudentMapper接口的实现类对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //5.调用实现类对象中的方法,接收结果
    Student stu = new Student(7,"赵六",36);
    Integer result = mapper.update(stu);
    //6.处理结果
    System.out.println(result);
    //7.释放资源
    sqlSession.close();
    is.close();

运行效果如下:

1.5 删除功能的实现

定义功能类并提供获取删除的 SQL 语句的方法。

//删除功能
//@Delete("DELETE FROM student WHERE id=#id")
@DeleteProvider(type = ReturnSql.class , method
public abstract Integer delete(Integer id);

属性说明:

属性说明
@DeleteProvider生成删除用的 SQL 语句注解。
type 属性生成 SQL 语句功能类对象
method 属性指定调用方法

编写测试函数:

@Test
public void delete() throws Exception
    //1.加载核心配置文件
    InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    //2.获取SqlSession工厂对象
    SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    //3.通过工厂对象获取SqlSession对象
    SqlSession sqlSession = sqlSessionFactory.openSession(true);
    //4.获取StudentMapper接口的实现类对象
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //5.调用实现类对象中的方法,接收结果
    Integer result = mapper.delete(7);
    //6.处理结果
    System.out.println(result);
    //7.释放资源
    sqlSession.close();
    is.close();

运行效果如下:

以上是关于Mybatis构建SQL——有码有依靠,有图有真相的主要内容,如果未能解决你的问题,请参考以下文章

非旋 treap 结构体数组版(无指针)详解,有图有真相

教你 21 天学会 C++(有图有真相)

async ,await 有图有真相

C#项目间循环引用的解决办法,有图有真相

孙武玩《魔兽》?有图有真相

Qt中QGraphics类坐标映射关系详解(有图有真相,实例讲解)