Mybatis

MyBatis

myBatis bolg

myBatis 配置解析

使用注解

映射注解

  • Results 用于填写结果集的多个字段的映射关系
  • Result 用于填写结果集的单个字段映射关系
  • ResultMap 根据ID关联XML里的 <resultMap>

其中property表示实体对象的属性名,column表示对应的数据库字段名

1
2
3
4
5
6
7
8
9
@Results({
@Result(property = "userId" , column = "USER_ID"),
@Result(property = "username" , column = "USERNANME"),
@Result(property = "password" , column = "PASSWORD"),
@Result(property = "mobileNum" , column = "PHONE_NUM")
})

@Select("select * form t_user")
List<User> list();

Lombok

Project Lombok is a java library that automatically plugs into your editor and build tools, spicing up your java.
Never write another getter or equals method again, with one annotation your class has a fully featured builder, Automate your logging variables, and much more.

  1. 首先在IDEA中下载Lombok插件

  2. 然后在xml配置文件中导入lombok依赖

    1
    2
    3
    4
    5
    6
    <!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.10</version>
    </dependency>
  3. 在maven中看一下有没有成功导入

主要用法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Getter and @Setter(1)
@FieldNameConstants
@ToString(1)
@EqualsAndHashCode(1)
@AllArgsConstructor(全部构造参数), @RequiredArgsConstructor and @NoArgsConstructor(无参构造)
@Log, @Log4j, @Log4j2, @Slf4j, @XSlf4j, @CommonsLog, @JBossLog, @Flogger, @CustomLog
@Data(1)
@Builder
@SuperBuilder
@Singular
@Delegate
@Value
@Accessors(1)
@Wither
@With
@SneakyThrows
@val
@var
experimental @var
@UtilityClass
Lombok config system

标1为掌握。

1
2
3
4
5
6
7
8
9
10
11
12
package com.example.demo.helloworld;

import lombok.Data;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private String name;
private int id;
private String pwd;
}

@Data注解会自动实现该类的get,set,hashCode,toString,加上了@AllArgsConstructor,就会自动生成包含所有参数的构造方法,在后面并且加上了无参构造方法@NoArgsConstructor。

1
2
3
4
5
6
7
8
9
10
11
package com.example.demo.helloworld;

import lombok.*;

public class User {
//只生成name的get方法
@Getter
private String name;
private int id;
private String pwd;
}

复杂查询

按结果嵌套处理

接口方法 public List<Student> getStudents2();

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<!--
按查询结果嵌套处理
思路:
1. 直接查询出结果,进行结果集的映射
-->
<select id="getStudents2" resultMap="StudentTeacher2" >
select s.id sid, s.name sname , t.name tname
from student s,teacher t
where s.tid = t.id
</select>

<resultMap id="StudentTeacher2" type="Student">
<id property="id" column="sid"/>
<result property="name" column="sname"/>
<!--关联对象property 关联对象在Student实体类中的属性,javaType结果是老teacher-->
<association property="teacher" javaType="Teacher">
<result property="name" column="tname"/>
</association>
</resultMap>

test

1
2
3
4
5
6
7
8
9
10
11
12
13
@Test
public void testGetStudents2(){
SqlSession session = MybatisUtils.getSession();
StudentMapper mapper = session.getMapper(StudentMapper.class);

List<Student> students = mapper.getStudents2();

for (Student student : students){
System.out.println(
"学生名:"+ student.getName()
+"\t老师:"+student.getTeacher().getName());
}
}

一对多的处理

一对多的理解:

  • 一个老师拥有多个学生
  • 如果对于老师这边,就是一个一对多的现象,即从一个老师下面拥有一群学生(集合)!

实体类编写

1
2
3
4
5
6
7
8
9
10
11
12
13
@Data
public class Student {
private int id;
private String name;
private int tid;
}
@Data
public class Teacher {
private int id;
private String name;
//一个老师多个学生
private List<Student> students;
}

… 和之前一样,搭建测试的环境!

按结果嵌套处理

  1. TeacherMapper接口编写方法

    1
    2
    //获取指定老师,及老师下的所有学生
    public Teacher getTeacher(int id);
  2. 编写接口对应的Mapper配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    <mapper namespace="com.kuang.mapper.TeacherMapper">

    <!--
    思路:
    1. 从学生表和老师表中查出学生id,学生姓名,老师姓名
    2. 对查询出来的操作做结果集映射
    1. 集合的话,使用collection!
    JavaType和ofType都是用来指定对象类型的
    JavaType是用来指定pojo中属性的类型
    ofType指定的是映射到list集合属性中pojo的类型。
    -->
    <select id="getTeacher" resultMap="TeacherStudent">
    select s.id sid, s.name sname , t.name tname, t.id tid
    from student s,teacher t
    where s.tid = t.id and t.id=#{id}
    </select>

    <resultMap id="TeacherStudent" type="Teacher">
    <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>
  3. 将Mapper文件注册到MyBatis-config文件中

    1
    2
    3
    <mappers>
    <mapper resource="mapper/TeacherMapper.xml"/>
    </mappers>
  4. 测试

    1
    2
    3
    4
    5
    6
    7
    8
    @Test
    public void testGetTeacher(){
    SqlSession session = MybatisUtils.getSession();
    TeacherMapper mapper = session.getMapper(TeacherMapper.class);
    Teacher teacher = mapper.getTeacher(1);
    System.out.println(teacher.getName());
    System.out.println(teacher.getStudents());
    }

按查询嵌套处理

  1. TeacherMapper接口编写方法

    1
    public Teacher getTeacher2(int id);
  2. 编写接口对应的Mapper配置文件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <select id="getTeacher2" resultMap="TeacherStudent2">
    select * from teacher where id = #{id}
    </select>
    <resultMap id="TeacherStudent2" type="Teacher">
    <!--column是一对多的外键 , 写的是一的主键的列名-->
    <collection property="students" javaType="ArrayList" ofType="Student" column="id" select="getStudentByTeacherId"/>
    </resultMap>
    <select id="getStudentByTeacherId" resultType="Student">
    select * from student where tid = #{id}
    </select>
  3. 将Mapper文件注册到MyBatis-config文件中

  4. 测试

    1
    2
    3
    4
    5
    6
    7
    8
    @Test
    public void testGetTeacher2(){
    SqlSession session = MybatisUtils.getSession();
    TeacherMapper mapper = session.getMapper(TeacherMapper.class);
    Teacher teacher = mapper.getTeacher2(1);
    System.out.println(teacher.getName());
    System.out.println(teacher.getStudents());
    }

小结

  1. 关联-association
  2. 集合-collection
  3. 所以association是用于一对一和多对一,而collection是用于一对多的关系
  4. JavaType和ofType都是用来指定对象类型的
    • JavaType是用来指定pojo中属性的类型
    • ofType指定的是映射到list集合属性中pojo的类型。

动态sql

Mybatis3主要提供了以下CRUD高级注解

  • @SelectProvider(type = XXXX.class , method = “方法名”)
  • @InsertProvider()
  • @UpdataProvider()
  • @DeleteProvider()

例子:

Mapper类

1
2
3
4
5
@Mapper
public interface UserMapper{
@SelectProvider(type = UserProvider.class , method = "list22")
List<User> list();
}

工具类

1
2
3
4
5
public class UserProvider{
public String list22(){
return "select * from t_user";
}
}

type表示工具类,method表示工具类的某个方法,用来返回具体的sql字符串。

补充:当sql传参数有多个的时候必须用@param和#{}来配合使用,如果有${}会报错,如果不加@param也会报错。

缓存

0%