mybatis中一增删改查

Posted 流世幻羽

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis中一增删改查相关的知识,希望对你有一定的参考价值。

基本了解:

<!-- namespace:命名空间,用于隔离sql,类似java的包 -->
<!-- #{}:点位符,相当于jdbc的? -->
<!-- ${}:字符串拼接指令,如果入参为普通数据类型时,{}里只能写value -->
<!-- 动态代理开发规则:
1. namespace必需是接口的全路径名
2. 接口的方法名必需与映射文件的sql id一致
3. 接口的输入参数必需与映射文件的parameterType类型一致
4. 接口的返回类型必须与映射文件的resultType类型一致
-->

输出结果

<resultMap type="com.itheima.mybatis.pojo.Order" id="order_map">
		<!-- 用<id>映射主键 -->
		<id property="id" column="id"/>
		<!-- 普通字段用<result>映射 -->
		<result property="userId" column="user_id"/>
		<!-- 单表查询下,字段与属性一致时可以省略 -->
		<!-- <result property="number" column="number"/>
		<result property="createtime" column="createtime"/>
		<result property="note" column="note"/> -->
	</resultMap>
<!-- resultMap的使用 -->
	<select id="getOrderListResultMap" resultMap="order_map">
		SELECT
		  `id`,
		  `user_id`,
		  `number`,
		  `createtime`,
		  `note`
		FROM `order`
	</select>

sql片段

<!-- 定义sql片段 -->
	<sql id="user_sql">
		  `id`,
		  `username`,
		  `birthday`,
		  `sex`,
		  `address`
	</sql>
<!-- parameterType:入参的数据类型
resultType:返回结果的数据类型
-->
<!-- 别名的使用,不区分大小写 -->
<select id="getUserById" parameterType="int" resultType="uSEr">
SELECT
<include refid="user_sql"/>
FROM `user`
WHERE id = #{id}
</select>

LIKE使用

<!-- resultType:返回为集合时,只需设置为其中一个元素的数据类型就可 -->
	<select id="getUserByUserName" parameterType="string" resultType="com.itheima.mybatis.pojo.User">
		SELECT
		  `id`,
		  `username`,
		  `birthday`,
		  `sex`,
		  `address`
		FROM `user`
		<!-- WHERE username LIKE #{name} -->
		<!--  -->
		WHERE username LIKE ‘%${value}%‘
	</select>

where标签使用

<!-- resultType:返回为集合时,只需设置为其中一个元素的数据类型就可 -->
	<select id="getUserByWhere" parameterType="user" resultType="com.itheima.mybatis.pojo.User">
		SELECT
		   <include refid="user_sql"/>
		FROM `user`
		
		<!-- WHERE 1 = 1
		<if test="username != null and username != ‘‘">
			and username LIKE ‘%${username}%‘
		</if>
		<if test="sex != null and sex != ‘‘">
			and sex = #{sex}
		</if> -->
		
		<!-- <where>会自动添加where关键字,同时处理多余的and  -->
		<where>
			<if test="username != null and username != ‘‘">
				and username LIKE ‘%${username}%‘
			</if>
			<if test="sex != null and sex != ‘‘">
				and sex = #{sex}
			</if>
		</where>
	</select>

for标签使用

public class QueryVo {

	private User user;
	
	private List<Integer> ids;

//..省略get和set方法
}

<select id="getUserByIds" parameterType="queryvo" resultType="user">
		SELECT
		  `id`,
		  `username`,
		  `birthday`,
		  `sex`,
		  `address`,
		  `uuid2`
		FROM `user`
		<!-- WHERE id IN(1,10,25,30,32) -->
		<if test="ids != null">
			<where>
				<!-- collection:要遍历的集合,在这里就是pojo的id列表
					 open:循环开始之前输出的内容
					 close:循环结束之后输出的内容
					 item:每次循环的变量
					 separator:分隔符
				 -->
				<foreach collection="ids" open="id IN(" close=")" item="u_id" separator=",">
					#{u_id}
				</foreach>
			</where>
		</if>
	</select>

  

一对一查询

public class Order {
	private Integer id;

	private Integer userId;

	private String number;

	private Date createtime;

	private String note;
	
	private User user;
//..省略get和set
}
public class OrderUser extends Order {
	private String username;
	private String address;
//..省略get和set
}
//方式一查询
<select id="getOrderUser" resultType="orderUser">
		SELECT 
		  o.`id`,
		  o.`user_id`,
		  o.`number`,
		  o.`createtime`,
		  o.`note`,
		  u.`username`,
		  u.`address` 
		FROM
		  `order` o 
		  LEFT JOIN `user` u 
		    ON u.`id` = o.`user_id` 
	</select>
