文章目录
- 一、基本概念
- 二、传递 pojo 包装对象
- 三、resultMap
- 四、传递参数
- 1. 传递单个参数
- 2. 传递实体类
- 3. 传递多个参数
一、基本概念
已经知道SQL语句传参,是使用标签的 parameterType 来设定参数的具体类型,这个取值可以是基本类型(int),引用类型(string)还可以是实体类型(pojo或称为JavaBean),同时也可以使用实体类的包装类
需要注意的是:
基 本 类 型 和 String 我 们 可 以 直 接 写 类 型 名 称 , 也 可 以 使 用 包 名 . 类 名
的 方 式 , 例 如 :java.lang.String
实体类类型,目前我们只能使用全限定类名,究其原因,是 mybaits 在加载时已经把常用的数据类型注册了别名,从而我们在使用时可以不写包名,而我们的是实体类并没有注册别名,所以必须写全限定类名
<!--保存用户-->
<insert id="saveUser" parameterType="mybatis.domain.User">
insert into user(username,birthday,sex,address)
values (#{username},#{birthday},#{sex},#{address})
</insert>
二、传递 pojo 包装对象
开发中通过 pojo 传递查询条件 ,查询条件是综合的查询条件,不仅包括用户查询条件还包括其它的查询条件(比如将用户购买商品信息也作为查询条件),这时可以使用包装对象传递输入参数。
Pojo 类中包含 pojo。
需求:根据用户名查询用户信息,查询条件放到 QueryVo 的 user 属性中。
@Data
public class QueryVo {
private User user;
}
<!--根据用户名模糊查询,参数是一个QueryVo对象-->
<select id="findByVo" parameterType="mybatis.domain.QueryVo" resultType="mybatis.domain.User">
select * from user where username like #{user.username};
</select>
/**
* 测试包装类作为参数
*/
@Test
public void testFindByQueryVo() {
//5.使用代理对象执行方法
QueryVo queryVo = new QueryVo();
User user = new User();
user.setUsername("%王%");
queryVo.setUser(user);
List<User> users = userDao.findByVo(queryVo);
for (User u : users) {
System.out.println(u);
}
三、resultMap
由于当建立查询的列名和实体类的属性名称不一致时,会导致建立对应关系出错,无法实现封装。
只有保持名称一致时,才能保证返回的结果正确的封装。
当建立查询的列名和实体类的属性名称不一致时可以使用resultMap
标签可以建立对应关系。从而实现封装。
在 select 标签中使用 resultMap 属性指定引用即可。同时 resultMap 可以实现将查询结果映射为复杂类型的 pojo,比如在查询结果映射对象中包括 pojo 和 list 实现一对一查询和一对多查询
在UserDao.xml 中定义resultMap
<resultMap id="userMap" type="mybatis.domain.UserTest">
<!--主键字段的对应-->
<id property="userId" column="id"></id>
<!--非主键字段的对应-->
<result property="userName" column="name"></result>
<result property="userAddress" column="address"></result>
<result property="userSex" column="sex"></result>
<result property="userBirthday" column="birthday"></result>
</resultMap>
id 属性:给定一个唯一标识,是给查询 select 标签引用用的
type 属性:指定实体类的全限定类名
id 标签:用于指定主键字段
result 标签:用于指定非主键字段
column 属性:用于指定数据库列名
property 属性:用于指定实体类属性名称
<!--保存用户-->
<insert id="saveUser" parameterType="mybatis.domain.UserTest">
insert into user(username,birthday,sex,address)
values (#{userName},#{userBirthday},#{userSex},#{userAddress})
</insert>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select * from user
</select>
四、传递参数
建议传递参数时,只按照实体类和加@parm
注解两种情况处理
1. 传递单个参数
可以通过${}
和#{}
以任意的名称获取参数值,但是需要注意${}
的单引号问题
/**
* 根据用户名查询用户信息
*/
User getUserByUsername(String username);
单个参数的传递,也推荐加上@parm
:
User getUserByUsername(@Param("username") String username);
<select id="getUserByUsername" resultType="User">
<!--select * from user where username = #{username}-->
select * from user where username = '${username}'
</select>
@Test
public void testGetUserByUsername(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.getUserByUsername("zhangsan");
System.out.println(user);
}
2. 传递实体类
只需要通过#{}
和${}
以属性的方式访问属性值即可,但是需要注意${}
的单引号问题
public class User {
private Integer id;
private String username;
private String password;
}
<insert id="insertUser">
insert into test values(#{id},#{username},#{password})
</insert>
@Test
public void testInsertUser(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
int result = mapper.insertUser(new User(2, "李四", "123"));
}
3. 传递多个参数
使用@Param
注解命名参数:
此时MyBatis会将这些参数放在一个map集合中,以两种方式进行存储
- 以
@Param
注解的值为键,以参数为值 - 以
param1, param2...
为键,以参数为值
因此只需要通过#{}
和${}
以键的方式访问值即可,但是需要注意${}
的单引号问题
User checkLoginByParam(@Param("username") String username, @Param("password") String password);
<select id="checkLoginByParam" resultType="User">
select * from user where username = #{username} and password = #{password}
</select>
或者:
<select id="checkLoginByParam" resultType="User">
select * from user where username = #{param1} and password = #{param2}
</select>
@Test
public void testCheckLoginByParam(){
SqlSession sqlSession = SqlSessionUtils.getSqlSession();
ParameterMapper mapper = sqlSession.getMapper(ParameterMapper.class);
User user = mapper.checkLoginByParam("zhangsan", "123");
System.out.println(user);
}
源码: