Mybatis_基础学习_03

Mybatis 的连接池技术

<dataSource type="POOLED">
    <!-- 配置连接数据库的4个基本信息 -->
    <property name="driver" value="${jdbc.driver}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
</dataSource>
  • type属性值

UNPOOLED : 不使用连接池的数据源,直接使用传统技术新建
POOLED : 使用连接池的数据源
JNDI : 使用 JNDI 实现的数据源

POOLED 使用连接池的数据源: 在使用时会引用一个UNPOOLED对象, 在添加链接到连接池时调用

  • 有关POOLED 底层代码逻辑:
获取连接时,首先在空闲池寻找是否有空闲的链接
-- 有,直接获取返回
-- 没有,判断活动池的链接数量是否已经达到最大链接数量
   -- 没有达到,使用UNPOOLED的方法新建一个链接并返回
   -- 已经达到,查询最先开始使用的链接,判断使用时间是否超过20S
	-- 没有超过,进入等待
	-- 已经超过,强制终止连接,此时链接空闲,将连接返回
  • 活动池的链接在使用完毕后会返回到空闲池 ,有被调用则存在于活动池

动态sql的应用

更新语句的范例:

<select id="updateUser" parameterType="User">
    UPDATE USER
    <trim prefix="set" suffixOverrides=",">
        <if test="_username!=null and _username!=''">
            username =#{_username} ,
        </if>
        <if test="_birthday!=null">
            birthday =#{_birthday} ,
        </if>
        <if test="_sex!=null and _sex!=''">
            sex =#{_sex} ,
        </if>
        <if test="_address!=null and _address!=''">
            address =#{_address}
        </if>
    </trim>
    where id =#{_id};
</select>

查询语句范例
foreach标签导入集合参数的使用

<!--查询多个用户-->
<select id="findOneByIds"  resultMap="userMap">
    SELECT * FROM USER
    <where>a
    <foreach collection="list" open="id in(" close=")" item="id" separator=",">
        #{id}
    </foreach>
    </where>
</select>

模糊查询

<!--模糊查询-->
<select id="findOneByCondition" resultMap="userMap">
    SELECT * FROM USER WHERE username like '%${value}%';
    <where>
        <if test="username!=null and username!=''">
            and username = #{_username}
        </if>
        <if test="address!=null and address!=''">
            and address = '${_address}'
        </if>
    </where>
</select>

Mybatis 表关系
表之间的关系有几种:
一对多
多对一
一对一
多对多
一对一关系的数据库查询

<resultMap id="accountMap" type="account">
    <id property="id" column="id"></id>
    <result property="uid" column="uid"></result>
    <result property="money" column="money"></result>
    <association property="user" javaType="User">
        <!--主键字段的对应使用id标签-->
        <id property="_id" column="uuida"></id>
        <!--a非主键字段的对应使用result标签-->
        <result property="_username" column="username"></result>
        <result property="_birthday" column="birthday"></result>
        <result property="_sex" column="sex"></result>
        <result property="_address" column="address"></result>
    </association>
</resultMap>

一对多关系的数据库查询

<resultMap id="userMap" type="lorihen.domain.User">
    <!--主键字段的对应使用id标签-->
    <id property="_id" column="id"></id>
    <!--非主键字段的对应使用result标签-->
    <result property="_username" column="username"></result>
    <result property="_birthday" column="birthday"></result>
    <result property="_sex" column="sex"></result>
    <result property="_address" column="address"></result>
    <collection property="accounts" ofType="Account">
        <id property="id" column="aid"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
    </collection>
</resultMap>

多对多关系的数据库查询
在resultMap标签的编写上和一对多比较并没有什么不同,不同的地方在于sql的编写上,多对多的数据库查询需要引用一个中间表.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值