MyBatis---进阶

输入参数用包装类传入

简单类型数据作为输入参数,已经在mybatis入门篇说明。

业务:根据用户名模糊查询。

源码下载

输出类型

查询用户总数

查询订单表所有数据

注意在Mapper.xml中书写sql语句的时候,为了避免出错,可以先在数据库中试一下有没有问题。

动态sql

根据用户名和性别查询用户

if标签和where标签

<!-- 根据用户名和性别查询用户 
		注意下面两个if标签,其中任何一个if都可能是假的,如果第一个if是假的,查询语句就变成了:
			select * from user and username = #{username}
		这个查询语句多了一个and导致错误。怎么办?可以引入where标签,where标签可以去掉前置
		and。
	-->
	<select id="selectUserByUsernameAndSex" parameterType="User" resultType="User">
		select * from user 
		<where>
			<if test="sex != null and sex != ''">
				sex = #{sex} 
			</if>
			<if test="username != null and username != ''">
				and username = #{username}
			</if>
		</where>
	</select>

sql标签

把经常重复出现的sql语句提取出来,供其他地方直接引用。下面提取并使用:

<sql id="selectCommon">
		select * from user
	</sql>

<include refid="selectCommon"></include>
<sql id="selectCommon">
		select * from user
	</sql>

	<!-- 根据用户名和性别查询用户 
		注意下面两个if标签,其中任何一个if都可能是假的,如果第一个if是假的,查询语句就变成了:
			select * from user and username = #{username}
		这个查询语句多了一个and导致错误。怎么办?可以引入where标签,where标签可以去掉前置
		and。
	-->
	<select id="selectUserByUsernameAndSex" parameterType="User" resultType="User">
		<!-- select * from user  -->
		<include refid="selectCommon"></include>
		<where>
			<if test="sex != null and sex != ''">
				sex = #{sex} 
			</if>
			<if test="username != null and username != ''">
				and username = #{username}
			</if>
		</where>
	</select>

根据多个id查询用户

关联查询

一对一查询

查询所有订单信息,订单表关联用户表,一个订单对应一个用户,以用户为中心是一对一的。

一对多查询

查询所有用户信息,及与之关联的订单信息。

<!-- 关联查询中,所查的字段一定都要进行手动映射 
		  有一次教训:sql语句中的表名一定要用``引起来,注意不是单引号
	-->
	<resultMap type="Order" id="orderList">
		<id column="id" property="id"/>
		<result column="user_id" property="userId"/>
		<result column="number" property="number"/>
		<result column="createtime" property="createtime"/>
		<association property="user" javaType="User">
			<!-- 注意下面这行代码,配置关联 -->
			<id column="user_id" property="id"/>
			<result column="username" property="username"/>
			<result column="address" property="address"/>
		</association>
	</resultMap>
	
	<!-- 一对一查询:查询所有订单,及关联的相应用户 -->
	<select id="selectOrderList" resultMap="orderList">
		SELECT 
			o.id, 
			o.number, 
			o.createtime, 
			u.id, 
			u.username, 
			u.address
		FROM 
			`order` o
		LEFT JOIN `user` u ON o.user_id = u.id
	</select>
	
	<resultMap type="User" id="userList">
		
		<id column="id" property="id"/>
		<result column="username" property="username"/>
		<collection property="orderList"  javaType="list" ofType="Order">
			<!-- 注意下面这行代码配置关联,用主键配置 -->
			<id column="user_id" property="userId"/>
			<result column="number" property="number"/>
			<result column="createtime" property="createtime"/>
		</collection>
	</resultMap>
	
	<!-- 一对多查询:查询所有用户,及关联的相应订单 -->
	<select id="selectUserList" resultMap="userList">
		SELECT 
			u.id,
			u.username,
			o.id,
			o.number
		FROM 
			`user` u
		LEFT JOIN `order` o ON u.id = o.user_id
	</select>

mybatis整合spring

参考资料

黑马程序员官方资料

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值