Mybatis持久层框架 | Lombok搭建

Posted wei_shuo

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis持久层框架 | Lombok搭建相关的知识,希望对你有一定的参考价值。

💗wei_shuo的个人主页

💫wei_shuo的学习社区

🌐Hello World !


Lombok

Lombok项目是一个java库,它可以自动插入到编辑器和构建工具中,增强java的性能。不需要再写getter、setter或equals方法,只要有一个注解,你的类就有一个功能齐全的构建器、自动记录变量……

使用步骤:

  • IDEA中安装Lombok插件
  • 项目中导入Lombok的jar包
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
</dependencies>
  • 实体类中添加注解
import lombok.*;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User 
    private int id;
    private String name;
    private String password;


  • @Data:无参构造、get/set方法、toString、hashCode、equals
  • @AllArgsConstructor:带参构造方法
  • @NoArgsConstructor:无参构造方法
  • @ToString:toString方法
  • @EqualsAndHashCode:equals和hashCode方法
  • @Getter:get方法
  • @Setter:set方法

复杂查询环境搭建

  • 导入lombok
<dependencies>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.24</version>
    </dependency>
</dependencies>
  • 新建实体类Teacher、Student

Teacher类

package com.wei.pojo;

import lombok.Data;

@Data
public class Teacher 
    private int id;
    private String name;
    //学生关联一个老师
    private Teacher teacher;

Student类

package com.wei.pojo;

import lombok.Data;

//多对一
@Data
public class Student 
    private int id;
    private String name;
    //学生需要关联一个老师
    private Teacher teacher;

  • 建立Mapper接口

TeacherMapper接口

package com.wei.dao;

import com.wei.pojo.Teacher;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

public interface TeacherMapper 

    @Select("select * from mybatis_03.teacher where id = #tid")
    Teacher getTeacher(@Param("tid") int id);

StudentMapper接口

package com.wei.dao;

public interface StudentMapper 


  • 建立Mapper.xml文件

TeacherMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.wei.dao.TeacherMapper">

</mapper>

StudentMapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.wei.dao.StudentMapper">

</mapper>
  • 核心配置文件mybatis-config.xml绑定注册接口
<!--绑定接口-->
<mappers>
    <mapper resource="com/wei/dao/StudentMapper.xml"/>
    <mapper resource="com/wei/dao/TeacherMapper.xml"/>
</mappers>
  • 测试
public class MyTest 
    @Test
    public void test()
        //取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //获得TeacherMapper接口类
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);

        //关闭SqlSession,避免造成内存出现问题
        sqlSession.close();
    

多对一处理

环境搭建

  • Student实体类
package com.wei.pojo;

import lombok.Data;

//多对一
@Data
public class Student 
    private int id;
    private String name;
    //学生需要关联一个老师
    private Teacher teacher;

  • Teacher实体类
package com.wei.pojo;

import lombok.Data;

@Data
public class Teacher 
    private int id;
    private String name;
    //学生关联一个老师
    private Teacher teacher;

查询嵌套处理

  • Mapper.xml映射文件
<!--namespace=绑定一个对应的Dao/Mapper接口-->
<mapper namespace="com.wei.dao.StudentMapper">
<!--    - resultType:Sql语句执行的返回值-->
<!--    - parameterMap:参数类型-->

    <!--
    子查询思路:
    1.查询所有的学生信息
    2.根据查询出来的学生的tid,寻找对应的老师
    -->
    <select id="getStudent" resultMap="StudentTeacher">
        select * from mybatis_03.student;
    </select>

    <resultMap id="StudentTeacher" type="Student">
        <result property="id" column="id"/>
        <result property="name" column="name"/>
        <!--
        复杂的属性,我们需要单独处理
        对象:association
        集合:collection
        column:数据库中的字段
        property:实体类中的属性
        javaType:设置对象类型
        -->
        <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
    </resultMap>

    <select id="getTeacher" resultType="Teacher">
        select * from mybatis_03.teacher where id=#tid;
    </select>
</mapper>
  • Test测试类
    @Test
    public void testStudent() 
        //取SqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //获得StudentMapper接口类
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        //获取getStudent学生信息
        List<Student> studentList = mapper.getStudent();

        for (Student student : studentList) 
            System.out.println(student);
        

        //关闭SqlSession,避免造成内存出现问题
        sqlSession.close();
    

结果嵌套查询

  • Mapper.xml映射文件
<!--按照结果嵌套处理-->
<select id="getStudent2" resultMap="StudentTeacher2">
    select s.id sid, s.name sname,t.name tname from mybatis_03.student s , mybatis_03.teacher t where t.id=s.tid;