//方式二查询用association
<resultMap type="order" id="order_user_map">
		<!-- 用<id>映射主键 -->
		<id property="id" column="id"/>
		<!-- 普通字段用<result>映射 -->
		<result property="userId" column="user_id"/>
		<!-- 单表查询下,字段与属性一致时可以省略 -->
		<result property="number" column="number"/>
		<result property="createtime" column="createtime"/>
		<result property="note" column="note"/>
		
		<!-- 一对一关联查询用association
			 property:关联的属性,在这里就是订单的用户属性
			 javaType:user属性的数据类型,支持别名
		 -->
		<association property="user" javaType="com.itheima.mybatis.pojo.User">
			<!-- 用<id>映射主键 -->
			<id property="id" column="user_id"/>
			<!-- 普通字段用<result>映射 -->
			<result property="username" column="username"/>
			<result property="address" column="address"/>
			<result property="birthday" column="birthday"/>
			<result property="sex" column="sex"/>
		</association>
	</resultMap>
	
	<select id="getOrderUserResultMap" resultMap="order_user_map">
		SELECT 
		  o.`id`,
		  o.`user_id`,
		  o.`number`,
		  o.`createtime`,
		  o.`note`,
		  u.`username`,
		  u.`address`,
		  u.`birthday`,
		  u.`sex`
		FROM
		  `order` o 
		  LEFT JOIN `user` u 
		    ON u.`id` = o.`user_id` 
	</select>

 一对多查询

public class User {

	private Integer id;
	private String username;// 用户姓名
	private String sex;// 性别
	private Date birthday;// 生日
	private String address;// 地址
	
	private String uuid2;
	
	private List<Order> orders;
//..省略get和set
}
//第一种方法
<!-- resultType:返回为集合时,只需设置为其中一个元素的数据类型就可 -->
	<select id="getUserByQueryVo" parameterType="queryvo" resultType="com.itheima.mybatis.pojo.User">
		SELECT
		  `id`,
		  `username`,
		  `birthday`,
		  `sex`,
		  `address`
		FROM `user`
		<!-- WHERE username LIKE #{name} -->
		<!--  -->
		WHERE username LIKE ‘%${user.username}%‘
	</select>
//第二种方法是用标签collection

<resultMap type="user" id="user_order_map">
		<!-- 用<id>映射主键 -->
		<id property="id" column="id"/>
		<!-- 普通字段用<result>映射 -->
		<result property="username" column="username"/>
		<result property="address" column="address"/>
		<result property="birthday" column="birthday"/>
		<result property="sex" column="sex"/>
		
		<!-- 一对多关联用<collection>
			 property:属性名称
			 ofType:属性的数据类型
		 -->
		<collection property="orders" ofType="order">
			<id property="id" column="o_id"/>
			<!-- 普通字段用<result>映射 -->
			<result property="userId" column="id"/>
			<!-- 单表查询下,字段与属性一致时可以省略 -->
			<result property="number" column="number"/>
			<result property="createtime" column="createtime"/>
			<result property="note" column="note"/>
		</collection>
	</resultMap>
<select id="getUserOrderMap" resultMap="user_order_map">
		SELECT 
		  u.`id`,
		  u.`username`,
		  u.`birthday`,
		  u.`sex`,
		  u.`address`,
		  u.`uuid2`,
		  o.`id` o_id,
		  o.`number`,
		  o.`createtime`,
		  o.`note` 
		FROM
		  `user` u 
		  LEFT JOIN `order` o 
		    ON o.`user_id` = u.`id` 
	</select>

  

 

 

1.增

<insert id="insertUser" parameterType="User">
		INSERT INTO USER
		            (`username`,
		             `birthday`,
		             `sex`,
		             `address`)
		VALUES (#{username},
		        #{birthday},
		        #{sex},
		        #{address});
</insert>

  

 

2.删

<delete id="deleteUser" parameterType="int">
		DELETE FROM `user` WHERE `id` = #{id1}
	</delete>

  

3.改

<update id="updateUser" parameterType="User">
        UPDATE USER SET username = #{username} WHERE id = #{id}
</update>

 









以上是关于mybatis中一增删改查的主要内容,如果未能解决你的问题,请参考以下文章

MyBatis增删改查(步骤详细,由浅入深,适合初学者,只看这一篇就够了)

mybatis怎么进行增删改查

Maven+Mybatis实现数据库增删改查

Mybatis实现增删改查

Mybatis增删改查

mybatis之增删改查