标签:xsl 使用步骤 问题 als 阅读 @Value ntb 小明 使用
1 <dependencies> 2 <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok --> 3 <dependency> 4 <groupId>org.projectlombok</groupId> 5 <artifactId>lombok</artifactId> 6 <version>1.18.10</version> 7 </dependency> 8 </dependencies>
1 @Getter and @Setter 2 @FieldNameConstants 3 @ToString 4 @EqualsAndHashCode 5 @AllArgsConstructor, @RequiredArgsConstructor and @NoArgsConstructor 6 @Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger, @CustomLog 7 @Data 8 @Builder 9 @SuperBuilder 10 @Singular 11 @Delegate 12 @Value 13 @Accessors 14 @Wither 15 @With 16 @SneakyThrows 17 @val 18 @var 19 experimental @var 20 @UtilityClass 21 Lombok config system 22 Code inspections 23 Refactoring actions (lombok and delombok)
只需要知道:
1 import lombok.Data; 2 3 //实体类 4 @Data 5 public class User { 6 private int id; //id 7 private String name; //姓名 8 private String password; //密码 9 10 11 }
1 CREATE TABLE `teacher` ( 2 `id` INT(10) NOT NULL, 3 `name` VARCHAR(30) DEFAULT NULL, 4 PRIMARY KEY (`id`) 5 ) ENGINE=INNODB DEFAULT CHARSET=utf8; 6 7 INSERT INTO teacher(`id`, `name`) VALUES (1, ‘秦老师‘); 8 9 CREATE TABLE `student` ( 10 `id` INT(10) NOT NULL, 11 `name` VARCHAR(30) DEFAULT NULL, 12 `tid` INT(10) DEFAULT NULL, 13 PRIMARY KEY (`id`), 14 KEY `fktid` (`tid`), 15 CONSTRAINT `fktid` FOREIGN KEY (`tid`) REFERENCES `teacher` (`id`) 16 ) ENGINE=INNODB DEFAULT CHARSET=utf8; 17 18 19 INSERT INTO `student` (`id`, `name`, `tid`) VALUES (‘1‘, ‘小明‘, ‘1‘); 20 INSERT INTO `student` (`id`, `name`, `tid`) VALUES (‘2‘, ‘小红‘, ‘1‘); 21 INSERT INTO `student` (`id`, `name`, `tid`) VALUES (‘3‘, ‘小张‘, ‘1‘); 22 INSERT INTO `student` (`id`, `name`, `tid`) VALUES (‘4‘, ‘小李‘, ‘1‘); 23 INSERT INTO `student` (`id`, `name`, `tid`) VALUES (‘5‘, ‘小王‘, ‘1‘);
1 import lombok.Data; 2 3 @Data 4 public class Student { 5 6 private int id; 7 private String name; 8 9 //学生需要关联一个老师 10 private Teacher teacher; 11 }
1 import lombok.Data; 2 3 @Data 4 public class Teacher { 5 6 private int id; 7 private String name; 8 }
1 select s.id as ‘学生id‘,s.name as ‘学生姓名‘,t.id as ‘老师id‘,t.name as ‘老师姓名‘ from student s ,teacher t where s.tid = t.id;
1 List<Student> getStudent();
1 <!--需要查询所有学生信息以及对应的老师信息--> 2 <resultMap id="StudentTeacher" type="Student"> 3 <result property="id" column="id"></result> 4 <result property="name" column="name"></result> 5 6 <!--复杂的属性,我们需要单独处理, 对象使用association,集合使用collection--> 7 <!--property属性名 javaType属性类型 column表中的列名--> 8 <association property="teacher" column="tid" javaType="Teacher" select="getTeacher"></association> 9 </resultMap> 10 11 <select id="getStudent" resultMap="StudentTeacher"> 12 select * from mybatis.student; 13 </select> 14 15 <select id="getTeacher" parameterType="_int" resultType="Teacher"> 16 select * from mybatis.teacher where id=#{teacherId}; 17 </select>
1 @Test 2 public void getStudentTest() { 3 SqlSession session = MybatisUtils.getSession(); 4 5 StudentMapper mapper = session.getMapper(StudentMapper.class); 6 7 List<Student> studentList = mapper.getStudent(); 8 9 for (Student student : studentList) { 10 System.out.println(student); 11 } 12 13 session.close(); 14 }
1 List<Student> getStudent2();
1 <!--方式二:连表查询--> 2 <select id="getStudent2" resultMap="StudentTeacher2"> 3 select s.id as sid,s.name as sname,t.id as tid,t.name as tname 4 from student s ,teacher t 5 where s.tid = t.id; 6 </select> 7 8 <resultMap id="StudentTeacher2" type="Student"> 9 <result property="id" column="sid"></result> 10 <result property="name" column="sname"></result> 11 <association property="teacher" javaType="Teacher"> 12 <result property="id" column="tid"></result> 13 <result property="name" column="tname"></result> 14 </association> 15 </resultMap>
1 @Test 2 public void getStudent2Test() { 3 SqlSession session = MybatisUtils.getSession(); 4 5 StudentMapper mapper = session.getMapper(StudentMapper.class); 6 7 List<Student> studentList = mapper.getStudent2(); 8 9 for (Student student : studentList) { 10 System.out.println(student); 11 } 12 13 session.close(); 14 }
1 import lombok.Data; 2 3 @Data 4 public class Student { 5 6 private int id; 7 private String name; 8 private int tid; 9 }
1 import lombok.Data; 2 3 import java.util.List; 4 5 @Data 6 public class Teacher { 7 8 private int id; 9 private String name; 10 11 //一个老师拥有多个学生 12 private List<Student> students; 13 }
1 select t.id as tid, t.name as tname,s.id as sid, s.name as sname 2 from student s,teacher t 3 where s.tid = t.id;
1 Teacher getTeacher2(@Param("tid") int id);
1 <select id="getTeacher2" resultMap="TeacherStudent2"> 2 select id , name from mybatis.teacher where id = #{tid} 3 </select> 4 5 <resultMap id="TeacherStudent2" type="Teacher"> 6 <result property="id" column="id"></result> 7 <result property="name" column="name"></result> 8 <collection property="students" javaType="ArrayList" ofType="Student" select="getStudentByTeacherId" column="id"></collection> 9 </resultMap> 10 11 <select id="getStudentByTeacherId" resultType="Student"> 12 select * from mybatis.student where tid = #{tid}; 13 </select>
1 @Test 2 public void getTeacherTest2() { 3 SqlSession session = MybatisUtils.getSession(); 4 5 TeacherMapper mapper = session.getMapper(TeacherMapper.class); 6 7 Teacher teacher = mapper.getTeacher2(1); 8 9 System.out.println(teacher); 10 11 session.close(); 12 }
1 //根据老师id获得老师信息并获得对应的多个学生信息 2 Teacher getTeacher(@Param("tid") int id);
每次只获得一条数据所以 property="students" ofType="Student" ,每次拼接一条数据最后一次性返回
1 <!--联表查询方式--> 2 <select id="getTeacher" resultMap="TeacherStudent"> 3 select t.id as tid, t.name as tname,s.id as sid, s.name as sname 4 from student s,teacher t 5 where s.tid = t.id and t.id = #{tid}; 6 </select> 7 8 <resultMap id="TeacherStudent" type="Teacher"> 9 <result property="id" column="tid"></result> 10 <result property="name" column="tname"></result> 11 <!--集合的话,使用collection! 12 JavaType和ofType都是用来指定对象类型的 13 JavaType是用来指定pojo中属性的类型 14 ofType指定的是映射到list集合属性中pojo的类型。--> 15 <collection property="students" ofType="Student"> 16 <result property="id" column="sid"></result> 17 <result property="name" column="sname"></result> 18 <result property="tid" column="tid"></result> 19 </collection> 20 </resultMap>
1 @Test 2 public void getTeacherTest() { 3 SqlSession session = MybatisUtils.getSession(); 4 5 TeacherMapper mapper = session.getMapper(TeacherMapper.class); 6 7 Teacher teacher = mapper.getTeacher(1); 8 9 System.out.println(teacher); 10 11 session.close(); 12 }
标签:xsl 使用步骤 问题 als 阅读 @Value ntb 小明 使用
原文地址:https://www.cnblogs.com/zhihaospace/p/12301067.html