程序员最近都爱上了这个网站  程序员们快来瞅瞅吧!  it98k网:it98k.com

本站消息

站长简介/公众号

  出租广告位,需要合作请联系站长


+关注
已关注

分类  

java(0)

标签  

暂无标签

日期归档  

2023-06(2)

MyBatis-10-笔记

发布于2021-06-12 12:47     阅读(548)     评论(0)     点赞(4)     收藏(5)


构建sql

1 SQL 构建对象介绍

  • 我们之前通过注解开发时,相关 SQL 语句都是自己直接拼写的。一些关键字写起来比较麻烦、而且容易出错。
  • MyBatis 提供了 org.apache.ibatis.jdbc.SQL 功能类,专门用于构建 SQL 语句,通过一些方法来替换SQL语句的关键字。
  • SQL语句要是熟练,这玩意可以忽略,个人感觉这个类挺废的@_@
  • 注意只能在注解开发的方式中使用
  • 看完之后思考一个问题,你认为一个对SQL语句不熟的人会对这个SQL对象的方法熟吗?
  • 就是脱裤子放屁——多此一举
    在这里插入图片描述
  • 简单演示——定义方法,获取查询student表的sql语句

    package com.itheima.sql;
    
    import org.apache.ibatis.jdbc.SQL;
    
    public class SqlTest {
        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;
        }*/
    	//两个方法是一样的 只不过下面的是使用一些提供的方法来写sql语句
        public static String getSql() {
            String sql = new SQL(){
                {
                    SELECT("*");
                    FROM("student");
                }
            }.toString();
    
            return sql;
        }
    }
    

2 查询功能的实现

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

  • @SelectProvider:生成查询用的 SQL 语句注解。

    type 属性:生成 SQL 语句功能类对象

    method 属性:指定调用方法

  • 定义一个生成SQL语句的功能类

     package com.itheima.sql;
    
    import com.itheima.bean.Student;
    import org.apache.ibatis.jdbc.SQL;
    
    public class ReturnSql {
        //定义方法,返回查询的sql语句
        public String getSelectAll() {
            return new SQL() {
                {
                    SELECT("*");
                    FROM("student");
                }
            }.toString();
        }
    }
    
  • 修改之前注解方式的Mapper接口

    package com.itheima.mapper;
    
    public interface StudentMapper {
        //查询全部
        //@Select("SELECT * FROM student")
        @SelectProvider(type = ReturnSql.class , method = "getSelectAll")
        public abstract List<Student> selectAll();
    }
    
  • 测试类

    package com.itheima.test;
    
    public class Test01 {
        @Test
        public void selectAll() 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.调用实现类对象中的方法,接收结果
            List<Student> list = mapper.selectAll();
    
            //6.处理结果
            for (Student student : list) {
                System.out.println(student);
            }
    
            //7.释放资源
            sqlSession.close();
            is.close();
        }
    }
    

3 新增功能的实现

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

  • @InsertProvider:生成新增用的 SQL 语句注解。

    type 属性:生成 SQL 语句功能类对象

    method 属性:指定调用方法

4 修改功能的实现

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

  • @UpdateProvider:生成修改用的 SQL 语句注解。

    type 属性:生成 SQL 语句功能类对象

    method 属性:指定调用方法

5 删除功能的实现

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

  • @DeleteProvider:生成删除用的 SQL 语句注解。

    type 属性:生成 SQL 语句功能类对象

    method 属性:指定调用方法

6 代码演示

  • 定义一个生成SQL语句的功能类

    package com.itheima.sql;
    
    import com.itheima.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();
        }
    }
    
    
  • 修改之前注解方式的Mapper接口

    package com.itheima.mapper;
    
    import com.itheima.bean.Student;
    import com.itheima.sql.ReturnSql;
    import org.apache.ibatis.annotations.DeleteProvider;
    import org.apache.ibatis.annotations.InsertProvider;
    import org.apache.ibatis.annotations.SelectProvider;
    import org.apache.ibatis.annotations.UpdateProvider;
    
    import java.util.List;
    
    public interface StudentMapper {
        //查询全部
        //@Select("SELECT * FROM student")
        @SelectProvider(type = ReturnSql.class , method = "getSelectAll")
        public abstract List<Student> selectAll();
    
        //新增功能
        //@Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
        @InsertProvider(type = ReturnSql.class , method = "getInsert")
        public abstract Integer insert(Student stu);
    
        //修改功能
        //@Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
        @UpdateProvider(type = ReturnSql.class , method = "getUpdate")
        public abstract Integer update(Student stu);
    
        //删除功能
        //@Delete("DELETE FROM student WHERE id=#{id}")
        @DeleteProvider(type = ReturnSql.class , method = "getDelete")
        public abstract Integer delete(Integer id);
    }
    
    
  • 测试类

    package com.itheima.test;
    
    import com.itheima.bean.Student;
    import com.itheima.mapper.StudentMapper;
    import org.apache.ibatis.io.Resources;
    import org.apache.ibatis.session.SqlSession;
    import org.apache.ibatis.session.SqlSessionFactory;
    import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    import org.junit.Test;
    
    import java.io.InputStream;
    import java.util.List;
    
    public class Test01 {
        @Test
        public void selectAll() 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.调用实现类对象中的方法,接收结果
            List<Student> list = mapper.selectAll();
    
            //6.处理结果
            for (Student student : list) {
                System.out.println(student);
            }
    
            //7.释放资源
            sqlSession.close();
            is.close();
        }
    
        @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();
        }
    
        @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(4,"赵六",36);
            Integer result = mapper.update(stu);
    
            //6.处理结果
            System.out.println(result);
    
            //7.释放资源
            sqlSession.close();
            is.close();
        }
    
        @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(4);
    
            //6.处理结果
            System.out.println(result);
    
            //7.释放资源
            sqlSession.close();
            is.close();
        }
    }
    
    


所属网站分类: 技术文章 > 博客

作者:java王侯

链接:http://www.javaheidong.com/blog/article/222004/2109ff2a057d002eeb93/

来源:java黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

4 0
收藏该文
已收藏

评论内容:(最多支持255个字符)