Mybatis 注解

古城微笑少年丶 2022-12-05 04:56 222阅读 0赞

1.1 基本使用

1.1.1 常用注解










































注解 描述
@Insert 实现新增
@Update 实现更新
@Delete 实现删除
@Select 实现查询
@Result 实现结果集封装
@Results 可以与 @Result 一起使用,封装多个结果集
@One 实现一对一结果集封装
@Many 实现一对多结果集封装

1.1.2 MyBatis 增删改查

☞ 实体类

  1. public class Student {
  2. private Long sId;
  3. private String sName;
  4. private Long sAge;
  5. private String sSex;
  6. // set and get
  7. }

☞ DAO

  1. /** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/9 * @description Mybatis 注解 */
  2. public interface StudentDao {
  3. @Select("select * from student")
  4. public List<Student> findAll();
  5. @Select("select * from where s_id = #{id} ")
  6. public Student findOne(Long id);
  7. @Insert("insert into student values (#{sId}, #{sName}), #{sAge}, #{sSex}")
  8. public int insert(Student student);
  9. @Update("update from student set s_id = #{sId}, s_name = #{sName}, s_age = #{sAge}, s_sex = #{sSex}")
  10. public int update(Student student);
  11. @Delete("delete from student where s_id = #{id} ")
  12. public int delete(Long id);
  13. }

☞ 配置文件

  1. <typeAliases>
  2. <package name="com.software.mybatis.entity"/>
  3. </typeAliases>
  4. <environments default="development">
  5. <environment id="development">
  6. <transactionManager type="JDBC"/>
  7. <dataSource type="POOLED">
  8. <property name="driver" value="com.mysql.jdbc.Driver"/>
  9. <property name="url" value="jdbc:mysql://localhost:3306/db"/>
  10. <property name="username" value="root"/>
  11. <property name="password" value="root"/>
  12. </dataSource>
  13. </environment>
  14. </environments>
  15. <mappers>
  16. <!-- 指定接口所在的包 -->
  17. <package name="com.software.mybatis.dao"/>
  18. <!-- 指定接口 -->
  19. <mapper class="com.software.mybatis.dao.StudentDao"/>
  20. </mappers>

☞ 测试

  1. /** * Created with IntelliJ IDEA. * * @author Demo_Null * @date 2020/9/9 * @description */
  2. public class MybatisDemo {
  3. private StudentDao studentDao;
  4. @Before
  5. public void before() throws IOException {
  6. // 加载核心配置文件
  7. InputStream resourceAsStream = Resources.getResourceAsStream("mybatis.xml");
  8. // 获得 sqlSession 工厂对象
  9. SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
  10. // 获得 sqlSession 对象
  11. SqlSession sqlSession = sqlSessionFactory.openSession();
  12. // 获取 mapper
  13. studentDao = sqlSession.getMapper(StudentDao.class);
  14. }
  15. @Test
  16. public void TestA() throws IOException {
  17. List<Student> studentList = studentDao.findAll();
  18. System.out.println(studentList);
  19. }
  20. }

在这里插入图片描述
  我们可以看到,明明结果已经查询出来了,为什么打印出来却是空的。这个是因为属性名和列名不一致造成的,类似于我们这种 sId ☞ s_id 可以打开驼峰之自动转换。如果二者之间没有任何联系就需要使用 @Results 一一映射。

  1. <settings>
  2. <setting name="mapUnderscoreToCamelCase" value="true"/>
  3. </settings>
  4. @Select("select * from student")
  5. @Results(value = {
  6. @Result(property = "sId", column = "s_id"),
  7. @Result(property = "sName", column = "s_name"),
  8. @Result(property = "sAge", column = "s_age"),
  9. @Result(property = "sSex", column = "s_sex")
  10. })
  11. public List<Student> findAll();

在这里插入图片描述

1.2 复杂映射

1.2.1 注解详解


























