3-Mybatis高级

网友投稿 600 2022-11-16

3-Mybatis高级

3-Mybatis高级

1.Mybatis 注解开发2.Mybatis 注解实现多表操作3.Mybatis 构建SQL语句4.Mybatis 案例实现1.Mybatis 注解开发常用注解@Select("查询的SQL语句"): 执行查询操作注解@Insert("新增的SQL语句"): 执行新增操作注解@Update("修改的SQL语句"): 执行修改操作注解@Delete("删除的SQL语句"): 执行删除操作注解配置映射关系

注解实现查询操作 1.创建接口和查询方法 2.在核心配置文件中配置映射关系 3.编写测试类封装类

public class Student { private Integer id; private String name; private Integer age;

接口类-注解方法编写sql语句

public interface StudentMapper { //查询全部的操作 @Select("SELECT * FROM student") public abstract List selectAll();}

核心配置文件,只需添加配置文件,不需要加载映射配置文件标签

测试单元

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

//新增操作 @Insert("insert into student values(#{id},#{name},#{age})") public abstract Integer insert(Student stu);

核心配置文件不变测试单元

@Test public void insert() throws IOException { //注意: 这里前面四个步骤和查询的测试单元一样,我这里省略了.......... //调用实现类中的方法,接收结果 Student stu1 = new Student(12, "金吒", 555); Integer insert = mapper.insert(stu1); //处理结果 System.out.println(insert); sqlSession.close(); is.close(); }

//修改操作 @Update("update student set name=#{name},age=#{age} where id=#{id}") public abstract Integer update(Student stu);

核心配置文件不变测试单元

@Test public void upadte() throws IOException { //注意: 这里前面四个步骤和查询的测试单元一样,我这里省略了.......... //调用实现类中的方法,接收结果 Student stu1 = new Student(12, "哪吒", 18); Integer result= mapper.update(stu1); //处理结果 System.out.println(result); sqlSession.close(); is.close(); }

//删除操作 @Delete("delete from student where id=#{id}") public abstract Integer delete(Integer id);

核心配置文件不变测试单元

@Test public void delete() throws IOException { //注意: 这里前面四个步骤和查询的测试单元一样,我这里省略了.......... //调用实现类中的方法,接收结果 Integer result = mapper.delete(12); //处理结果 System.out.println(result); sqlSession.close(); is.close(); }

//get,set,toString等方法省略public class Card { private Integer id; private String number; private Person P;}

//get,set,toString等方法省略public class Person { private Integer id; private String name; private Integer age;}

核心配置文件

查询全部接口

//一对一注解开发方式public interface CardMapper { //查询全部 @Select("select * from card") @Results({ @Result(column = "id",property = "id"), @Result(column = "number",property = "number"), @Result( property = "P", //被包含对象的变量名 javaType = Person.class, //被包含对象的实际数据类型 column = "pid", //根据查询处的card表中的pid字段,来查询person表 /* one,@One: 一对一查询的固定写法 select属性: 指定调用哪个接口中的哪个方法 */ one=@One(select ="com.itheima.one_to_one.PersonMapper.selectById" ) ) }) public abstract List selectAll();}

根据id查询接口

public interface PersonMapper { //根据id查询 @Select("select * from person where id=#{id}") public abstract Person selectById(Integer id);}

测试单元

public class Test01 { @Test public void selectAll() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); //获取SqlSession工厂类对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //通过工厂类对象获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取StudentMapper接口的实现类对象 CardMapper mapper = sqlSession.getMapper(CardMapper.class); //调用实现类中的方法,接收结果 List list = mapper.selectAll(); //处理结果 for (Card card : list) { System.out.println(card); } sqlSession.close(); is.close(); }}

//get,set,等方法省略public class Person { private Integer id; private String name; private Integer age;}

//get,set,等方法省略public class Classes { private Integer id; //主键id private String name; //班级名称 private List students; //班级中所有学生对象

一对多的注解开发的接口

public interface ClassesMapper { //查询全部 @Select("Select * from classes") @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result( property = "students", //被包含对象的变量名 javaType = List.class, //被包含对象的数据类型 column = "id", //根据查询处的classes表的id字段来查询student表 /* 一对多固定的写法 */ many=@Many(select = "com.itheima.one_to_many.StudentMapper.selectById") ) }) public abstract List selectAll();}

以ClassMapper接口的查询结果的id,作为StudentMapper接口中查询方法的参数

public interface StudentMapper { //根据id查询 @Select("Select * from student where cid=#{id}") public abstract List selectById(Integer id);}

测试单元

public class Test1 { @Test public void selectAll() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); //获取SqlSession工厂类对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //通过工厂类对象获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取StudentMapper接口的实现类对象 ClassesMapper mapper = sqlSession.getMapper(ClassesMapper.class); //调用实现类中的方法,接收结果 List list = mapper.selectAll(); //处理结果 for (Classes cls : list) { System.out.println(cls.getId()+","+cls.getName()); List stu = cls.getStudents(); for (Student student : stu) { System.out.println(student); } } sqlSession.close(); is.close(); }}

public class Course { private Integer id; //主键id private String name; //课程名称}

public class StudentAddCource { private Integer id; //主键id private String name; //学生新明 private Integer age; //学生年龄 private List courses; //指当前学生所选择的课程集合}

接口

public interface CourseMapper { //根据id查询 @Select("select c.id,c.name from stu_cr sc,course c where sc.cid=c.id and sc.sid=#{id}") public abstract List selectById();}

查询所有学生接口,通过结果的id,调用CourseMapper中的根据id查询的方法

public interface StudentMapper { //查询全部 @Select("SELECT DISTINCT s.id,s.name,s.age FROM student s, stu_cr sc WHERE sc.sid=s.id") //这条语句主要是筛选掉没有选课的学生 @Results({ @Result(column = "id",property = "id"), @Result(column = "name",property = "name"), @Result(column = "age",property = "age"), @Result( property = "courses", javaType = List.class, column = "id", many =@Many(select = "com.itheima.many_to_many.CourseMapper.selectById") ) }) public abstract List selectAll();}

测试单元

@org.junit.Test public void selectAll() throws IOException { //加载核心配置文件 InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml"); //获取SqlSession工厂类对象 SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is); //通过工厂类对象获取SqlSession对象 SqlSession sqlSession = sqlSessionFactory.openSession(true); //获取StudentMapper接口的实现类对象 StudentMapper mapper = sqlSession.getMapper(StudentMapper.class); //调用实现类中的方法,接收结果 List studentAddCources = mapper.selectAll(); //处理结果 for (StudentAddCource cls : studentAddCources) { System.out.println(cls.getId()+","+cls.getName()+","+cls.getAge()); List courses = cls.getCourses(); for (Course cours : courses) { System.out.println("\t"+cours); } } sqlSession.close(); is.close(); }

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

定义功能类并提供获取新增的SQL语句的方法@InsertProvider: 生成新增用的SQL语句注解type属性: 生成SQL语句功能类对象method属性: 指定调用方法

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

定义功能类并提供获取修改的SQL语句的方法@UpdateProvider: 生成修改用的SQL语句注释type属性: 生成SQL语句功能类对象method属性: 指定调用方法

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

定义功能类并提供获取删除的SQL语句的方法DeleteProvider: 生成删除用的SQL语句注解type属性: 生成SQL语句功能类对象method属性: 指定调用方法

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

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:小程序开发基础-swiper 滑块视图容器
下一篇:微信小程序之视图容器(swiper)组件创建轮播图
相关文章

 发表评论

暂时没有评论,来抢沙发吧~