mybatis学习小结(六)

 1.连接池就是用于存储连接的一个容器

    容器其实就是一个集合对象,该集合必须是线程安全的,不能两个线程拿到同一连接

     同时该集合还必须实现队列的特性(先进先出)

 2.mybatis中的连接池:

     mybatis连接池提供了3种方式的配置:
        配置的位置:
            主配置文件SqlMapConfig.xml中的dataSource标签,type属性就是表示采用何种连接池方式。
        type属性的取值:
            POOLED     采用传统的javax.sql.DataSource规范中的连接池,mybatis中有针对规范的实现
            UNPOOLED 采用传统的获取连接的方式,虽然也实现Javax.sql.DataSource接口,但是并没有使用池的思想。
            JNDI     采用服务器提供的JNDI技术实现,来获取DataSource对象,不同的服务器所能拿到DataSource是不一样。
                 注意:如果不是web或者maven的war工程,是不能使用的。
                 tomcat服务器,采用连接池就是dbcp连接池。

 3.mybatis中的事务
    什么是事务
    事务的四大特性ACID
    不考虑隔离性会产生的3个问题
    解决办法:四种隔离级别

    它是通过sqlsession对象的commit方法和rollback方法实现事务的提交和回滚

    mybatis中事务自动提交设置:           

     SqlSession session=factory.openSession(true);

  4.mybatis中的动态sql语句:if标签:

<!--    <select id="findUserByCondition" resultMap="userMap" parameterType="user">
        select * from user where 1=1
        <if test="userName != null">
            and username = #{userName}
        </if>
        <if test="userSex != null">
           and  sex = #{userSex}
        </if>
    </select>-->

   5.mybatis中的动态sql语句:where标签:  不用再写where 1=1

<select id="findUserByCondition" resultMap="userMap" parameterType="user">
    select * from user
    <where>
        <if test="userName != null">
           and username = #{userName}
        </if>
        <if test="userSex != null">
           and sex = #{userSex}
        </if>
    </where>
</select>

   6.mybatis中动态sql语句:foreach标签和sql标签:

      sql标签:   

     <!-- 了解的内容:抽取重复的sql语句-->
     <sql id="defaultUser">
        select * from user
        </sql>

<!-- 查询所有 -->
<select id="findAll" resultMap="userMap">
    <include refid="defaultUser"></include>
</select>

      foreach标签:

<!-- 根据queryvo中的Id集合实现查询用户列表 -->
<select id="findUserInIds" resultMap="userMap" parameterType="queryvo">
    <include refid="defaultUser"></include>
    <where>
        <if test="ids != null and ids.size()>0">
            <foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
                #{uid}
            </foreach>
        </if>
    </where>
</select>

      

 

 

posted @ 2019-11-16 10:49  小帅学java  阅读(9)  评论(0)    收藏  举报