$ 与 #的区别
#{}
将传入的数据都当成一个字符串,会对自动传入的数据加一个双引号。
例如:order by #{parameterName} //或取Map中的value#{Key}也是一样操作。
假设传入参数是“Smith”
会解析成:order by "Smith"${}
将传入的数据直接显示生成在sql
例如:order by #{parameterName} //或取Map中的value#{Key}也是一样操作。
假设传入参数是“Smith”
会解析成:order by Smith
like语法
select * from table where username like concat('%',#{username},'%')
多参数传递@Param
userMapper.updateByUserId(@Param("useranme") String username,@Param("id") Long id)
update table set username=#{username} where id =#{id}
多参数传递Map(不推荐)
HashMap<String,Object> map = new HashMap();
map.put("username","小明");
map.put("address","地址");
userMapper.addUser(map);
<insert id = "addUser" prarmeterType="java.util.HashMap">
insert into user (username,address) values (#{useranme},#{address})
</insert>
主键Id回填的两种方式实现
- 方式一
userGenerateKeys = "true" keyProperty="id"
<insert id = "addUser" prarmeterType="java.util.HashMap" userGenerateKeys = "true" keyProperty="id">
insert into user (username,address) values (#{useranme},#{address})
</insert>
- 方式二(不推荐)
<insert id = "addUser" prarmeterType="java.util.HashMap">
<selectKey keyPoperty="id" resultType="java.lang.Long" order="AFTER">
select last_insert_id();
</selectKey>
insert into user (username,address) values (#{useranme},#{address})
</insert>
resultType 详解 返回数据类型 对象,long Integer
resultMap 对象和数据库对应
<resultMap id="BaseResultMap" type="clic.cldc.pojo.ClApply">
-- column:数据库里面列的名字 -- property java对象里面的 名字
<id column="id" jdbcType="BIGINT" property="id" />
<result column="apply_title" jdbcType="VARCHAR" property="applyTitle" />
<result column="address" jdbcType="VARCHAR" property="address" />
<result column="create_time" jdbcType="BIGINT" property="createTime" />
<result column="create_userid" jdbcType="BIGINT" property="createUserid" />
<result column="start_time" jdbcType="BIGINT" property="startTime" />
<result column="end_time" jdbcType="BIGINT" property="endTime" />
<result column="apply_num" jdbcType="INTEGER" property="applyNum" />
<result column="remark" jdbcType="LONGVARCHAR" property="remark" />
<result column="status" jdbcType="INTEGER" property="status" />
</resultMap>
-
<construnctor>标签:有多个构造方法的时候 用这个标签去指定对应的构造方法
如果不想用 arg0,arg1,arg2 就需要给构造参数 加入@Param("") 注解
<constucotr>
<idArg column="id" name="arg0"/>
<result column="apply_title" jdbcType="VARCHAR" property="arg1" />
<result column="address" jdbcType="VARCHAR" property="arg2" />
</constuctor>
动态SQL
-
<if>
<select id = "getBookeByPage" resultMap="BookMap">
select * from t_booke limit #{start},#{size}
</select>
使用后:
<select id = "getBookeByPage" resultMap="BookMap">
select * from t_booke
<if test="statrt !=null and size!=null">
limit #{start},#{size}
</if>
</select>
-
choose 类似于Java 语法switch() case
<select id = "getBookeByPage" resultMap="BookMap">
select * from t_booke
where 1 =1
<choose>
<when test="name!=null and name!=''">
and b_name =#{name}
</when>
<when test="auther!=null and auther!=''">
and auter=#{auther}
</when>
<otherwise>
# 如果前面都不满足 可以继续写在这个里面
</otherwise>
</choose>
</select>
-
<where>
<select id = "getBookeByPage" resultMap="BookMap">
select * from t_booke
<where>
<if test="name!=null and name!=''">
and b_name =#{name}
</if>
<if test="auther!=null and auther!=''">
and auter=#{auther}
</if>
</where>
</select>
-
<set>
<update id ="udaoteBooke">
update booke
<set>
<if test="name!=null and name!=''">
b_name = #{name} ,
</if>
<if test="author!=null and author!=''">
author = #{author} ,
</if>
</set>
where id =#{id}
</update>
-
<trim> 可以代替 where 和 set
- 代替where
<select id = "getBookeByPage" resultMap="BookMap">
select * from t_booke
-- 注意 and 后面有一个空格 preFixOverrides="and " 判断这个 and 要不要去掉 处理前缀
<trim prefix="where" prefixOverrides="and ">
<if test="name!=null and name!=''">
and b_name =#{name}
</if>
<if test="auther!=null and auther!=''">
and auter=#{auther}
</if>
</trim>
</select>
- 代替set
<update id ="udaoteBooke">
update booke
-- 注意 , 后面有一个空格 suffixOverrides="and " 判断这个 逗号 要不要去掉 处理后缀
<trim prefix="set " suffixOverrides=", ">
<if test="name!=null and name!=''">
b_name = #{name} ,
</if>
<if test="author!=null and author!=''">
author = #{author} ,
</if>
</trim >
where id =#{id}
</update>
-
<foreach>
- 批量查询
<select id ="getBookByIds" resultMap="BookMap">
select * from t_book where id in
-- collection集合名称 index:下标 item集合元素里面的每一个项 separator: 分隔符
<foreach collection="ids" open="(" close=")" index="index" item="id" separator=",">
#{id}
</foreach>
</select>
- 批量插入
<insert id ="batchAddBook">
insert into t_book(b_name,author) values -- (xxx,xxx),(xxx,xxx)
<foreach collection="books" separator="," item="book">
(#{book.name},#{booke.author})
</foreach>
</insert >