JavaEE--mybatis(2)

博客围绕MyBatis展开,介绍了其连接池、事务提交方式、动态SQL语句和多表查询等要点,并给出用户与账户、用户与角色的示例,帮助理解MyBatis在实际中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

mybatis事务提交方式

	1.手动提交:sqlSession.commit();
	2.自动提交:sqlSessionFactory.openSession(true);

mybatis动态sql语句

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

2.where:为了简化上面 where 1=1 的条件拼装,我们可以采用<where>标签来简化开发
		<select id="findByUser" resultType="user" parameterType="user">
			select * from user
			<where>
				<if test="username!=null and username != ' ' ">
					and username like #{username}
				</if>
				 <if test="address != null">
					and address like #{address}
				</if>
			</where>
		</select>

3.foreach:传入多个 id 查询用户信息
	SELECT * FROM USERS WHERE username LIKE '%张%' AND (id =10 OR id =89 OR id=16)
	SELECT * FROM USERS WHERE username LIKE '%张%' AND id IN (10,89,16)
	这样我们在进行范围查询时,就要将一个集合中的值,作为参数动态添加进来。
		<select id="findInIds" resultType="user" parameterType="queryvo">
			select * from user
			<where>
				<if test=""ids != null and ids.size() > 0">
					<foreach collection="ids" open="id in ( " close=")" item="uid" 
						separator=",">
						#{uid}
					</foreach>
				</if>
			</where>
		</select>
		* <foreach>标签用于遍历集合,它的属性:
		* collection:代表要遍历的集合元素,注意编写时不要写#{}
		* open:代表语句的开始部分
		* close:代表结束部分
		* item:代表遍历集合的每个元素,生成的变量名
		* sperator:代表分隔符

4.简化编写的 SQL 片段:
	1.定义代码片段
	<!-- 抽取重复的语句代码片段 --> 
	<sql id="defaultSql">
		select * from user
	</sql>
	2.引用代码片段
	<!-- 配置查询所有操作 --> 
	<select id="findAll" resultType="user">
		<include refid="defaultSql"></include>
	</select>

mybatis中的多表查询

表之间的关系有几种:
	一对多
	多对一
	一对一
	多对多
举例:
	用户和订单就是一对多
	订单和用户就是多对一
		一个用户可以下多个订单
		多个订单属于同一个用户

	人和身份证号就是一对一
		一个人只能有一个身份证号
		一个身份证号只能属于一个人

	老师和学生之间就是多对多
		一个学生可以被多个老师教过
		一个老师可以交多个学生
特例:
	如果拿出每一个订单,他都只能属于一个用户。
	所以Mybatis就把多对一看成了一对一。

示例

用户和账户

一个用户可以有多个账户
一个账户只能属于一个用户(多个账户也可以属于同一个用户)

步骤:
	1、建立两张表:用户表,账户表
			让用户表和账户表之间具备一对多的关系:需要使用外键在账户表中添加
	2、建立两个实体类:用户实体类和账户实体类
		让用户和账户的实体类能体现出来一对多的关系
	3、建立两个配置文件
		用户的配置文件
		账户的配置文件
	4、实现配置:
		当我们查询用户时,可以同时得到用户下所包含的账户信息
		当我们查询账户时,可以同时得到账户的所属用户信息

实现1.当我们查询用户时,可以同时得到用户下所包含的账户信息
	* User 类加入 List<Account>
	* 用户持久层 Dao 接口中加入查询方法
	* 用户持久层 Dao 映射文件配置
	<mapper namespace="com.demo.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="accounts":
	关联查询的结果集存储在 User 对象上的哪个属性。
* ofType="account":
	指定关联查询的结果集中的对象类型即List中的对象类型。此处可以使用别名,也可以使用全限定名。

实现2.当我们查询账户时,可以同时得到账户的所属用户信息
	* Account类加入 User
	* 账户持久层 Dao 接口中加入查询方法
	* 账户持久层 Dao 映射文件配置
	<mapper namespace="com.demo.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>

* association 
	用于指定从表方的引用实体属性
* property="user":
	关联查询的结果存储在 Account对象上的哪个属性。
* javaType="user":
	指定关联查询的结果中的对象类型。此处可以使用别名,也可以使用全限定名。

用户和角色

一个用户可以有多个角色
一个角色可以赋予多个用户
步骤:
	1、建立两张表:用户表,角色表
		让用户表和角色表具有多对多的关系。需要使用中间表,中间表中包含各自的主键,在中间表中是外键。
	2、建立两个实体类:用户实体类和角色实体类
		让用户和角色的实体类能体现出来多对多的关系
		各自包含对方一个集合引用
	3、建立两个配置文件
		用户的配置文件
		角色的配置文件
	4、实现配置:
		当我们查询用户时,可以同时得到用户所包含的角色信息
		当我们查询角色时,可以同时得到角色的所赋予的用户信息

实现1.当我们查询用户时,可以同时得到用户所包含的角色信息
		* User 类加入 List<Role>
		* 用户持久层 Dao 接口中加入查询方法
		* 用户持久层 Dao 映射文件配置
		<mapper namespace="com.demo.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="roles" ofType="role"> 
					<id column="rid" property="roleId"/>
					<result column="role_name" property="roleName"/>
					<result column="role_desc" property="roleDesc"/>
				</collection>
			</resultMap>
			<!-- 配置查询所有操作 --> 
			<select id="findAll" resultMap="userMap">
				select u.*,r.id as rid,r.role_name,r.role_desc from uesr u
				left outer join user_role ur on u.id = ur.uid
				left outer join role r on r.id = ur.rid
			</select>
	</mapper>

实现2.当我们查询角色时,可以同时得到角色的所赋予的用户信息
		* Role类加入 List<User>
		* 角色持久层 Dao 接口中加入查询方法
		* 角色持久层 Dao 映射文件配置
		<mapper namespace="com.demo.dao.IRoleDao"> 
			<resultMap type="role" id="roleMap"> 
				<id column="rid" property="roleId"></id> 
				<result column="role_name" property="roleName"/>
				<result column="role_desc" property="roleDesc"/>
				<!-- collection 是用于建立一对多中集合属性的对应关系
					ofType 用于指定集合元素的数据类型--> 
				<collection property="users" ofType="user">
					 <id column="id" property="id"></id>
					 <result column="username" property="username"></result>
					 <result column="address" property="address"></result>
					 <result column="sex" property="sex"></result>
					 <result column="birthday" property="birthday"></result>
				</collection>
			</resultMap>
			<!-- 配置查询所有操作 --> 
			<select id="findAll" resultMap="userMap">
				select u.*,r.id as rid,r.role_name,r.role_desc from role r
				left outer join user_role ur on r.id = ur.rid
				left outer join user u on u.id = ur.uid
			</select>
		</mapper>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值