iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >Mybatis超详细讲解构建SQL方法
  • 669
分享到

Mybatis超详细讲解构建SQL方法

2024-04-02 19:04:59 669人浏览 八月长安

Python 官方文档:入门教程 => 点击学习

摘要

目录1 sql 构建对象介绍1.1.1 介绍1.1.2 实现准备2 查询功能的实现3 新增功能的实现4 修改功能的实现5 删除功能的实现1 SQL 构建对象介绍 1.1.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 = new SQL(){
        {
            SELECT("*");
            FROM("student");
        }
    }.toString();
    return sql;
}

运行结果如下:

就是返回sql语句

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();
}

运行结果如下:

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();
}

运行效果如下:

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();
}

运行效果如下:

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方法的文章就介绍到这了,更多相关Mybatis构建SQL内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Mybatis超详细讲解构建SQL方法

本文链接: https://www.lsjlt.com/news/153499.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作