注解 说明
@Results 代替的是标签该注解中可以使用单个@Result注解,也可以使用@Result集合
使用格式:@Results({@Result(),@Result()})或@Results(@Result())
@Result 代替了标签和标签
@Result中属性介绍:
column:数据库的列名
property:需要装配的属性名
one:需要使用的@One 注解(@Result(one=@One)()))
many:需要使用的@Many 注解(@Result(many=@many)()))
@One(一对一) 代替了标签,是多表查询的关键,在注解中用来指定子查询返回单一对象。
@One注解属性介绍:
select:指定用来多表查询的sqlmapper使用格式:@Result(column=””,property=””.one=@One(select=”))
@Many(一对多) 代替了标签,是是多表查询的关键,在注解中用来指定子查询返回对象集合。
使用格式:@Result(property=””.column=”,many=@Many(select=”))

1.2.2 一对一

☞ 实体类

  1. public class Student {
  2. private Long sId;
  3. private String sName;
  4. private Long sAge;
  5. private String sSex;
  6. private Class class;
  7. // set and get
  8. }
  9. public class Class {
  10. private Long cId;
  11. private String cName;
  12. private String cAddr;
  13. // set and get
  14. }

☞ DAO

  1. public interface ClassDao {
  2. @Select("select * from class where c_id = #{id} ")
  3. public Class findById(Long id);
  4. }
  5. public interface StudentDao {
  6. @Select("select * from student")
  7. @Results({
  8. @Result(property = "sId", column = "s_id"),
  9. @Result(property = "sName", column = "s_name"),
  10. @Result(property = "sAge", column = "s_age"),
  11. @Result(property = "sSex", column = "s_sex"),
  12. // 类似于先查询出 student 然后拿 c_id 再去查 class
  13. @Result(property = "class", column = "c_id", javaType = Class.class,
  14. one = @One(select = "com.software.mybatis.dao.ClassDao.findById"))
  15. })
  16. public List<Student> findAll();
  17. }

1.2.3 一对多

☞ 实体类

  1. public class Student {
  2. private Long sId;
  3. private String sName;
  4. private Long sAge;
  5. private String sSex;
  6. private Long cId;
  7. // set and get
  8. }
  9. public class Class {
  10. private Long cId;
  11. private String cName;
  12. private String cAddr;
  13. private List<Student> students;
  14. // set and get
  15. }

☞ DAO

  1. public interface StudentDao {
  2. @Select("select * from where c_id = #{id} ")
  3. public List<Student> findByCid(Long id);
  4. }
  5. public interface ClassDao {
  6. @Select("select * from class")
  7. @Results({
  8. @Result(property = "cId", column = "c_id"),
  9. @Result(property = "cName", column = "c_name"),
  10. @Result(property = "cAddr", column = "c_addr"),
  11. // 类似于先查询 class 然后使用 c_id 查询 student
  12. @Result(property = "students", column = "c_id", javaType = List.class,
  13. many = @Many(select = "com.software.mybatis.dao.StudentDao.findByCid")),
  14. })
  15. public List<Class> findAll();
  16. }

1.2.4 多对多

☞ 实体类

  1. public class Course {
  2. private Integer cId;
  3. private String cName;
  4. private List<Student> students;
  5. // set and get
  6. }
  7. public class Student {
  8. private Integer sId;
  9. private String sName;
  10. private Long sAge;
  11. private String sSex;
  12. private List<Course> courses;
  13. // set and get
  14. }

☞ DAO

  1. public interface CourseDao {
  2. @Select("select * from course c, s_c sc where c.c_id = sc.c_id and sc.s_id = #{id} ")
  3. public List<Course> findBySid(Long id);
  4. }
  5. public interface StudentDao {
  6. @Select("select * from student")
  7. @Results({
  8. @Result(property = "sId", column = "s_id"),
  9. @Result(property = "sName", column = "s_name"),
  10. @Result(property = "sAge", column = "s_age"),
  11. @Result(property = "sSex", column = "s_sex"),
  12. // 类似于先查询 student 然后连接查询 course 和 s_c 表条件为 c.c_id = sc.c_id and s_id = sc.s_id
  13. @Result(property = "Courses", column = "s_id", javaType = List.class,
  14. many = @Many(select = "com.software.mybatis.dao.CoursesDao.findBySid"))
  15. })
  16. public List<Student> findAll();
  17. }

发表评论

表情:
评论列表 (有 0 条评论,222人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Mybatis 注解开发

    这几年来注解开发越来越流行,Mybatis 也可以使用注解开发方式,这样我们就可以减少编写 Mapper 映射 文件了。本次我们先围绕一些基本的 CRUD 来学习,再学习复杂映