MyBatis-映射文件学习

MyBatis-映射文件的学习

基本结构

1
2
3
4
5
6
7
8
9
<?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="">
<!--
namespace:名称空间;指定为接口的全类名,随意起名,后续推荐与接口绑定
-->
</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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!-- 
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
public Student getStudentById(Integer id);
-->

<!--
@MapKey("id") //需要在接口方法上添加注解设定主键
public Map<Integer, Student> getStudentByLastNameLikeReturnMap(String lastName);
-->
<select id="getStudentByLastNameLikeReturnMap" resultType="myBatis_Study.Student">
select * from user where name like #{name}
</select>

<!--public Map<String, Object> getStudentByIdReturnMap(Integer id); -->
<select id="getStudentByIdReturnMap" resultType="map">
select * from user where id=#{id}
</select>

<!-- public List<Student> getStudentsByLastNameLike(String lastName); -->
<!--resultType:如果返回的是一个集合,要写集合中元素的类型 -->
<select id="getStudentsByLastNameLike" resultType="myBatis_Study.Student">
select * from user where name like #{name}
</select>

<!-- public Student getStudentByMap(Map<String, Object> map); -->
<select id="getStudentByMap" resultType="myBatis_Study.Student">
select * from ${tableName} where id=${id} and name=#{name}
</select>

<!-- public Student getStudentByIdAndLastName(Integer id,String lastName);-->
<select id="getStudentByIdAndLastName" resultType="myBatis_Study.Student">
select * from user where id = #{id} and name=#{name}
</select>

<select id="getStudentById" resultType="myBatis_Study.Student">
select * from user where id = #{id}
</select>
<select id="getStudentById" resultType="myBatis_Study.Student"
databaseId="mysql">
select * from user where id = #{id}
</select>
<!-- 可以使用全局配置文件中的<typeAliases> 设置别名 resultType="别名" -->
<select id="getStudentById" resultType="myBatis_Study.Student"
databaseId="oracle">
select id,name,sex from Student where Student_ID=#{id}
</select>

关于多个参数时报错

单个参数:mybatis不会做特殊处理,

  #{参数名/任意名}:取出参数值。

多个参数:mybatis会做特殊处理。

  多个参数会被封装成 一个map,

  key:param1...paramN,或者参数的索引也可以

   value:传入的参数值

  #{}就是从map中获取指定的key的值;


异常:org.apache.ibatis.binding.BindingException: Parameter 'id' not found. Available parameters are [1, 0, param1, param2]

  操作:

   方法:public Student getEmpByIdAndLastName(Integer id,String name);

   取值:#{id},#{name}

【命名参数】:明确指定封装参数时map的key;@Param(“id”)
多个参数会被封装成 一个map。

   key:使用@Param注解指定的值

   value:参数值

  #{指定的key}取出对应的参数值

解决方案

POJO:
如果多个参数正好是我们业务逻辑的数据模型,我们就可以直接传入pojo

  #{属性名}:取出传入的pojo的属性值

Map:
如果多个参数不是业务模型中的数据,没有对应的pojo,不经常使用,为了方便,我们也可以传入map

  #{key}:取出map中对应的值

TO:
如果多个参数不是业务模型中的数据,但是经常要使用,推荐来编写一个TO(Transfer Object)数据传输对象
Page{
int index;
int size;
}

更新标签

1
2
3
4
5
6
7
8
9
<!-- public void updateStudent(Integer id,String Name,String sex); 
public void updateStudent(@Param("id")Integer id,@Param("name")String Name,@Param("sex")String sex);
-->
<update id="updateStudent">
<!-- 当有多个参数时无法使用属性名来进行绑定,可以使用param1.....paramN 来进行绑定
update user set name=#{param2},sex=#{param3} where id=#{param1} -->
<!-- 如要使用属性名绑定使用@param("")来进行绑定 -->
update user set name=#{name},sex=#{sex} where id=#{id}
</update>

添加标签

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<!-- public void addStudent(Student student); -->
<!-- parameterType:参数类型,可以省略,
获取自增主键的值:
mysql支持自增主键,自增主键值的获取,mybatis也是利用statement.getGenreatedKeys();
useGeneratedKeys="true";使用自增主键获取主键值策略
keyProperty;指定对应的主键属性,也就是mybatis获取到主键值以后,将这个值封装给javaBean的哪个属性
databaseId:在MyBatis 全局配置文件配置的数据库别名,判断当前在MySQL环境下才会运行
-->
<insert id="addStudent" parameterType="com.atguigu.mybatis.bean.Employee"
useGeneratedKeys="true" keyProperty="id" databaseId="mysql">
insert into tbl_employee(name,sex)
values(#{name},#{sex})
</insert>

<!--
获取非自增主键的值:
Oracle不支持自增;Oracle使用序列来模拟自增;
每次插入的数据的主键是从序列中拿到的值;如何获取到这个值;
-->
<insert id="addStudent" databaseId="oracle">
<!--
keyProperty:查出的主键值封装给javaBean的哪个属性
order="BEFORE":当前sql在插入sql之前运行
AFTER:当前sql在插入sql之后运行
resultType:查出的数据的返回值类型

BEFORE运行顺序:
先运行selectKey查询id的sql;查出id值封装给javaBean的id属性
在运行插入的sql;就可以取出id属性对应的值
AFTER运行顺序:
先运行插入的sql(从序列中取出新值作为id);
再运行selectKey查询id的sql;
-->
<selectKey keyProperty="id" order="BEFORE" resultType="Integer">
<!-- 编写查询主键的sql语句 -->
<!-- BEFORE-->
select Student_SEQ.nextval from dual
<!-- AFTER:
select Student_SEQ.currval from dual -->
</selectKey>

<!-- 插入时的主键是从序列中拿到的 -->
<!-- BEFORE:-->
insert into Student(id,name,sex)
values(#{id},#{name},#{sex})
<!-- AFTER:
insert into Student(id,name,sex)
values(Student_seq.nextval,#{name},#{sex}) -->
</insert>

删除标签

1
2
3
4
<!-- public void deleteStudent(Integer id); -->
<delete id="deleteStudent">
delete from user where id=#{id}
</delete>

简单的映射文件演示

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
34
35
36
37
<?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="Dao.StudentDao">
<!--
namespace:名称空间;指定为接口的全类名
id:唯一标识
resultType:返回值类型
#{id}:从传递过来的参数中取出id值
-->

<!-- public Student getStudentId(Integer id); -->
<select id="getStudentId" resultType="student.Student">
select * from user where id = #{id}
</select>

<!-- public void updateStudent(Integer id,String Name,String sex);
public void updateStudent(@Param("id")Integer id,@Param("name")String Name,@Param("sex")String sex);
-->
<update id="updateStudent">
<!-- 当有多个参数时无法使用属性名来进行绑定,可以使用param1.....paramN 来进行绑定
update user set name=#{param2},sex=#{param3} where id=#{param1} -->
<!-- 如要使用属性名绑定使用@param("")来进行绑定 -->
update user set name=#{name},sex=#{sex} where id=#{id}
</update>

<!-- public void addStudent(Student student); -->
<insert id="addStudent">
insert into user(name,sex) values(#{name},#{sex})
</insert>

<!-- public void deleteStudent(Integer id); -->
<delete id="deleteStudent">
delete from user where id=#{id}
</delete>
</mapper>
原创技术分享,您的支持将鼓励我继续创作
0%