MyBatis高级 构建对象介绍

SQL构建对象介绍

我们之前通过注解开发时,相关的SQL语句都是自己拼写的。一些关键字写起来比较麻烦,而且容易出错

 MyBatis给我们提供了org.apache.ibatis.jdbc.SQL功能类,专门用于构建SQL语句

 

 sql包下 SqlTest

package itheima.sql;


import org.apache.ibatis.jdbc.SQL;

public class SqlTest {
    public static void main(String[] args) {
        String sql=getSql();
        System.out.println(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的方法。

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

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

  method属性:指定调用方法

ReturnSql方法

package itheima.sql;

import org.apache.ibatis.jdbc.SQL;

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

  StudentMapper查询全部注解

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

 test包下Test01

package itheima.test;

import itheima.bean.Student;
import 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();
    }
}

  结果如图

 

   新增操作

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

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

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

  method属性:指定调用方法

ReturnSql中定义方法

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

            }
        }.toString();
    }

  StudentMapper新增注解

 //新增操作
    //@Insert("insert into Student v alues(#{id},#{name},#{age})")
    @InsertProvider(type = ReturnSql.class,method = "getInsert")
    public abstract Integer insert(Student stu);

  Test01新增方法

  @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(9,"赵气",26);
        Integer result = mapper.insert(stu);

        //6.处理结果
        System.out.println(result);

        //7.释放资源
        sqlSession.close();
        is.close();
    }

  修改操作

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

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

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

  method属性:指定调用方法

ReturnSql

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

  StudentMapper

 //修改操作
    //@Update("update Student set name=#{name},age=#{age} where id=#{id}")
    @UpdateProvider(type = ReturnSql.class,method = "getUpdate")
    public abstract Integer update(Student stu);

  Test01

 @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(9,"赵爸",26);
        Integer result = mapper.update(stu);

        //6.处理结果
        System.out.println(result);

        //7.释放资源
        sqlSession.close();
        is.close();
    }

  删除操作

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

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

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

  method属性:指定调用方法

ReturnSQL

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

  StudentMapper

 //删除操作
    //@Delete("delete from Student where id=#{id}")
    @DeleteProvider(type = ReturnSql.class,method = "getDelete")
    public abstract Integer delete(Integer id);

  Test01

 @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(9);

        //6.处理结果
        System.out.println(result);

        //7.释放资源
        sqlSession.close();
        is.close();
    }

  构建SQL语句小结

org.apache.ibatis.jdbc.SQLL:构建SQL语句的功能类。通过一些方法来代替SQL关键字。

  SELECT()

  FROM()

  WHERE()

  INSERT_INTO()

  VALUES()

  UPDATE()

  DELETE_FROM()

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

 @InsertProvider:生成新增用的SQL注解

 @UpdateProvider:生成修改用的SQL注解

 @DeleteProvider:生成删除用的SQL注解

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

  method属性:指定调用方法

 

posted @ 2021-05-30 19:56  星梦泪痕  阅读(93)  评论(0编辑  收藏  举报