接口注解实现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的类型。