insert
插入数据获取最近插入的id:
- sekectKey
//使用selectKey
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
<insert id="insertFile" parameterType="cn.com.wuf.prom.entity.ApplicationFile">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
insert into t_application_file(file_name,file_path,type,
values (#{fileName},#{filePath})
</insert>
- mapper.xml文件中在添加的标签中加上属性useGeneratedKeys=“true” keyProperty=“id”
keyProperty对应的是Java对象的属性名,useGeneratedKeys的作用是允许 JDBC 支持自动生成主键,keyColumn表主键
<insert id="addMember" parameterType="cn.com.wuf.user.center.manager.pojo.member"
useGeneratedKeys="true" keyColumn="id" keyProperty="id">
select
查找包含语句
concat 用于将两个字符串连接起来,形成一个单一的字符串。
REPLACE函数:是用第三个表达式替换第一个字符串表达式中出现的所有第二个字符串表达式的方法。
语法:REPLACE ( string_expression , string_pattern , string_replacement )
CONCAT('%',REPLACE(REPLACE(#{modelName},'%','\%'), '_', '\_'),'%')
select * from t_user where name like CONCAT('%',REPLACE(REPLACE(#{modelName},'%','\%'), '_', '\_'),'%')
mybatis循环
<foreach collection="list" item="orgCode" index="index" open="(" close=")" separator=",">
#{orgCode}
</foreach>