MyBatis学习笔记——视频补充2

发现之前看的别人的博客也是按照这系列的视频写的,emm;
传智播客牛X!
嘻!

输入映射和输出映射

输入参数映射

即parameterType的使用;
可以是基本数据类型或引用数据类型;
使用Vo类:

public class QueryVo {
    //这个类的作用是:如果页面中查询条件有多种组合(而非单一查询条件:如仅通过id或姓名查询)
    //使用Vo类,将User类封装在里面,可以简化操作;
    private User user;
    public User getUser() {
        return user;
    }
    public void setUser(User user) {
        this.user = user;
    }
}

对应的映射文件:

    <select id="findUserByVo" parameterType="cn.itheima.pojo.QueryVo" resultType="cn.itheima.pojo.User">
        select * from atestlee where name like '%${user.username}%' and sex like '%${user.sex}%'
    </select>

返回值映射

resultType,可取的类型:
pojo类型;
基本数据类型;
返回基本数据类型举例:
映射文件:

    <!-- 只有返回结果为一行一列时,返回值类型才可以指定为基本类型 -->
    <select id="findUserCount" resultType="java.lang.Integer">
        select count(*) from atestlee
    </select>

动态sql

if

作用就是组成动态sql;
使用方式举例:

    <select id="findUserByUserNameAndSex" parameterType="cn.itheima.pojo.User" resultType="cn.itheima.pojo.User">
        select * from atestlee where 1=1
        <if test="username!=null and username!=''">
            and name like '%${username}%'
        </if>
        <if test="sex!=null and sex!=''">
            and sex=#{sex}
        </if>
    </select>

where

作用:

  • 自动向sql语句添加where关键字
  • 会去掉第一个if条件的and关键字
    使用举例:
    <select id="findUserByUserNameAndSex" parameterType="cn.itheima.pojo.User" resultType="cn.itheima.pojo.User">
        select * from atestlee
        <where>
            <if test="username!=null and username!=''">
                and name like '%${username}%'
            </if>
            <if test="sex!=null and sex!=''">
                and sex=#{sex}
            </if>
        </where>
    </select>

特别的,如果之后没有if语句,则不会添加where语句;

sql片段

作用:将sql条件封装起来,封装后可以重用;id:是这个sql条件的唯一标识。
使用举例:

    <sql id="user_where">
        <where>
            <if test="username!=null and username!=''">
                and name like '%${username}%'
            </if>
            <if test="sex!=null and sex!=''">
                and sex=#{sex}
            </if>
        </where>
    </sql>
        <select id="findUserByUserNameAndSex" parameterType="cn.itheima.pojo.User" resultType="cn.itheima.pojo.User">
        select * from atestlee
        <include refid="user_where"></include>
    </select>

foreach

作用:循环传入的集合参数;
属性:
collection:传入的集合的变量名称;
item:每次循环将循环出的数据放入这个变量中;
open:循环开始拼接的字符串;
close:循环结束拼接的字符串;
separator:循环中拼接的分隔符;
sql语句中出现or时,查询效率大幅度降低;所以多用in替代or;

select * from user where id in (1,3,5,7,9)

用foreach写法:

    <select id="findUsersByIds" parameterType="cn.itheima.pojo.QueryVo" resultType="cn.itheima.pojo.User">
        select * from atestlee where id in (1,3,5,7,9)
        <where>
            <if test="ids!=null">
                <foreach collection="ids" item="id" open="id in(" close=")" separator=",">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

关联查询

一对一关联

数据模式:用户表(user_id,)和订单表(id,user_id,number,createtime,note)
先从订单查用户;

select a.*,b.id uid,username, from orders a,user b 
where a.user_id=b.user_id

举例:
一对一自动映射

    <!-- 一对一自动映射 -->
    <!-- 使用返回类型为resultType,即为自动映射,不用手动指定 -->
    <!-- 自动映射的基础是数据库列名和类的对象成员名称相同 -->
    <select id="findOrderAndUser1" resultType="cn.itheima.pojo.CustomOrders">
        select a.*,b.id uid,username,birthday,sex,address
        from orders a,user b
        where a.user_id=b.id
    </select>
public class CustomOrders extends Orders{
    //继承了Orders,有Orders的相关属性;类中定义User的对应属性;
    private Integer uid;
    private String number;
    private Date createtime;
    private String note;
    private User user;
    //...
}

