CURD(增删改查)
标签
- select
- insert
- update
- delete
- sql
- resultMap
- include
属性
- id = “dao 层中的方法名”
- parameterType = “接口中传入方法的参数类型”
- resultType = “返回实体类对象:包.类名” 处理结果集
符号
-
#{} : 占位符,参数占位符 ?,即sql 预编译,可防止 sql注入。
变量替换后,#{} 对应的变量自动加上
单引号 -
${} ` : 拼接符,字符串替换,即 sql 拼接,不可防止 sql注入。
变量替换后,${} 对应的变量 不会加上 单引号
(1)能用 #{} 的地方就用 #{},不用或少用 ${}
(2)表名作参数时,必须用 ${}。如:select * from ${tableName}
(3)order by 时,必须用 ${}。如:select * from t_user order by ${columnName}
(4)使用 ${} 时,要注意何时加或不加单引号
-- 一开始
#{}:select * from t_user where name=#{name}
${}:select * from t_user where name= '${name}'
-- 过程
#{}:select * from t_user where name=?
${}:select * from t_user where name= 'tom'
-- 最后
#{}:select * from t_user where name= 'tom'
${}:select * from t_user where name= 'tom'
***Mapper.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.zcdf.school.dao.NoticeDao">
<sql id="base_clumn_list">
id, name, status, create_time, name_english
</sql>
<resultMap id="notice_res" type="com.zcdf.school.sqlmodel.Notice">
<id column="id" property="id" />
<result column="name" property="name" />
<result column="status" property="status" />
<result column="create_time" property="createTime" />
</resultMap>
<!-- 查 -->
<select id="queryById" resultMap="notice_res">
select
<include refid="base_clumn_list"></include>
from rz_notice
where id = #{id}
</select>
<!-- 增 -->
<insert id="add" parameterType="User">
insert into tb_user values(null,#{username},#{realname})
</insert>
<!-- 删 -->
<delete id="delete" parameterType="int">
delete from tb_user where userid = #{userid}
</delete>
<!-- 改 -->
<update id="update" parameterType="User">
update tb_user set username=#{username},realname=#{realname} where userid=#{userid}
</update>
</mapper>
注意: sql语句后不要出现 ;
号, 使用分页库的时候会出问题。
动态SQL
元素 | 作用 | 具体描述 |
---|---|---|
if | 条件判断 | 单个 条件判断 |
choose、when、otherwise | 条件选择 | 多条件 分支判断 |
where、set | 拼接辅助 | 处理sql语句的拼接问题 |
foreach | 循环 | 循环操作 |
- if
<select id="findByCondition" resultType="com.wtt.entity.Account">
select * from account where 1=1
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="money!=null">
and money=#{money}
</if>
</select>
- choose
<select id="findByCondition" resultType="com.wtt.entity.Account">
select * from account where 1=1
<choose>
<when test="name!=null and name!=''">
and name=#{name}
</when>
<when test="money!=null">
and money=#{money}
</when>
<otherwise>
and isdeleted=0
</otherwise>
</choose>
</select>
- where
where关键字 可以自动去除 第一个 and | or
<select id="findByCondition" resultType="com.wtt.entity.Account">
select * from account
<where>
<if test="name!=null and name!=''">
and name=#{name}
</if>
<if test="money!=null">
and money=#{money}
</if>
</where>
</select>
- set
set关键字 可以自动去除 最后一个 逗号
<update id="updateUserById" parameterType="user">
update user
<set>
<if test="uid!=null">
uid=#{uid},
</if>
<if test="uname!=null and uname!=''">
uname=#{uname},
</if>
</set>
where uid=#{uid}
</update>
- foreach
foreach标签内属性:
- collection:类型 如果你使用的为数组 那么就用
array
; 如果你使用的为集合 那么就用list
- item:数组中每个元素赋值的变量名
- open: 以谁开始
- close:以谁结束
- separator:分割符
<!-- -->
<select id="selectByFor" resultMap="stuMap">
select * from users where id in
<foreach collection="array" item="forEachIds" separator="," open="(" close=")">
#{forEachIds}
</foreach>
</select>
<!-- 集合里面装的都是 Users对象类型的数据 -->
<insert id="insertByFor">
insert into users values
<foreach collection="list" item="stu" separator=",">
(null,#{stu.name},#{stu.age},#{stu.cid})
</foreach>
</insert>