trim 标签的使用 mapper.xml去除结尾逗号
trim 标签常用于去掉结尾的 逗号
prefix:前缀覆盖并增加其内容
suffix:后缀覆盖并增加其内容
prefixOverrides:前缀判断的条件
suffixOverrides:后缀判断的条件
if 标签忽略为空的字段
通常我们在dao层即mapper.xml中使用 if标签 进行过滤判断操作,如下
trim 标签 和 if 标签 使用方法如下代码
<insert id="inserttable" parameterType="map">
INSERT INTO tablename
<trim prefix="(" suffix=")" suffixOverrides="," >
<if test="value!='' and value != null">
value,
</if>
</trim>
<trim prefix="values(" suffix=")" suffixOverrides="," >
<if test="value!='' and value != null">
#{value},
</if>
</trim>
</insert>
插入一条记录时返回该条记录的自增主键值
使用MyBatis往MySQL数据库中插入一条记录后,返回该条记录的自增主键值。
Mybatis的Mapper的标签中有一个属性
Mybatis 配置文件 useGeneratedKeys 参数只针对 insert 语句生效,默认为 false。
当设置为 true 时,表示如果插入的表以自增列为主键,则允许 JDBC 支持自动生成主键,并可将自动生成的主键返回。
<!-- useGeneratedKeys默认值为false,keyProperty的值对应的是User类中的主键名 -->
<insert id="" parameterType="" useGeneratedKeys="true" keyProperty="id">
insert into user(name, password) values(#{name}, #{password})
</insert>
mybatis中使用mysql的模糊查询字符串拼接(like)
<select id="" resultType="">
SELECT * FROM table where name like concat('%',#{name},'%')
</select>
mybatis 判断参数要加.toString()
<if test="params.selectTime =='2'.toString() ">