一对一手动映射
在Order中添加User类对象,作为其成员对象;
id:resultMap的唯一标识;
type:将查询出来的数据放入这个指定的对象中;
注意
手动映射需要指定数据库中表的字段名与java中pojo类的属性名称的对应关系

<!-- 一对一手动映射 -->
    <resultMap type="cn.itheima.pojo.Orders" id="orderAndUserResultMap">
        <!-- id标签指定主键字段对应关系
        column:列,数据库中的字段名称;
        property:属性,java中pojo中的属性名称; 
        -->
        <id column="id" property="id"/>
        <!-- result标签指定非主键字段的对应关系 -->
        <result column="user_id" property="userId"/>
        <result column="number" property="number"/>
        <result column="createtime" property="createtime"/>
        <result column="note" property="note"/>
        <!-- association标签,指定单个对象的对应关系 
        property:指定将数据放入Orders中的哪个属性中:user属性;
        javaType:user属性的类型;
        -->
        <!--  -->
        <association property="user" javaType="cn.itheima.pojo.User">
            <!-- 这里的column和property和前面的意义相同,
            property取值为User类的成员变量;
            -->
            <id column="uid" property="id"/>
            <result column="username" property="username"/>
            <result column="birthday" property="birthday"/>
            <result column="sex" property="sex"/>
            <result column="address" property="address"/>
        </association>
    </resultMap>
    <select if="findOrdersAndUser2" resultMap="orderAndUserResultMap">
        select a.*,b.id uid,username,birthday,sex,address
        from orders a,user b
        where a.user_id=b.id
    </select>

Note:
手动映射是mybatis提供的标准方法;自动方法,em,谨慎用。

一对多关联

用户订单的数据模型中,用户相对于订单是一对多。

select a.*,b.id oid,user_id,number,createtime from user a,orders b where a.id=b.user_id

一对多查询时,一个用户对应多个订单,需要在User类中添加List< Orders>对象;
举例:

    <resultMap type="cn.itheima.pojo.User" id="userAndOrderResultMap">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="birthday" property="birthday"/>
        <result column="sex" property="sex"/>
        <result column="address" property="address"/>
        <!-- collection标签,指定对应的集合对象关系映射
        property:将数据放入User对象的哪个属性中:orderList;
        ofType:指定orderList属性的泛型类型;(使用collection标签后,mybatis知道要返回list,所以指定泛型类型即可)
         -->
        <collection property="orderList" ofType="cn.itheima.pojo.Orders">
            <id column="oid" property="id"/>
            <resutl column="userid" property="userId"/>
            <resutl column="createtime" property="createtime"/>
            <resutl column="number" property="number"/>
        </collection>
    </resultMap>
    <select id="findUserAndOrders" resultMap="userAndOrderResultMap">
        select a.*,b.id oid,user_id,number,createtime 
        from user a,orders b 
        where a.id=b.user_id
    </select>

MyBatis整合Spring

整合思路

    <!-- 整合后,会话工厂归spring管理 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis核心配置文件 -->
        <!-- Spring的配置文件,需要加classpath;不是的话,不需要加; -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"></property>
        <!-- 指定会话工厂使用的数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

原生Dao实现

    <!-- 
        配置原生Dao实现
        注意:class必须指定Dao实现类的全路径名称 
    -->
    <bean id="userDao" class="cn.itheima.dao.UserDaoImpl">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
    </bean>

Mapper接口代理实现

其实讲真……我他妈没看懂;
1)

    <!-- 
        Mapper接口代理方式实现
     -->
     <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <!-- 配置mapper接口的权路径名称 -->
        <property name="mapperInterface" values="cn.itheima.maper.UserMapper"></property>
        <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
     </bean>

2)包扫描的方式

     <!-- 
        使用包扫描的方式,批量引入Mapper 
        扫描后饮用的时候,可以使用类名,首字母小写;
    -->
     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定要扫描的包的权路径名称;如果有多个包,用英文状态的都好分隔 -->
        <property name="basePackage" value="cn.itheima.mapper"></property>
     </bean>

逆向工程

通过数据库自动生成单表的、接口类和pojo类
生成单表的增删改查操作;
注意:如果多次执行生成文件的文件,会采用添加而不是覆盖的方式产生新文件;这可能会给以后运行带来问题;
criteria的使用:
实现单表多种条件的查询;
这里写图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值