Mybatis的连接池以及事务控制
1 连接池 减少获取连接次数(时间)
连接池用于存储连接的一个容器
容器是一个集合对象,集合必须是线程安全的不能两个线程拿到统一连接
实现队列的特性。
2 mybatis的连接池 配置位置 主配置文件 SqlMapConfig.xml dataSource标签 type属性表示采用何种连接池方式。 取值: POOLED 采用传统的Javax.sql.DataSource规范的连接池 从池中获取连接 UNPOLED 采用传统的获取连接的方式 也实现Javax.sql.DataSource 没有使用池的思想 创建新的连接 JNDI 采用服务器提供的JNDI技术实现获取dataSource对象 不同的服务器能拿到dataSource对象不同。如果不是web或者maven的war工程不能使用。 tomcat服务器采用连接池是dbcp连接池
3 事务
面试:
什么是事务
事务的四大特性ACID
不考虑隔离性会产生的三个问题
解决办法:四种隔离级别
mybatis通过sqlsession对象的commit方法和rollback方法实现事务的提交和回滚.
sqlSession = factory.openSession(true); 自动提交事务。
Mybatis映射文件的SQL深入
标签
<select id="findByUser" resultType="user" parameterType="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>
注意:标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。 另外要注意 where 1=1 的作用~!
标签
简化上面 where 1=1 的条件拼装
<select id="findByUser" resultType="user" parameterType="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>
标签
<!-- 查询所有用户在 id 的集合之中 -->
<select id="findInIds" resultType="user" parameterType="queryvo">
<!-- select * from user where id in (1,2,3,4,5); --> <include refid="defaultSql"></include> <where> <if test="ids != null and ids.size() > 0"> <foreach collection="ids" open="id in ( " close=")" item="uid"
separator=","> #{uid}
</foreach>
</if>
</where>
</select>
SQL 语句:
select 字段 from user where id in (?)
标签用于遍历集合,它的属性:
collection:代表要遍历的集合元素
open:代表语句的开始部分
close:代表结束部分
item:代表遍历集合的每个元素,生成的变量名
sperator:代表分隔符
简化编写的 SQL 片段
<!-- 抽取重复的语句代码片段 --> <sql id="defaultSql">
select * from user
</sql>
<select id="findById" resultType="UsEr" parameterType="int">
<include refid="defaultSql"></include>
where id = #{uid}
</select>
Mybatis的多表查询
一对多 多对一 一对一 多对多
mybatis把多对一看成一对一。
Mybatis中resultMap的column和property属性
resultMap id="给ID名" type="实体类" >
<result column="数据库字段名" property="实体类属性" jdbcType="数据库字段类型" />
</resultMap>
mybatis多表查询
一对一查询
示例:
一个用户有多个账户 一个账户只能属于一个用户
外键在账户表添加
第一种方式
创建用户账户类AccountUser
public class AccountUser extends Account implements Serializable
<mapper namespace="com.itheima.dao.IAccountDao">
<!-- 配置查询所有操作--> <select id="findAll" resultType="accountuser">
select a.*,u.username,u.address from account a,user u where a.uid =u.id;
</select> </mapper>
第二种方式
使用 resultMap,定义专门的 resultMap 用于映射一对一查询结果。
Account类增加User属性
<!-- 建立对应关系 -->
<resultMap type="account" id="accountMap"> <id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
<!-- 它是用于指定从表方的引用实体属性的 -->
<association property="user" javaType="user"> <id column="id" property="id"/>
<result column="username" property="username"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<result column="address" property="address"/>
</association>
</resultMap>
<select id="findAll" resultMap="accountMap">
select u.*,a.id as aid,a.uid,a.money from account a,user u where a.uid =u.id;
</select>
一对多查询
User 类加入 List
<resultMap type="user" id="userMap">
<id column="id" property="id"></id> <result column="username" property="username"/>
<result column="address" property="address"/>
<result column="sex" property="sex"/>
<result column="birthday" property="birthday"/>
<!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型-->
<collection property="accounts" ofType="account"> <id column="aid" property="id"/>
<result column="uid" property="uid"/>
<result column="money" property="money"/>
</collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
select u.*,a.id as aid ,a.uid,a.money from user u left outer join account
a on u.id =a.uid
</select>
collection 部分定义了用户关联的账户信息。表示关联查询结果集
property="accList": 关联查询的结果集存储在 User 对象的上哪个属性。
ofType="account": 指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。
多表查询
类似一对多查询。需要中间表处理
示例:
用户和角色
用户表和角色表需要中间表 中间表包含各自主键 中间表是外键