介面注解實作CRUD:
第一步:直接在介面里面寫注解和方法;(此步驟省去了在mapper.xml映射檔案)這也是注解比較簡便的原因
第二步:在mybatis核心組態檔中系結介面(注意和系結mapper.xml的區別,系結介面用的是class!!!)
第三步:寫test方法
在使用增刪改查,注解時候引數需要加@Param
使用@Param時要注意:
1.一個引數時可以不用@Param
2.多個引數時必須使用@Param
3.如果引數是 JavaBean , 則不能使用@Param,
4.不使用@Param注解時,引數只能有一個,并且是Javabean,
Q:為啥洗掉、添加、更新一條sql陳述句的時候,用的是int型別方法?
A:主要是看從資料庫拿到資料后在dao層怎么處理(如果想讓)
Q:在添加一條用戶資訊時,出現了識別不到物體類引數的問題(no parameters)
A: 使用@Param注解,傳參型別原來寫的是user物體型別別,但是里面包含有三個引數(id、name、password),所以要使用@Param來挨個宣告,
lombok插件,里面包括了getter、setter、toString等等一些常用方法…引入此插件可以比較方便,直接在物體類頭上@Data @AllArgsConstructor @NoArgsConstructor
多對一的處理:
1、撰寫物體類,注意要在Student物體類中加入變數Teacher
@Data
public class Teacher {
private int id;
private String name;
public Teacher(int id, String name) {
this.id = id;
this.name = name;
}
}
@Data
public class Student {
private int id;
private String name;
private Teacher teacher;
public Student(int id, String name, Teacher teacher, int tid) {
this.id = id;
this.name = name;
this.teacher = teacher;
}
}
2、寫學生的mapper介面
public interface studentMapper {
List<Student> getStudent();
}
3、寫mapper介面對應的xml檔案
配置映射檔案有兩種resultType和resultMap,resultType適用于簡單的欄位名查詢;resultMap適用于對復雜陳述句的映射,此處應該用resultMap
1. 做一個結果集映射:StudentTeacher
2. StudentTeacher結果集的型別為 Student
3. 學生中老師的屬性為teacher,對應資料庫中為tid,
多個 [1,…)學生關聯一個老師=> 一對一,一對多
4. 查看官網找到:association – 一個復雜型別的關聯;使用它來處理關聯查詢
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.li.dao.studentMapper">
<select id="getStudent" resultMap="TeacherStudent">
select * from student
</select>
<resultMap id="TeacherStudent" type="Student">
<result property="id" column="id"/>
<result property="name" column="name"/>
<!--association關聯屬性 property屬性名 javaType屬性型別 column在多的一方的表中的列名-->
<association property="teacher" column="tid" javaType="Teacher" select="getTeacher"/>
</resultMap>
<select id="getTeacher" resultType="teacher">
select * from teacher where id=#{id}
</select>
</mapper>
4、測驗類
public class test01 {
@Test
public void getStudents(){
SqlSession session = mybatisUtils.getSession();
studentMapper mapper = session.getMapper(studentMapper.class);
List<Student> students = mapper.getStudent();
for(Student student:students){
System.out.println(
"學生:"+student.getName()+"老師:"+student.getTeacher().getName()
);
}
}
}
寫完這些不出意外的又出現了bug
經過很多次的檢查,本來以為是mybatis-config核心組態檔出現了錯誤(mapper的配置),結果不是,后來往后面翻了翻bug發現貌似是說決議不了Student和Teacher類,百度了一下大致意思是說沒有在核心
組態檔里面配置這兩個物體類導致在映射里面找不到,
解決辦法:在核心組態檔里面加typeAlias
<typeAliases>
<typeAlias type="com.li.pojo.Student" alias="Student"/>
<typeAlias type="com.li.pojo.Teacher" alias="Teacher"/>
</typeAliases>
另外一種是在寫mapper.xml的時候將學生和老師聯合起來查詢
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.li.dao.studentMapper">
<select id="getStudent" resultMap="TeacherStudent">
select s.id sid,s.name sname,t.name tname
from Student s,Teacher t
where s.tid = t.id
</select>
<resultMap id="TeacherStudent" type="Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>
</mapper>
一對多
1、物體類中teacher中應該有學生集合
2、寫mapper映射
<mapper namespace="com.li.dao.teacherMapper">
<select id="getTeacher" resultMap="TeacherStudent">
select * from teacher where id=#{id}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<result property="id" column="id"/>
<collection property="students" ofType="Student" javaType="ArrayList" column="id" select="getStudentById"/>
</resultMap>
<select id="getStudentById" resultType="Student">
select * from student where tid=#{id};
</select>
</mapper>
另一種寫法:
<mapper namespace="com.li.dao.teacherMapper">
<select id="getTeacher" resultMap="TeacherStudent" >
select t.id tid,t.name tname,s.id sid,s.name sname
from teacher t ,student s
where s.tid = t.id and t.id = #{id}
</select>
<resultMap id="TeacherStudent" type="Teacher">
<id property="id" column="tid"/>
<result property="name" column="tname"/>
<collection property="students" ofType="Student">
<result property="id" column="sid"/>
<result property="name" column="sname"/>
<result property="tid" column="tid"/>
</collection>
</resultMap>
</mapper>
collection和association的區別:collection用于集合,association用于物件
ofType也是用于回傳結果為集合型別的類
練習一對多的時候出來一個很坑的bug,識別不了我的mapper介面,因為我命名的時候首字母是小寫,沒有按照規范,結果運行的時候它找的是我首字母是大寫的mapper,然而沒有所以報錯了!!真是無語子,應該平時多注意一下命名的規范了,
1、關聯-association
2、集合-collection
3、所以association是用于一對一和多對一,而collection是用于一對多的關系
4、JavaType和ofType都是用來指定物件型別的
JavaType是用來指定pojo中屬性的型別
ofType指定的是映射到list集合屬性中pojo的型別,
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/250296.html
標籤:java
