06.mybatis中的连接池以及标签

mybatis中的连接池以及标签


mybatis测试表结构.png

一、mybatis中的事务

mybatis底层中从连接池等获取的连接对象默认是关闭了自动提交(整合到Spring中默认是开启),项目生产中也建议这样使用,根据业务需求提交事务;,如果想关闭手动提交,如下,获得指定对象时设置自动提交为true

InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
SqlSessionsession = factory.openSession(true);

二、Mybatis的动态sql语句

需要传入的参数为pojo,因为解析标签的底层是依赖pojo的get方法的!
当然,也可以传入一个Map集合,key就对应为变量名!

1.<if>标签

比如,查询是,根据id或者username的取值是否为空选择具体的查询语句。<if>标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。另外要注意 where 1=1 的作用~!

  1. dao
public interface UserDao {
    /**  根据user信息进行查询 */
    public List<User> findByUser(User user);
}
  1. UserDao.xml
<select id="findByUser" parameterType="User" resultType="User">
    select * from user where 1 = 1
    <if test="username != null and username != ''">
        and username like #{username}
    </if>
    <if test="address != null">
        and address like #{address}
    </if>
</select>
  1. 测试类略

2.<where>标签 -- where!

使用where标签时,可以不用1=1这种操作;where 元素只会在至少有一个子元素的条件返回 SQL 子句的情况下才去插入“WHERE”子句。而且,若语句的开头为“AND”或“OR”,where 元素会将它们去除。

UserDao.xml

    <select id="findByUser" parameterType="User" resultType="User">
        select * from user
        <where>
            <if test="username != null and username != ''">
                and username like #{username}
            </if>
            <if test="address != null">
                and address like #{address}
            </if>
        </where>
    </select>

3. <foreach>标签

适用于集合查询

参数 作用
collection 代表要遍历的集合元素,注意编写时不要写#{}
open 代表语句的开始
close 代表语句的结束
item 代表遍历集合的每个元素,生成的变量名
separator 代表分隔符
  1. 集合pojo QueryVo
public class QueryVo implements Serializable {

    private List<Integer> ids;

    public List<Integer> getIds() {
        return ids;
    }

    public void setIds(List<Integer> ids) {
        this.ids = ids;
    }
}
  1. dao
    /** 根据集合查询 */
    public List<User> findInIds(QueryVo vo);
  1. UserDao.xml
    <select id="findInIds" parameterType="QueryVo" resultType="User">
        select * from user
        <where>
            <if test="ids != null and ids.size() > 0">
                <foreach collection="ids" open="id in ( " close=")" item="uid" separator=",">
                    #{uid}
                </foreach>
            </if>
        </where>
    </select>
  1. 测试类
...
UserDao userDao = session.getMapper(UserDao.class);
List<Integer> ids = new ArrayList<Integer>();
ids.add(41); ids.add(42); ids.add(43);
QueryVo vo = new QueryVo();
vo.setIds(ids);
List<User> users = userDao.findInIds(vo);
for (User user : users) {
    System.out.println(user);
}

4. mybatis中简化编写的sql片段

Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。

<sql id="defaultSql">
    select * from user
</sql>

<select id="findByUser" parameterType="User" resultType="User">
    <include refid="defaultSql"></include>
    <where>
        <if test="username != null and username != ''">
            and username like #{username}
        </if>
        <if test="address != null">
            and address like #{address}
        </if>
    </where>
</select>

5. mybatis动态SQL拓展

  1. https://blog.csdn.net/wt_better/article/details/80992014

  2. 动态SQL官方文档

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容