• Mybatis注解开发


    1.注解的概述

    常用注解:

    @Select(“查询的 SQL 语句”):执行查询操作注解

    @Insert(“查询的 SQL 语句”):执行新增操作注解

    @Update(“查询的 SQL 语句”):执行修改操作注解

    @Delete(“查询的 SQL 语句”):执行删除操作注解

    2.注解实现查询的操作

    1.创建接口和查询方法

    2.在核心配置文件中配置映射关系

    3.编写测试类

    例:

    bean.Student:

    1. package Mybatis4.bean;
    2. public class Student {
    3. private Integer id;
    4. private String name;
    5. private Integer age;
    6. public Student() {
    7. }
    8. public Student(Integer id, String name, Integer age) {
    9. this.id = id;
    10. this.name = name;
    11. this.age = age;
    12. }
    13. public Integer getId() {
    14. return id;
    15. }
    16. public void setId(Integer id) {
    17. this.id = id;
    18. }
    19. public String getName() {
    20. return name;
    21. }
    22. public void setName(String name) {
    23. this.name = name;
    24. }
    25. public Integer getAge() {
    26. return age;
    27. }
    28. public void setAge(Integer age) {
    29. this.age = age;
    30. }
    31. @Override
    32. public String toString() {
    33. return "Student{" +
    34. "id=" + id +
    35. ", name='" + name + '\'' +
    36. ", age=" + age +
    37. '}';
    38. }
    39. }

    Mapper接口 :

    1. package Mybatis4.mapper;
    2. import Mybatis4.bean.Student;
    3. import org.apache.ibatis.annotations.Select;
    4. import java.util.List;
    5. public interface StudentMapper {
    6. //查询全部
    7. @Select("SELECT * FROM student")
    8. public abstract List selectAll();
    9. }

    MyBatisConfig.xml :

    1. <mappers>
    2. <package name="Mybatis4.mapper"/>
    3. mappers>

    测试类:

    1. package Mybatis4.test;
    2. import Mybatis4.bean.Student;
    3. import Mybatis4.mapper.StudentMapper;
    4. import org.apache.ibatis.io.Resources;
    5. import org.apache.ibatis.session.SqlSession;
    6. import org.apache.ibatis.session.SqlSessionFactory;
    7. import org.apache.ibatis.session.SqlSessionFactoryBuilder;
    8. import org.junit.Test;
    9. import java.io.InputStream;
    10. import java.util.List;
    11. public class Test01 {
    12. @Test
    13. public void selectAll() throws Exception{
    14. //1.加载核心配置文件
    15. InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
    16. //2.获取SqlSession工厂对象
    17. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    18. //3.通过工厂对象获取SqlSession
    19. SqlSession sqlSession = sqlSessionFactory.openSession(true);
    20. //4.获取StudentMapper接口的实现类对象
    21. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    22. //5.调用实现类的方法,接收结果
    23. List list = mapper.selectAll();
    24. //6.处理结果
    25. for (Student student : list) {
    26. System.out.println(student);
    27. }
    28. //7.释放资源
    29. sqlSession.close();
    30. is.close();
    31. }
    32. }

    3.注解实现新增的操作 

    1.创建新增方法

    2.编写测试类

    Mapper接口:

    1. //新增操作: sql的参数与之前的写法一致,从insert方法的参数中获取对应属性值
    2. @Insert("INSERT INTO student VALUES (#{id},#{name},#{age})")
    3. public abstract Integer insert(Student stu);

    测试类:

    1. @Test
    2. public void insert() throws Exception{
    3. //1.加载核心配置文件
    4. InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
    5. //2.获取SqlSession工厂对象
    6. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    7. //3.通过工厂对象获取SqlSession
    8. SqlSession sqlSession = sqlSessionFactory.openSession(true);
    9. //4.获取StudentMapper接口的实现类对象
    10. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    11. //5.调用实现类的方法,接收结果
    12. Student stu = new Student(4,"赵六",26);
    13. Integer result = mapper.insert(stu);
    14. //6.处理结果
    15. System.out.println(result);
    16. //7.释放资源
    17. sqlSession.close();
    18. is.close();
    19. }

     4.注解实现修改的操作

    1.创建修改方法

    2.编写测试类

    Mapper接口:

    1. //修改操作
    2. @Update("UPDATE student SET name=#{name},age=#{age} WHERE id=#{id}")
    3. public abstract Integer update(Student stu);

    测试类:

    1. @Test
    2. public void update() throws Exception{
    3. InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    5. SqlSession sqlSession = sqlSessionFactory.openSession(true);
    6. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    7. Student stu = new Student(4,"赵六",36);
    8. Integer result = mapper.update(stu);
    9. System.out.println(result);
    10. sqlSession.close();
    11. is.close();
    12. }

    5.注解实现删除操作

    1.创建删除方法

    2.编写测试类

    Mapper接口:

    1. //删除操作
    2. @Delete("DELETE FROM student WHERE id=#{id}")
    3. public abstract Integer delete(Integer id);

    测试类:

    1. @Test
    2. public void delete() throws Exception{
    3. InputStream is = Resources.getResourceAsStream("MyBatisConfig.xml");
    4. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
    5. SqlSession sqlSession = sqlSessionFactory.openSession(true);
    6. StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    7. Integer result = mapper.delete(4);
    8. System.out.println(result);
    9. sqlSession.close();
    10. is.close();
    11. }

    注:映射配置关系

    1. <mappers>
    2. <package name="接口所在包"/>
    3. mappers>
  • 相关阅读:
    在由buildroot制作出来的根文件系统中移植sudo命令
    Docker Compose使用教程
    SpringBoot整合Hive(开启Kerberos认证)作三方数据源
    全球首个“AI程序员”Deven诞生,真的能替代人类程序员吗?
    php的数组问题,一个二维数组怎么取出里面的部分元素组成一个新数组
    git容易出问题的命令
    互联网进入存量博弈时代,小程序技术创造移动应用新机遇
    Spring Cloud - 手写 Gateway 源码,实现自定义局部 FilterFactory
    Apache Ignite 作为 MySql的加速层
    【Unity】万人同屏高级篇, BRG & Jobs实战应用, 海量物体同屏
  • 原文地址:https://blog.csdn.net/weixin_61611746/article/details/132070361