mybatis整理(二)链接池与复杂多向查询

  • Mybatis 中有连接池技术,但是它采用的是自己的连接池技术。在 Mybatis 的 SqlMapConfig.xml 配置文件中,通过来实现 Mybatis 中连接池的配置。大佬讲mybatis连接池的好文章

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

  • 数据源的配置

    我们的数据源配置就是在 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 实例,然后返回使用
    
  • 动态 SQL 大佬全面详解
    if

    <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>
    注意:<if>标签的 test 属性中写的是对象的属性名,如果是包装类的对象要使用 OGNL 表达式的写法。
    另外要注意 where 1=1 的作用~!
    

    where:
    为了简化上面 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>
    

  • Mybatis 中简化编写的 SQL 片段

    Sql 中可将重复的 sql 提取出来,使用时用 include 引用即可,最终达到 sql 重用的目的。
    定义代码片段
    <!-- 抽取重复的语句代码片段 -->
    <sql id="defaultSql">
    	select * from user
    </sql>
    
    引用代码片段
    例一
    <!-- 配置查询所有操作 -->
    <select id="findAll" resultType="user">
    	<include refid="defaultSql"></include>
    </select>
    
    例二
    <!-- 根据 id 查询 -->
    <select id="findById" resultType="UsEr" parameterType="int">
    	<include refid="defaultSql"></include>
    	where id = #{uid}
    </select>
    
  • 多表查询

    • 一对一查询(多对一)
      • 需求
        查询所有账户信息,关联查询下单用户信息。

        注意:
        因为一个账户信息只能供某个用户使用,所以从查询账户信息出发关联查询用户信息为一对一查询。如 果从用户信息出发查询用户下的账户信息则为一对多查询,因为一个用户可以有多个账户

      • 法一:
        新创建一个实体类,使之与两个表查询出来的数据吻合,也就是创建一个类,让他拥有所查询的所有表的属性。

        <!-- 配置查询所有操作-->
        <select id="findAll" resultType="accountuser">  accountuser类就是匹配下一行的accout表和user表而单独创建的。
        	select a.*,u.username,u.address from account a,user u where a.uid =u.id;
        </select>
        
      • 法二:
        使用 resultMap,定义专门的 resultMap 用于映射一对一查询结果。
        通过面向对象的(has a)关系可以得知,我们可以在 Account 类中加入一个 User 类的对象来代表这个账户是哪个用户的。 即扩充两表所对应的类中的其中一个类。
        从表中加上主表的引用

        在account里面加上一个user类。
        public class Account implements Serializable {
        	private Integer id;
        	private Integer uid;
        	private Double money;
        	
        	private User user;
        	............
        	
        
        xml文件:
        <?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.itheima.dao.IAccountDao">
        	<!-- 建立对应关系 -->
        	<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>
        </mapper>
        
    • 一对多查询
      • 需求:
        查询所有用户信息及用户关联的账户信息。

        分析:
        用户信息和他的账户信息为一对多关系,并且查询过程中如果用户没有账户信息,此时也要将用户信息 查询出来,我们想到了左外连接查询比较合适。

      • 主表中加上从表的集合的引用。

        public class User implements Serializable {
        	.......
        	private List<Account> accounts;
        	.......
        
        xml文件
        <?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.itheima.dao.IUserDao">
        	<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>
        </mapper>
        collection
        部分定义了用户关联的账户信息。表示关联查询结果集
        property="accList" :
        关联查询的结果集存储在 User 对象的上哪个属性。
        ofType="account" :
        指定关联查询的结果集中的对象类型即 List 中的对象类型。此处可以使用别名,也可以使用全限定名。
        
    • 多对多
      暂存
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值