Mybatis(二)

目录

连接池

连接池原理

事务控制

Mysql的动态SQL语句

if 标签where标签

foreach标签

sql标签include标签

多表查询

 一对一查询

一对多查询

多对多查询


连接池

mybatis的数据源分为三类

  • UNPOOLED    不适用连接池的数据源(每次都创建一个新的连接)
  • POOLED    使用连接池的数据源
  • JNDI    使用JNDI实现的数据源

通常我们都会使用POOLED 使用数据库的数据源,它创建连接的时间点是执行SQL语句的时候。

连接池原理

创建连接时,如果空闲池还有连接,直接拿来用即可;否则,判断活动池是否到达最大值,如果没有达到则创建新的连接;否则,将最先进来的连接返回获取

事务控制

自动提交的设置

sqlSession = factory.openSession(true);

Mysql的动态SQL语句

if 标签where标签

<where>:等价于where 1=1

可以根据实体类的不同取值,使用不同的SQL语句,例如相关信息不为空时,才进行保存

示例代码:如果username不为空 则根据username查询 ,如果password不为空 则根据username、password两个条件进行查询

 <select id="findUser" parameterType="user" resultType="user">
        select * from test_dao
        <where>
            <if test="username!=null">
                and username=#{username}
            </if>
            <if test="password!=null">
                and password=#{password}
            </if>
        </where>
    </select>

foreach标签

示例代码:Peoper类中有一个ArrayList成员

<select id="findUser_arry" resultType="user" parameterType="peoper">
        select * from test_dao
        <where>
            <foreach collection="list" open="id in (" close=")" item="uid" index=",">
                #{uid}
            </foreach>
        </where>
    </select>
  • collection:遍历的集合元素
  • open:语句开始部分
  • close:代表结束部分
  • item:遍历集合的每一个元素
  • separator:分隔符

sql标签include标签

作用:片段抽取

<sql id="select" >
        select * from test_dao
    </sql>

    <select id="findUser_arry" resultType="user" parameterType="peoper">
       <include refid="select"></include>
        <where>
            <foreach collection="list" open="id in (" close=")" item="uid" index=",">
                #{uid}
            </foreach>
        </where>
    </select>

多表查询

案例:用户与账单

 一对一查询

一个账单对应一个用户

方式一:创建一个类,包含用户信息(username、address)、账单信息

@Getter @Setter
public class accountUser extends account {
    private String username;
    private String address;
    @Override
    public String toString() {
        return super.toString()+"accountUser{" +
                "username='" + username + '\'' +
                ", address='" + address + '\'' +
                '}';
    }
}
<select id="findAll_t1" resultType="accountUser">
        SELECT account.* ,user.username,user.address FROM USER ,account  WHERE account.UID=user.id
    </select>

方式二:使用resultMap

在account类中添加成员user

@Setter @Getter @ToString
public class account {
    private int ID;
    private int UID;
    private double MONEY;
//    一个账户对应一个角色
    private user user;
}
<resultMap id="accountMap" type="account">
        <id property="ID" column="aid"/>
        <result property="UID" column="UID"/>
        <result property="MONEY" column="MONEY"/>
        <association property="user" javaType="user">
            <result property="id" column="id"/>
            <result property="username" column="username"/>
            <result property="sex" column="sex"/>
            <result property="birthday" column="birthday"/>
            <result property="address" column="address"/>
        </association>
    </resultMap>
  <select id="findAll_t2" resultMap="accountMap">
        SELECT u.*,a.ID as aid ,a.MONEY,a.UID FROM account a,USER u WHERE u.id=a.UID
    </select>
  • property:类成员的属性名称 
  • column:查询结果后的列名   例如:a.ID使用了别名,column就需要使用别名

一对多查询

一个用户对应多个账单

在user中添加成员List<account>成员

@Getter @Setter @ToString
public class user {
    private int id;
    private String username;
    private Date birthday;
    private String sex;
    private String address;
    private List<account> list;
}
 <resultMap id="usermapper" type="user">
        <id property="id" column="id"/>
        <result property="username" column="username"/>
        <result property="address" column="address"/>
        <result property="sex" column="sex"/>
        <result property="birthday" column="birthday"/>
        <collection property="list" ofType="account">
            <id property="ID" column="aid"/>
            <result property="UID" column="UID"/>
            <result property="MONEY" column="MONEY"/>
        </collection>
    </resultMap>

    <select id="findAll_t1" resultMap="usermapper">
        select u.*, a.id as aid,a.uid,a.money from user u left join account a on u.id=a.uid
    </select>

查询方法使用的是左连接,user表连接account表

多对多查询

 role连接user_role连接user  ,role(职位)对应user信息,采用的是内连接

在role里添加List<user> 成员

@Setter @Getter @ToString
public class role {
    private  int ID;
    private String ROLENAME;
    private  String ROLNDESC;
    private List<user> list;
}
 <resultMap id="roleMapper" type="role">
        <id property="ID" column="ID"/>
        <result property="ROLENAME" column="ROLENAME"/>
        <result property="ROLNDESC" column="ROLNDESC"/>
        <collection property="list" ofType="user">
            <result property="id" column="uid"/>
            <result property="username" column="username"/>
            <result property="sex" column="sex"/>
            <result property="birthday" column="birthday"/>
            <result property="address" column="address"/>
        </collection>
    </resultMap>

    <select id="findAll_t1" resultMap="roleMapper">
      SELECT r.* ,u.id AS uid ,u.username,u.birthday,u.sex,u.address
        FROM role r
         INNER JOIN user_role ON (r.id=user_role.rid)
        INNER  JOIN USER u ON (user_role.uid=u.id)
    </select>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值