💗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>
- 实体类中添加注解
importlombok.*;@Data@AllArgsConstructor@NoArgsConstructorpublicclassUser{privateint id;privateString name;privateString 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类
packagecom.wei.pojo;importlombok.Data;@DatapublicclassTeacher{privateint id;privateString name;//学生关联一个老师privateTeacher teacher;}
Student类
packagecom.wei.pojo;importlombok.Data;//多对一@DatapublicclassStudent{privateint id;privateString name;//学生需要关联一个老师privateTeacher teacher;}
- 建立Mapper接口
TeacherMapper接口
packagecom.wei.dao;importcom.wei.pojo.Teacher;importorg.apache.ibatis.annotations.Param;importorg.apache.ibatis.annotations.Select;publicinterfaceTeacherMapper{@Select("select * from mybatis_03.teacher where id = #{tid}")TeachergetTeacher(@Param("tid")int id);}
StudentMapper接口
packagecom.wei.dao;publicinterfaceStudentMapper{}
- 建立Mapper.xml文件
TeacherMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace=绑定一个对应的Dao/Mapper接口--><mappernamespace="com.wei.dao.TeacherMapper"></mapper>
StudentMapper.xml
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPEmapperPUBLIC"-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd"><!--namespace=绑定一个对应的Dao/Mapper接口--><mappernamespace="com.wei.dao.StudentMapper"></mapper>
- 核心配置文件mybatis-config.xml绑定注册接口
<!--绑定接口--><mappers><mapperresource="com/wei/dao/StudentMapper.xml"/><mapperresource="com/wei/dao/TeacherMapper.xml"/></mappers>
- 测试
publicclassMyTest{@Testpublicvoidtest(){//取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实体类
packagecom.wei.pojo;importlombok.Data;//多对一@DatapublicclassStudent{privateint id;privateString name;//学生需要关联一个老师privateTeacher teacher;}
- Teacher实体类
packagecom.wei.pojo;importlombok.Data;@DatapublicclassTeacher{privateint id;privateString name;//学生关联一个老师privateTeacher teacher;}
查询嵌套处理
- Mapper.xml映射文件
<!--namespace=绑定一个对应的Dao/Mapper接口--><mappernamespace="com.wei.dao.StudentMapper"><!-- - resultType:Sql语句执行的返回值--><!-- - parameterMap:参数类型--><!-- 子查询思路: 1.查询所有的学生信息 2.根据查询出来的学生的tid,寻找对应的老师 --><selectid="getStudent"resultMap="StudentTeacher"> select * from mybatis_03.student; </select><resultMapid="StudentTeacher"type="Student"><resultproperty="id"column="id"/><resultproperty="name"column="name"/><!-- 复杂的属性,我们需要单独处理 对象:association 集合:collection column:数据库中的字段 property:实体类中的属性 javaType:设置对象类型 --><associationproperty="teacher"column="tid"javaType="Teacher"select="getTeacher"/></resultMap><selectid="getTeacher"resultType="Teacher"> select * from mybatis_03.teacher where id=#{tid}; </select></mapper>
- Test测试类
@TestpublicvoidtestStudent(){//取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映射文件
<!--按照结果嵌套处理--><selectid="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><resultMapid="StudentTeacher2"type="Student"><resultproperty="id"column="sid"/><resultproperty="name"column="sname"/><associationproperty="teacher"javaType="Teacher"><resultproperty="name"column="tname"/></association></resultMap>
- Test测试类
@TestpublicvoidtestStudent2(){//取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实体类
packagecom.wei.pojo;importlombok.Data;//多对一@DatapublicclassStudent{privateint id;privateString name;privateint tid;}
- Teacher实体类
packagecom.wei.pojo;importlombok.Data;importjava.util.List;@DatapublicclassTeacher{privateint id;privateString name;//一个老师拥有多个学生privateList<Student> students;}
查询嵌套处理
- StudentMapper类
packagecom.wei.dao;importcom.wei.pojo.Student;importjava.util.List;publicinterfaceStudentMapper{//查询所有的学生信息,以及对应的老师的信息publicList<Student>getStudent2();}
- TeacherMapper.xml映射文件
<selectid="getTeacher2"resultMap="TeacherStudent2"> select * from mybatis_03.teacher where id=#{tid}; </select><!--resultMap结果映射--><resultMapid="TeacherStudent2"type="Teacher"><resultproperty="id"column="tid"/><resultproperty="name"column="tname"/><!-- 复杂的属性,我们需要单独处理 对象:association 集合:collection javaType:指定的属性类型 集合中的泛型信息,使用ofType获取 --><collectionproperty="students"javaType="ArrayList"ofType="Student"select="getStudentByTeacherId"column="id"/></resultMap><selectid="getStudentByTeacherId"resultType="Student"> select * from mybatis_03.student where tid=#{tid}; </select>
- 测试
@Testpublicvoidtest2(){//获取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类
packagecom.wei.dao;importcom.wei.pojo.Student;importjava.util.List;publicinterfaceStudentMapper{//查询所有的学生信息,以及对应的老师的信息publicList<Student>getStudent();}
- TeacherMapper.xml映射文件
<!--按照结果嵌套查询--><selectid="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:实体类中的属性 --><resultMapid="TeacherStudent"type="Teacher"><resultproperty="id"column="tid"/><resultproperty="name"column="tname"/><!-- 复杂的属性,我们需要单独处理 对象:association 集合:collection javaType:指定的属性类型 集合中的泛型信息,使用ofType获取 --><collectionproperty="students"ofType="Student"><resultproperty="id"column="sid"/><resultproperty="name"column="sname"/><resultproperty="tid"column="tid"/></collection></resultMap>
- 测试
publicclassMyTest{@Testpublicvoidtest(){//获取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类型,泛型中的约束类型
🌼 结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
👍
收藏
⭐️
评论
📝
冲冲冲
🤞
版权归原作者 wei_shuo 所有, 如有侵权,请联系我们删除。