mybatis03

1.mybaits的数据源

 Mybatis 将它自己的数据源分为三类:

UNPOOLED     不使用连接池的数据源(想用就查询建)

POOLED       使用连接池的数据源(常用,用的时候才池中取,完了放回)

JNDI            使用 JNDI 实现的数据源 
在这三种数据源中,我们一般采用的是 POOLED 数据源(很多时候我们所说的数据源就是为了更好的管理数据 库连接,也就是我们所说的连接池技术)

2.事务控制

默认不可开启自动提交事务   需要手动提交  session.commit(); 

开启自动提交事务:session = factory.openSession(true); 

设置为自动提交方式为 false再根据情况决定是否进行提交,这种方式更常用。因为我们可以根据业务 情况来决定提交是否进行提交。

3.动态sql语句

 if where foreach

1.需求:根据返回的id或者返回的name或者其他字段来查询对应的数据

 <select id="findByUser" resultType="user" parameterType="user"> 

select * from user 

<where>
<if test="username!=null and username != '' ">//如果username不为空   username是user输入类型里面的某个字段

and username like #{username}                       //sql加上这个

</if>
<if test="address != null">

and address like #{address}

</if>

</where>

 </select> 

2.需求

<foreach>标签用于遍历集合,它的属性:  collection:代表要遍历的集合元素,注意编写时不要写#{}  open:代表语句的开始部分  close:代表结束部分 

传入多个 id 查询用户信息,用下边 sql 实现:

SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16) 

这样我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。

public class QueryVo implements Serializable {

private List<Integer> ids;

**set ()

**get()

}

 

<!-- 查询所有用户在 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>

 4.定义代码片段 

<!-- 抽取重复的语句代码片段 --> 

<sql id="defaultSql"> 

   select * from user 

</sql> 

 

<select id="findAll" resultType="user">

               <include refid="defaultSql"></include>

</select> 

 5.一对一查询

 1.直接在pojo上加字段  使用内连接查询对应的字段

 <!-- 配置查询所有操作--> 

<select id="findAll" resultType="accountuser">   select a.*,u.username,u.address from account a,user u where a.uid =u.id;  </select> 

2.在pojo中引入另一种张表的pojo 使用resultMap 配置

<!-- 建立对应关系 -->

<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>

 

6.一对多

一样的在pojo在添加另一张表pojo的 List<> 类型   使用外连接  join on

<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>

 

 7.多对多

 跟1对多一样的 操作  数据库sql不一样而已

 

posted @ 2020-05-27 01:13  熬夜秃头选拨赛冠军  阅读(120)  评论(0编辑  收藏  举报