java——mybatis中的连接池以及事务控制、mybatis基于XML配置的动态SQL语句使用、mybatis中的多表操作

本文介绍了Mybatis中的连接池配置,包括POOLED、UNPOOLED和JNDI三种方式,并强调了连接池在提高效率中的作用。接着,讨论了mybatis通过sqlsession对象进行的事务管理。此外,文章还讲解了Mybatis基于XML配置的动态SQL,如if标签、where标签和foreach标签的使用。最后,探讨了mybatis中的一对一、一对多和多对多的多表操作,并给出了相关配置和实体类的示例。
摘要由CSDN通过智能技术生成

mybatis中的连接池以及事务控制

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连接池。

 

数据源配置就是在 SqlMapConfig.xml 文件中,具体配置如下:
<!-- 配置数据源(连接池)信息 -->
<dataSource type="POOLED">
<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>
MyBatis 在初始化时,根据 <dataSource> type 属性来创建相应类型的的数据源 DataSource ,即:
type=”POOLED” MyBatis 会创建 PooledDataSource 实例
type=”UNPOOLED” MyBatis 会创建 UnpooledDataSource 实例
type=”JNDI” MyBatis 会从 JNDI 服务上查找 DataSource 实例,然后返回使用

mybatis中的事务

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

    @Before  // 测试方法之前执行
    public void init() throws Exception{
        // 1、读取配置文件,生成字节输入流
        in = Resources.getResourceAsStream("SqlMapConfig.xml");

        // 2、获取SQLSessionFactory
        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);

        // 3、获取SqlSessiond对象
        sqlSession = factory.openSession();

        // 4、获取dao得代理对象
        userDao = sqlSession.getMapper(IUserDao.class);
    }

    @After  // 测试方法之后执行
    public void destroy() throws Exception{
        // 提交事务
        sqlSession.commit();

        // 6、释放资源
        sqlSession.close();
        in.close();
    }

Mybatis 的动态 SQL 语句 if标签和where标签

public interface IUserDao {
    // 查询所有用户
    List<User> findAll();

    // 根据id查询用户信息
    User findById(Integer userId);

    // 根据名称模糊查询用户信息
    List<User> findByName(String username);

    // 根据queryVo条件查询用户
    List<User> findUserByVo(QueryVo vo);

    // 根据传入的参数条件查询 user就是查询的条件
    List<User> findUserByCondition(User user);

}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.itcase.dao.IUserDao">

    <!-- 配置 查询结果的列名和实体类的属性名的对应关系 -->
    <resultMap id="userMap" type="user">
        <!-- 主键字段的对应-->
        <id property="userId" column="id"></id>
        <!-- 非主键字段的对应-->
        <result property="userName" column="username"></result>
        <result property="userAddress" column="address"></result>
        <result property="userSex" column="sex"></result>
        <result property="userBirthday" column="birthday"></result>
    </resultMap>

    <!-- 配置查询所有-->
    <select id="findAll" resultType="user">
        select * from user;
    </select>

    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultType="user">
        select * from user where id=#{uid};
    </select>

    <!-- 根据名称模糊查询-->
    <select id="findByName" parameterType="string" resultType="user">
        select * from user where username like '%${value}%';
    </select>

    <!-- 根据queryVo条件查询-->
    <select id="findUserByVo" parameterType="com.itcase.domain.QueryVo" resultType="user">
        select * from user where username like #{user.username};
    </select>

    <!-- 根据条件查询 -->
    <select id="findUserByCondition" resultType="user" parameterType="user">
        select * from user where 1=1
        <if test="username != null">
            and username = #{username}
        </if>
        <if test="sex != null">
            and sex = #{sex}
        </if>
    </select>

</mapper>
<!-- 根据条件查询 -->
    <select id="findUserByCondition" resultType="user" parameterType="user">
        select * from user
        <where>
            <if test="username != null">
                and username = #{username}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

放牛娃@搞IT

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值