当前位置: 首页 > news >正文

Myb atis基础3

  • Mybatis注解开发单表操作
    • Mybatis的常用注解
    • Mybatis的增删改查
  • MyBatis注解开发的多表操作
    • MyBatis的注解实现复杂映射开发
    • 一对一查询
    • 一对多查询
    • 多对多查询
  • 构建sql
    • sql构建对象介绍
    • 查询功能的实现
    • 新增功能的实现
    • 修改功能的实现
    • 删除功能的实现

Mybatis注解开发单表操作

Mybatis的常用注解

使用注解开发方式,可以减少编写Mapper映射文件

@Insert:实现新增

@Update:实现更新

@Delete:实现删除

@Select:实现查询

@Result:实现结果集封装

@Results:可以与@Result 一起使用,封装多个结果集

@One:实现一对一结果集封装

@Many:实现一对多结果集封装

Mybatis的增删改查

步骤一:创建mapper接口

public interface StudentMapper {//查询全部@Select("SELECT * FROM student")public abstract List<Student> selectAll();//新增操作@Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")public abstract Integer insert(Student stu);//修改操作@Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")public abstract Integer update(Student stu);//删除操作@Delete("DELETE FROM student WHERE id=#{id}")public abstract Integer delete(Integer id);
}

步骤二:测试类

public class Test01 {@Testpublic 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();}@Testpublic 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();}@Testpublic 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();}@Testpublic 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();}
}

加载使用了注解的Mapper接口

<mappers><!--扫描使用注解的类--><mapper class="com.itheima.mapper.UserMapper"></mapper>
</mappers>

或者指定扫描包含映射关系的接口所在的包

<mappers><!--扫描使用注解的类所在的包--><package name="com.itheima.mapper"></package>
</mappers>

MyBatis注解开发的多表操作

MyBatis的注解实现复杂映射开发

加粗样式

一对一查询

对应sql语句

SELECT * FROM card;SELECT * FROM person WHERE id=#{id};

创建PersonMapper接口

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

使用注解配置Mapper

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

测试类

public class Test01 {@Testpublic 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.获取CardMapper接口的实现类对象CardMapper mapper = sqlSession.getMapper(CardMapper.class);//5.调用实现类对象中的方法,接收结果List<Card> list = mapper.selectAll();//6.处理结果for (Card card : list) {System.out.println(card);}//7.释放资源sqlSession.close();is.close();}}

总结

@Results:封装映射关系的父注解。Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。column 属性:查询出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被包含对象的数据类型one 属性:一对一查询固定属性@One:一对一查询的注解。select 属性:指定调用某个接口中的方法

一对多查询

使用注解配置Mapper

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属性:指定调用哪个接口中的哪个查询方法*/many = @Many(select = "com.itheima.one_to_many.StudentMapper.selectByCid"))})public abstract List<Classes> selectAll();
}

总结

@Results:封装映射关系的父注解。Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。column 属性:查询出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被包含对象的数据类型many 属性:一对多查询固定属性
@Many:一对多查询的注解。select 属性:指定调用某个接口中的方法

多对多查询

使用注解配置Mapper

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",          // 根据查询出student表的id来作为关联条件,去查询中间表和课程表/*many、@Many 一对多查询的固定写法select属性:指定调用哪个接口中的哪个查询方法*/many = @Many(select = "com.itheima.many_to_many.CourseMapper.selectBySid"))})public abstract List<Student> selectAll();
}

总结

@Results:封装映射关系的父注解。Result[] value():定义了 Result 数组
@Result:封装映射关系的子注解。column 属性:查询出的表中字段名称property 属性:实体对象中的属性名称javaType 属性:被包含对象的数据类型many 属性:一对多查询固定属性
@Many:一对多查询的注解。select 属性:指定调用某个接口中的方法

构建sql

sql构建对象介绍

MyBatis 给我们提供了 org.apache.ibatis.jdbc.SQL 功能类,专门用于构建 SQL 语句
在这里插入图片描述

查询功能的实现

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

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

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

    method 属性:指定调用方法

新增功能的实现

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

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

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

    method 属性:指定调用方法

修改功能的实现

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

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

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

    method 属性:指定调用方法

删除功能的实现

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

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

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

    method 属性:指定调用方法

http://www.lryc.cn/news/1250.html

相关文章:

  • VHDL语言基础-时序逻辑电路-寄存器
  • 高通开发系列 - linux kernel更新msm-3.18升至msm-4.9
  • 【Tensorflow2.0】tensorflow中的Dense函数解析
  • PyTorch学习笔记:data.RandomSampler——数据随机采样
  • 设计模式(七)----创建型模式之建造者模式
  • DCGAN
  • 【速通版】吴恩达机器学习笔记Part3
  • 【leetcode】跳跃游戏
  • 论文投稿指南——中文核心期刊推荐(冶金工业 2)
  • 【GPLT 二阶题目集】L2-044 大众情人
  • SpringBoot整合(二)MyBatisPlus技术详解
  • 导入importk8s集群,添加node节点,rancher agent,Rancher Agent设置选项
  • C++11--右值引用与移动语义
  • Python SQLAlchemy入门教程
  • 你是真的“C”——操作符详解【下篇】+整形提升+算术转换
  • 文本匹配SimCSE模型代码详解以及训练自己的中文数据集
  • Biotin-PEG-FITC 生物素聚乙二醇荧光素;FITC-PEG-Biotin 科研用生物试剂
  • FISCO BCOS 搭建区块链,在SpringBoot中调用合约
  • 面试官:int和Integer有什么区别?
  • MFC常用技巧
  • C++ —— 多态
  • java agent设计开发概要
  • node.js笔记-模块化(commonJS规范),包与npm(Node Package Manager)
  • Linux 磁盘坏块修复处理(错误:read error: Input/output error)
  • API 面试四连杀:接口如何设计?安全如何保证?签名如何实现?防重如何实现?
  • 操作系统题目收录(六)
  • 2023年十款开源测试开发工具推荐!
  • MySQL慢查询分析和性能优化
  • C++学习笔记(四)
  • 【4】深度学习之Pytorch——如何使用张量处理时间序列数据集(共享自行车数据集)