</select>

<resultMap id="StudentTeacher2" type="Student">
    <result property="id" column="sid"/>
    <result property="name" column="sname"/>
    <association property="teacher" javaType="Teacher">
        <result property="name" column="tname"/>
    </association>
</resultMap>
  • Test测试类
@Test
public void testStudent2() 
    //取SqlSession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获得StudentMapper接口类
    StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
    //获取getStudent学生信息
    List<Student> studentList = mapper.getStudent2();

    for (Student student : studentList) 
        System.out.println(student);
    

    //关闭SqlSession,避免造成内存出现问题
    sqlSession.close();

一对多处理

环境搭建

  • Student实体类
package com.wei.pojo;

import lombok.Data;

//多对一
@Data
public class Student 
    private int id;
    private String name;
    private int tid;

  • Teacher实体类
package com.wei.pojo;

import lombok.Data;

import java.util.List;

@Data
public class Teacher 
    private int id;
    private String name;
    //一个老师拥有多个学生
    private List<Student> students;

查询嵌套处理

  • StudentMapper类
package com.wei.dao;

import com.wei.pojo.Student;

import java.util.List;

public interface StudentMapper 
    //查询所有的学生信息,以及对应的老师的信息
    public List<Student> getStudent2();

  • TeacherMapper.xml映射文件
<select id="getTeacher2" resultMap="TeacherStudent2">
    select * from mybatis_03.teacher where id=#tid;
</select>
<!--resultMap结果映射-->
<resultMap id="TeacherStudent2" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <!--
    复杂的属性,我们需要单独处理
    对象:association
    集合:collection
    javaType:指定的属性类型
    集合中的泛型信息,使用ofType获取
   -->
    <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"/>
</resultMap>

<select id="getStudentByTeacherId" resultType="Student">
    select * from mybatis_03.student where tid=#tid;
</select>
  • 测试
@Test
public void test2() 
    //获取sqlSession对象
    SqlSession sqlSession = MybatisUtils.getSqlSession();
    //获取TeacherMapper接口类
    TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

    Teacher teacher = mapper.getTeacher2(1);
    System.out.println(teacher);

    //关闭SqlSession,避免造成内存出现问题
    sqlSession.close();

结果嵌套查询

  • StudentMapper类
package com.wei.dao;

import com.wei.pojo.Student;

import java.util.List;

public interface StudentMapper 
    //查询所有的学生信息,以及对应的老师的信息
    public List<Student> getStudent();

  • TeacherMapper.xml映射文件
<!--按照结果嵌套查询-->
<select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid, s.name sname, t.name tname, t.id tid from mybatis_03.student s,mybatis_03.teacher t where t.id = s.tid and t.id=#tid;
</select>
<!--resultMap结果映射-->
<!--
column:数据库中的字段
property:实体类中的属性
-->
<resultMap id="TeacherStudent" type="Teacher">
    <result property="id" column="tid"/>
    <result property="name" column="tname"/>
    <!--
    复杂的属性,我们需要单独处理
    对象:association
    集合:collection
    javaType:指定的属性类型
    集合中的泛型信息,使用ofType获取
    -->
    <collection property="students" ofType="Student">
        <result property="id" column="sid"/>
        <result property="name" column="sname"/>
        <result property="tid" column="tid"/>
    </collection>
</resultMap>
  • 测试
public class MyTest 
    @Test
    public void test() 
        //获取sqlSession对象
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        //获取TeacherMapper接口类
        TeacherMapper mapper = sqlSession.getMapper(TeacherMapper.class);

        Teacher teacher = mapper.getTeacher(1);
        System.out.println(teacher);

        //关闭SqlSession,避免造成内存出现问题
        sqlSession.close();
    

总结:

collection & association区别:

  • 关联 - association [多对一]
  • 集合 - collection [一对多]

javatype & oftype区别:

  • javatype:指定实体类中属性的类型
  • oftype:指定映射到List或者集合的pojo类型,泛型中的约束类型

🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——点赞👍收藏⭐️评论📝冲冲冲🤞


以上是关于Mybatis持久层框架 | Lombok搭建的主要内容,如果未能解决你的问题,请参考以下文章

搭建SpringCloud微服务框架:数据库持久层-SpringDataJPA

MyBatisJava 持久层框架入门与实践 | 学习笔记

MyBatis第一课 MyBatis的框架的搭建和使用

Mybatis环境搭建

Mybatis

MyBatis的概述