0.准备工具
因为配置文件都是读取相同的,所以可以进行工厂类封装。
1 | package org.fkit.factory; |
1.简单的select,insert,update,delete测试
在UserMapper.xml中配置:1
2
3
4
5
6
7
8
9
10
11
12
13
14<insert id="saveUser" parameterType="user" useGeneratedKeys="true">
INSERT INTO tb_user(name, sex, age) VALUES (#{name},#{sex},#{age})
</insert>
<select id="selectUser" parameterType="int" resultType="user">
SELECT * from tb_user where id = #{id}
</select>
<update id="modifyUser" parameterType="user" >
UPDATE tb_user set name = #{name}, sex = #{sex}, age = #{age}
WHERE id = #{id}
</update>
<delete id="removeUser" parameterType="int">
DELETE from tb_user where id=#{id}
</delete>
注:均包含在
可以看到,在saveUser和modifyUser中,都是传入的User对象,而在selectUser和removeUser传入的是int值,所以在测试时,需要进行对应。resultType是返回类型,返回User类型,需要使用User user = …获取到该对象,默认返回的是int类型(好像是。。忘记了)。
分别对其进行测试及结果:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15//InsertTest.java
public class InsertTest {
public static void main(String args[]){
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
User user = new User("jack", "男", 22);
sqlSession.insert("org.fkit.mapper.UserMapper.saveUser", user);
sqlSession.commit();
sqlSession.close();
}
}
//output:
DEBUG [main] - ==> Preparing: INSERT INTO tb_user(name, sex, age) VALUES (?,?,?)
DEBUG [main] - ==> Parameters: jack(String), 男(String), 22(Integer)
DEBUG [main] - <== Updates: 1
1 | //SelectTest.java |
1 | //UpdateTest.java |
1 | //DeleteTest.java |
2.测试ResultMaps
resultMap是mybatis中最重要最强大的元素,它的作用是告诉mybatis将从结果集中取出的数据转化成开发者所需要的对象。1
2
3
4
5
6<select id="selectUsersToMap" resultType="map">
SELECT * from tb_user
</select>
<select id="selectUsers" resultType="org.fkit.domain.User">
SELECT * from tb_user
</select>
1 | //SelectMapTest.java |
查询语句返回的每一条数据都被封装成了一个Map集合,列名作为Map集合的key,而列的值作为Map的value。
虽然数据被封装成Map集合返回,但是Map集合并不能很好描述一个领域的模型,如果像后一个测试一样,使用POJO类来作为领域模型描述数据,会更加准确。
比较特殊的情况:查询到的数据的列和需要返回的对象(User)的属性不一致,则mybatis不会自动赋值,可以使用resultMap进行处理。
在数据库中创建一个tb_user2表,并且插入一定数据:1
2
3
4
5
6
7
8CREATE TABLE tb_user2
(
user_id INT AUTO_INCREMENT
PRIMARY KEY,
user_name VARCHAR(18) NULL,
user_sex CHAR(2) NULL,
user_age INT NULL
)
User映射表使用原来的User.java。
在UserMapper.xml中添加:1
2
3
4
5
6
7
8
9
10<resultMap id="userResultMap" type="org.fkit.domain.User">
<id property="id" column="user_id"/>
<!--column对应数据库中的属性-->
<result property="name" column="user_name"/>
<result property="sex" column="user_sex"/>
<result property="age" column="user_age"/>
</resultMap>
<select id="selectUsers2" resultMap="userResultMap">
SELECT * from tb_user2
</select>
测试:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17//ResultMapTest.java
public class ResultMapTest {
public static void main(String args[]) {
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
List<User> users =
sqlSession.selectList("org.fkit.mapper.UserMapper.selectUsers2");
for (User user : users) {
System.out.println(user);
}
}
}
//output:
DEBUG [main] - ==> Preparing: SELECT * from tb_user2
DEBUG [main] - ==> Parameters:
DEBUG [main] - <== Total: 2
User{id=1, name='qq1', sex='q', age=12}
User{id=2, name='qq2', sex='b', age=13}
(待补充
3.多表查询
两个表:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21CREATE TABLE tb_clazz
(
id INT AUTO_INCREMENT
PRIMARY KEY,
CODE VARCHAR(18) NULL,
CONSTRAINT TB_CLAZZ_id_uindex
UNIQUE (id)
)
CREATE TABLE tb_student
(
id INT AUTO_INCREMENT
PRIMARY KEY,
NAME VARCHAR(18) NULL,
sex CHAR(3) NULL,
age INT NULL,
clazz_id INT NULL,
CONSTRAINT TB_STUDENT_id_uindex
UNIQUE (id),
CONSTRAINT TB_STUDENT_tb_clazz_id_fk
FOREIGN KEY (clazz_id) REFERENCES tb_clazz (id)
)
插入2个班级,4个学生,其中每2个学生对应一个班级。
建立pojo类:1
2
3
4
5
6
7
8
9
10
11
12
13
14public class Clazz {
private Integer id;
private String code;
//setter,getter
}
public class Student {
private Integer id;
private String name;
private String sex;
private Integer age;
private Clazz clazz;
//getter,setter
UserMapper.xml映射:1
2
3
4
5
6
7
8
9
10
11
12<resultMap id="studentResultMap" type="org.fkit.domain.Student">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="sex" column="sex"/>
<result property="age" column="age"/>
<association property="clazz" column="clazz_id"
javaType="org.fkit.domain.Clazz" select="selectClazzWithId"/>
</resultMap>
<select id="selectClazzWithId" resultType="org.fkit.domain.Clazz">
SELECT * FROM tb_clazz where id = #{id}
</select>
测试: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
27//SelectStudentTest.java
public class SelectStudentTest {
public static void main(String args[]) {
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
List<Student> students =
sqlSession.selectList("org.fkit.mapper.UserMapper.selectStudent");
for (Student student : students) {
System.out.println(student);
}
sqlSession.commit();
sqlSession.close();
}
}
//output:
DEBUG [main] - ==> Preparing: SELECT * FROM tb_student
DEBUG [main] - ==> Parameters:
DEBUG [main] - ====> Preparing: SELECT * FROM tb_clazz where id = ?
DEBUG [main] - ====> Parameters: 1(Integer)
DEBUG [main] - <==== Total: 1
DEBUG [main] - ====> Preparing: SELECT * FROM tb_clazz where id = ?
DEBUG [main] - ====> Parameters: 2(Integer)
DEBUG [main] - <==== Total: 1
DEBUG [main] - <== Total: 4
Student{id=1, name='jack', sex='男', age=22, clazz=Clazz{id=1, code='j1601'}}
Student{id=2, name='rose', sex='女', age=18, clazz=Clazz{id=1, code='j1601'}}
Student{id=3, name='tom', sex='男', age=25, clazz=Clazz{id=2, code='j1602'}}
Student{id=4, name='mary', sex='女', age=20, clazz=Clazz{id=2, code='j1602'}}
查询每个班级的学生:
修改clazz.java1
2
3
4
5
6
7
8
9
10
11
12
13
14public class Clazz {
private Integer id;
private String code;
private List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
...
}
UserMapper.xml添加:1
2
3
4
5
6
7
8
9
10
11
12
13<resultMap id="clazzResultMap" type="org.fkit.domain.Clazz">
<id property="id" column="id"/>
<result property="code" column="code"/>
<collection property="students" javaType="ArrayList"
column="id" ofType="org.fkit.domain.Student"
select="selectStudentWithId"/>
</resultMap>
<select id="selectStudentWithId" resultType="org.fkit.domain.Student">
SELECT * FROM tb_student where clazz_id = #{id}
</select>
<select id="selectClazz" resultMap="clazzResultMap">
SELECT * from tb_clazz
</select>
测试: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
27
28
29
30
31
32
33//SelectClazzTest.java
public class SelectClazzTest {
public static void main(String args[]) {
SqlSession sqlSession = FKSqlSessionFactory.getSqlSession();
List<Clazz> clazzes =
sqlSession.selectList("org.fkit.mapper.UserMapper.selectClazz");
for (Clazz clazz : clazzes) {
System.out.println(clazz);
List<Student> students = clazz.getStudents();
for (Student student : students) {
System.out.println(student);
}
}
sqlSession.commit();
sqlSession.close();
}
}
//output:
DEBUG [main] - ==> Preparing: SELECT * from tb_clazz
DEBUG [main] - ==> Parameters:
DEBUG [main] - ====> Preparing: SELECT * FROM tb_student where clazz_id = ?
DEBUG [main] - ====> Parameters: 1(Integer)
DEBUG [main] - <==== Total: 2
DEBUG [main] - ====> Preparing: SELECT * FROM tb_student where clazz_id = ?
DEBUG [main] - ====> Parameters: 2(Integer)
DEBUG [main] - <==== Total: 2
DEBUG [main] - <== Total: 2
Clazz{id=1, code='j1601'}
Student{id=1, name='jack', sex='男', age=22, clazz=null}
Student{id=2, name='rose', sex='女', age=18, clazz=null}
Clazz{id=2, code='j1602'}
Student{id=3, name='tom', sex='男', age=25, clazz=null}
Student{id=4, name='mary', sex='女', age=20, clazz=null}
看到student没有clazz属性,应该是不能递归查询。