【Mybatis框架】输入映射-pojo包装类型

下面说说关于mapper.xml文件中的输入映射

我们看一下之前为User配置的mapper文件UserMapper.xml:
[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  5.   
  6. <!-- namespace命名空间,作用就是对sql进行分类化管理,理解sql隔离  
  7. 注意:使用mapper代理方法开发,namespace有特殊重要的作用 -->  
  8. <mapper namespace="cn.edu.hpu.mybatis.mapper.UserMapper">  
  9.     <!-- 在映射文件中配置很多sql语句 -->  
  10.       
  11.     <!-- 需求:通过id查询用户表的记录 -->  
  12.     <!-- 通过select执行数据库查询,  
  13.          id:标示映射文件中的sql,成为Statement的id   
  14.          将sql语句封装到mappedStatement对象中,所以将id称为statement的id,  
  15.            
  16.          parameterType:指定输入参数的类型,  
  17.            
  18.          #{}标示一个占位符,  
  19.            
  20.          #{id}其中id表示接收输入参数的名称,如果输入参数是简单类型,那么#{}中的值可以任意(如value)。  
  21.            
  22.          resultType:指定sql输出结果的映射的java对象类型,  
  23.          select指定resultType表示将单条记录映射成java对象-->     
  24.     <select id="findUserById" parameterType="int" resultType="cn.edu.hpu.mybatis.PO.User">  
  25.       SELECT * FROM USER WHERE id=#{id}  
  26.     </select>  
  27.       
  28.     <!-- ${}:表示拼接sql串,将接收到的参数内容不加任何修饰拼接在sql中。  
  29.          使用${}拼接sql,引起sql注入。  
  30.          ${}中只能使用value-->  
  31.     <select id="findUserByUserName" parameterType="java.lang.String"   
  32.             resultType="cn.edu.hpu.mybatis.PO.User">  
  33.        select * from user where username like '%${value}%'   
  34.     </select>  
  35.       
  36.     <!-- 添加用户  
  37.     parameterType:指定输入参数类型是pojo(包括用户信息)   
  38.     #{}中指定POJO的属性名,接收到POJO对象的属性值,mybatis通过OGNL获取对象的属性  
  39.     -->  
  40.     <insert id="insertUser" parameterType="cn.edu.hpu.mybatis.PO.User">  
  41.         <!-- 将插入数据的主键返回,返回到user对象中。  
  42.         SELECT_INSERT_ID():得到刚insert进去的主键值,只适用于自增主键   
  43.         KeyProperty:将查询到主键值设置到parameterType指定对象的哪个属性。  
  44.         order:SELECT LAST_INSERT_ID()执行顺序,相对于insert语句来说它的执行顺序  
  45.         -->  
  46.         <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">  
  47.             SELECT LAST_INSERT_ID()  
  48.         </selectKey>  
  49.           
  50.         <!-- 使用MySql的UUID来生成主键  
  51.         执行过程:  
  52.         首先通过uuid()得到主键,将主键设置到user对象的id属性中  
  53.         其次在insert执行时,从user对象中取出id属性值   
  54.         <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String">  
  55.             SELECT uuid()  
  56.         </selectKey>  
  57.         insert into user(id,birthday,sex,address) value(#{id},#{birthday,jdbcType=DATE},#{sex},#{address})-->  
  58.           
  59.         insert into user(username,birthday,sex,address) value(#{username},#{birthday,jdbcType=DATE},#{sex},#{address})  
  60.     </insert>  
  61.       
  62.     <!-- 删除用户 -->  
  63.     <delete id="deleteUser" parameterType="java.lang.Integer">  
  64.         delete from user where id=#{id}  
  65.     </delete>  
  66.       
  67.     <!-- 更新用户   
  68.     分析:  
  69.     需要传入用户的id,需要传入用户的更新信息.  
  70.     parameterType指定user对象,包括id和更新信息(注意:id必须存在)  
  71.     #{id}:从输入user对象中获取id属性值-->  
  72.     <update id="updateUser" parameterType="cn.edu.hpu.mybatis.PO.User">  
  73.         update user set username=#{username},birthday=#{birthday,jdbcType=DATE},sex=#{sex},address=#{address}   
  74.         where id=#{id}  
  75.     </update>  
  76. </mapper>  

在mapper.xml中我们通过parameterType指定输入参数的类型,类型可以是简单类型、hashmap、pojo的包装类型

上面的查询语句输入的都是一个查询参数,当我们输入多个查询参数时应当怎么操作呢?这就要用到POJO的包装对象把大量的查询参数包装在对象中传递给只能接收单个参数的Mapper操作方法了,看看如何来定义POJO包装对象

1传递pojo的包装对象
1.1需求
完成用户信息的综合查询,需要传入查询条件很复杂(可能包括用户信息、其它信息,比如商品、订单的)

1.2定义包装类型pojo
针对上边需求,建议使用自定义的包装类型的pojo。
在包装类型的pojo中将复杂的查询条件包装进去。

写一个例子
首先我们前面定义了一个User类对象,如果对象中的值要发生拓展,在源代码上改是不合适的(因为我们后期要使用工具自动生成User类,里面的东西建议不要改动),这里我们创建一个User的拓展类UserCustom,继承自User类,我们下面包装查询条件时使用的是UserCustom,特此说明。

查询包装类UserQueryVo:
[java]  view plain  copy
  1. package cn.edu.hpu.mybatis.PO;  
  2.   
  3. public class UserQueryVo {  
  4.   
  5.     //在这里包装需要的查询条件  
  6.       
  7.     //用户查询条件  
  8.     private UserCustom userCustom;  
  9.   
  10.     public UserCustom getUserCustom() {  
  11.         return userCustom;  
  12.     }  
  13.   
  14.     public void setUserCustom(UserCustom userCustom) {  
  15.         this.userCustom = userCustom;  
  16.     }  
  17.       
  18.     //包装其他的查询条件,订单、商品  
  19.     //......  
  20. }  

1.3mapper.xml
在UserMapper.xml中定义用户信息综合查询(查询条件复杂,通过高级查询进行复杂关联查询)。
[html]  view plain  copy
  1. <!-- 用户信息综合查询   
  2.     #{UserCustom.sex}取出包装对象中性别值  
  3.     ${UserCustom.username}取得pojo包装对象中用户名称  
  4.     -->  
  5. <select id="findUserList" parameterType="cn.edu.hpu.mybatis.PO.UserQueryVo"   
  6.     resultType="cn.edu.hpu.mybatis.PO.UserCustom">  
  7.     select * from user where user.sex=#{userCustom.sex} and user.username like '%${userCustom.username}%'  
  8. </select>  

在UserMapper类中定义综合查询方法:
//用户管理的Dao接口
[java]  view plain  copy
  1. public interface UserMapper {  
  2.       
  3.     //用户信息综合查询  
  4.     public List<UserCustom> findUserList(UserQueryVo userQueryVo) throws Exception;  
  5.     ......  
  6. }  

之后进行测试:
[java]  view plain  copy
  1. //用户信息综合查询  
  2. @Test  
  3. public void testFindUserList() throws Exception{  
  4.       
  5.     SqlSession sqlSession=sqlSessionFactory.openSession();  
  6.       
  7.     //创建UserMapper代理对象  
  8.     UserMapper userMapper=sqlSession.getMapper(UserMapper.class);  
  9.       
  10.     //创建包装对象,设置查询条件  
  11.     UserQueryVo userQueryVo=new UserQueryVo();  
  12.     UserCustom userCustom=new UserCustom();  
  13.     userCustom.setSex("男");  
  14.     userCustom.setUsername("张三");  
  15.     userQueryVo.setUserCustom(userCustom);  
  16.       
  17.     //调用userMapper的方法  
  18.     List<UserCustom> users=userMapper.findUserList(userQueryVo);  
  19.       
  20.     for (int i = 0; i < users.size(); i++) {  
  21.         UserCustom user=(UserCustom)users.get(i);  
  22.         System.out.println(user.getId()+":"+user.getUsername());  
  23.     }  
  24. }  

测试结果:
1:张三
4:张三丰

输出日志:
[plain]  view plain  copy
  1. DEBUG [main] - Opening JDBC Connection  
  2. DEBUG [main] - Created connection 13994297.  
  3. DEBUG [main] - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.Connection@d58939]  
  4. DEBUG [main] - ==>  Preparing: select * from user where user.sex=? and user.username like '%张三%'   
  5. DEBUG [main] - ==> Parameters: 男(String)  
  6. DEBUG [main] - <==      Total: 2  

传递HashMap与此类似,就不再赘述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值