wei_shuo的个人主页
wei_shuo的学习社区
Hello World !
Lombok
Lombok项目是一个java库,它可以自动插入到编辑器和构建工具中,增强java的性能。不需要再写getter、setter或equals方法,只要有一个注解,你的类就有一个功能齐全的构建器、自动记录变量……
使用步骤:
• IDEA中安装Lombok插件
• 项目中导入Lombok的jar包
org.projectlombok
lombok
1.18.24
• 实体类中添加注解
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
org.projectlombok
lombok
1.18.24
• 新建实体类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
StudentMapper.xml
• 核心配置文件mybatis-config.xml绑定注册接口
• 测试
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映射文件
select * from mybatis_03.student;
select * from mybatis_03.teacher where id=#{tid};
• Test测试类
@Test
public void testStudent() {
//取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//获得StudentMapper接口类
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//获取getStudent学生信息
List studentList = mapper.getStudent();
for (Student student : studentList) {
System.out.println(student);
}
//关闭SqlSession,避免造成内存出现问题
sqlSession.close();
}
}
结果嵌套查询
• Mapper.xml映射文件
select s.id sid, s.name sname,t.name tname from mybatis_03.student s , mybatis_03.teacher t where t.id=s.tid;
• Test测试类
@Test
public void testStudent2() {
//取SqlSession对象
SqlSession sqlSession = MybatisUtils.getSqlSession();
//获得StudentMapper接口类
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
//获取getStudent学生信息
List studentList = mapper.getStudent2();
for (Student student : studentList) {
System.out.println(student);
}
//关闭SqlSession,避免造成内存出现问题
sqlSession.close();
}
一对多处理
环境搭建
• Student实体类
package com.wei.pojo;
import lombok.Data;
//多对一
@Data
public cla服务器托管网ss 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 students;
}
查询嵌套处理
• StudentMapper类
package com.wei.dao;
import com.wei.pojo.Student;
import java.util.List;
public interface StudentMapper {
//查询所有的学生信息,以及对应的老师的信息
public List getStudent2();
}
• TeacherMapper.xml映射文件
select * from mybatis_03.teacher where id=#{tid};
select * from mybatis_03.student where tid=#{tid};
• 测试
@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 getStudent();
}
• TeacherMapper.xml映射文件
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};
• 测试
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类型,泛型中的约束类型
结语:创作不易,如果觉得博主的文章赏心悦目,还请——
点赞
收藏
⭐️评论
冲冲冲
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.fwqtg.net