A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

【郑州校区】Mybatis第二天框架课程(上)

1 课程计划
1、输入映射和输出映射
a) 输入参数映射
b) 返回值映射
2、动态sql
a) If
b) Where
c) Foreach
d) Sql片段
3、关联查询
a) 一对一关联
b) 一对多关联
4、Mybatis整合spring
2 输入映射和输出映射
Mapper.xml映射文件中定义了操作数据库的sql,每个sql是一个statement,映射文件是mybatis的核心。
2.1 parameterType(输入类型)2.1.1 传递简单类型
参考第一天内容
2.1.2 传递pojo对象
Mybatis使用ognl表达式解析对象字段的值,#{}或者${}括号中的值为pojo属性名称。
2.1.3 传递pojo包装对象
        开发中通过pojo传递查询条件 ,查询条件是综合的查询条件,不仅包括用户查询条件还包括其它的查询条件(比如将用户购买商品信息也作为查询条件),这时可以使用包装对象传递输入参数。
Pojo类中包含pojo。
需求:根据用户名查询用户信息,查询条件放到QueryVo的user属性中。
2.1.3.1 QueryVo
public class QueryVo {
        private User user;
        public User getUser() {
                return user;
        }
        public void setUser(User user) {
                this.user = user;
        }
        
        
}
2.1.3.2 Sql语句
SELECT * FROM user where username like '%刘%'
2.1.3.3 Mapper文件
<!-- 使用包装类型查询用户
                使用ognl从对象中取属性值,如果是包装对象可以使用.操作符来取内容部的属性
        -->
        <select id="findUserByQueryVo" parameterType="queryvo" resultType="user">
                SELECT * FROM user where username like '%${user.username}%'
        </select>
2.1.3.4 接口
2.1.3.5 测试方法
@Test
        public void testFindUserByQueryVo() throws Exception {
                SqlSession sqlSession = sessionFactory.openSession();
                //获得mapper的代理对象
                UserMapper userMapper = sqlSession.getMapper(UserMapper.class);
                //创建QueryVo对象
                QueryVo queryVo = new QueryVo();
                //创建user对象
                User user = new User();
                user.setUsername("刘");
                queryVo.setUser(user);
                //根据queryvo查询用户
                List<User> list = userMapper.findUserByQueryVo(queryVo);
                System.out.println(list);
                sqlSession.close();
        }
2.2 resultType(输出类型)2.2.1 输出简单类型
参考getnow输出日期类型,看下边的例子输出整型:
Mapper.xml文件
<!-- 获取用户列表总数 -->
        <select id="findUserCount"parameterType="user"resultType="int">
           select count(1) from user
        </select>
Mapper接口
public int findUserCount(User user) throws Exception;
调用:
Publicvoid testFindUserCount() throws Exception{
                //获取session
                SqlSession session = sqlSessionFactory.openSession();
                //获取mapper接口实例
                UserMapper userMapper = session.getMapper(UserMapper.class);
        
                User user = new User();
                user.setUsername("管理员");
                //传递Hashmap对象查询用户列表
                intcount = userMapper.findUserCount(user);
               
                //关闭session
                session.close();
        }
输出简单类型必须查询出来的结果集有一条记录,最终将第一个字段的值转换为输出类型。
使用session的selectOne可查询单条记录。
2.2.2 输出pojo对象
参考第一天内容
2.2.3 输出pojo列表
参考第一天内容。
2.3 resultMap
        resultType可以指定pojo将查询结果映射为pojo,但需要pojo的属性名和sql查询的列名一致方可映射成功。
        如果sql查询字段名和pojo的属性名不一致,可以通过resultMap将字段名和属性名作一个对应关系 resultMap实质上还需要将查询结果映射到pojo对象中。
        resultMap可以实现将查询结果映射为复杂类型的pojo,比如在查询结果映射对象中包括pojo和list实现一对一查询和一对多查询。
        
2.3.1 Mapper.xml定义
使用resultMap指定上边定义的personmap。
2.3.2 定义resultMap
由于上边的mapper.xml中sql查询列和Users.java类属性不一致,需要定义resultMap:userListResultMap将sql查询列和Users.java类属性对应起来
<id />:此属性表示查询结果集的唯一标识,非常重要。如果是多个字段为复合唯一约束则定义多个<id />。
Property:表示User类的属性。
Column:表示sql查询出来的字段名。
Column和property放在一块儿表示将sql查询出来的字段映射到指定的pojo类属性上。
<result />:普通结果,即pojo的属性。
2.3.3 Mapper接口定义
public List<User> findUserListResultMap() throws Exception;
3 动态sql
通过mybatis提供的各种标签方法实现动态拼接sql。
3.1 If
<!-- 传递pojo综合查询用户信息 -->
        <select id="findUserList"parameterType="user"resultType="user">
                select * from user
                where 1=1
                <if test="id!=null">
                and id=#{id}
                </if>
                <if test="username!=null and username!=''">
                and username like '%${username}%'
                </if>
        </select>
注意要做不等于空字符串校验。
3.2 Where
上边的sql也可以改为:
<select id="findUserList"parameterType="user"resultType="user">
                select * from user
                <where>
                <if test="id!=null and id!=''">
                and id=#{id}
                </if>
                <if test="username!=null and username!=''">
                and username like '%${username}%'
                </if>
                </where>
        </select>
<where />可以自动处理第一个and。
3.3 foreach
向sql传递数组或List,mybatis使用foreach解析,如下:
u 需求
传入多个id查询用户信息,用下边两个sql实现:
SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
SELECT * FROM USERS WHERE username LIKE '%张%'  id IN (10,89,16)
u 在pojo中定义list属性ids存储多个用户id,并添加getter/setter方法
u mapper.xml
<if test="ids!=null and ids.size>0">
                    <foreach collection="ids" open=" and id in(" close=")" item="id" separator="," >
                            #{id}
                    </foreach>
</if>
u 测试代码:
List<Integer> ids = new ArrayList<Integer>();
                ids.add(1);//查询id为1的用户
                ids.add(10); //查询id为10的用户
                queryVo.setIds(ids);
                List<User> list = userMapper.findUserList(queryVo);
3.4 Sql片段
Sql中可将重复的sql提取出来,使用时用include引用即可,最终达到sql重用的目的,如下:
<!-- 传递pojo综合查询用户信息 -->
        <select id="findUserList"parameterType="user"resultType="user">
                select * from user
                <where>
                <if test="id!=null and id!=''">
                and id=#{id}
                </if>
                <if test="username!=null and username!=''">
                and username like '%${username}%'
                </if>
                </where>
        </select>
u 将where条件抽取出来:
<sql id="query_user_where">
        <if test="id!=null and id!=''">
                and id=#{id}
        </if>
        <if test="username!=null and username!=''">
                and username like '%${username}%'
        </if>
</sql>
u 使用include引用:
<select id="findUserList"parameterType="user"resultType="user">
                select * from user
                <where>
                <include refid="query_user_where"/>
                </where>
        </select>
注意:如果引用其它mapper.xml的sql片段,则在引用时需要加上namespace,如下:
<include refid="namespace.sql片段/>
传智播客·黑马程序员郑州校区地址
河南省郑州市 高新区长椿路11号大学科技园(西区)东门8号楼三层
联系电话 0371-56061160/61/62
来校路线  地铁一号线梧桐街站A口出

1 个回复

正序浏览
wushuowei 来自手机 初级黑马 2018-5-16 11:18:43
沙发
谢谢分享!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马