多在mapper接口写@Param注解,为什么不写parameterType也可以运行成功? 因为mybatis能自动识别,但返回值类型不能不写。
idea 在DAO层方法上alt +enter 会提示生成@param(“xxx”)
方式一: where username like CONCAT(CONCAT(’%’, #{creditCode}), ‘%’)
方式二:
<select id="findRoleMulBind" resultType="org.chris.mybatis.entity.Role">
<bind name="pattern_roleName" value="'%'+roleName+'%'"/>
<bind name="pattern_note" value="'%'+note+'%'"/>
select id,role_name as roleName,note from t_role
where role_name like #{pattern_roleName}
and note like #{pattern_note}
</select>
<insert id="insertUUID" parameterType="org.chris.mybatis.entity.User" >
<selectKey keyProperty="id" resultType="string" order="BEFORE">
select replace(uuid(), '-', '') as id from dual
</selectKey>
INSERT INTO `t_user`(`id`,`user_name`,`password`,`sex`,`cnname`,`mobile`) VALUES (#{id},#{userName},# {password},#{sex,typeHandler=org.apache.ibatis.type.EnumOrdinalTypeHandler },#{cnname},#{mobile})
</insert>
SELECT LAST_INSERT_ID():得到刚insert进去记录的主键值,只适用与自增主键
keyProperty:将查询到主键值设置到parameterType指定的对象的哪个属性
order:SELECT LAST_INSERT_ID()执行顺序,相对于insert语句来说它的执行顺序
resultType:指定SELECT LAST_INSERT_ID()的结果类型
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
SELECT LAST_INSERT_ID()
</selectKey>
INSERT INTO user (username,birthday,sex,address)values (#{username},#{birthday},#{sex},#{address})
//创建sqlsession
SqlSession sqlSession = sqlSessionFactory.openSession();
//执行插入操作
sqlSession.insert(“test.insertUser”, user);
// 提交事务
sqlSession.commit();
// 释放资源
sqlSession.close();
<select id="findUserBySex" resultType="org.chris.mybatis.entity.User">
select id,user_name as userName,cnname as cnName,sex,mobile,email,note from t_user where sex in
<foreach collection="sexList" item="sex" index="index" open="(" separator="," close=")">
#{sex}
</foreach>
</select>
<!-- Sql块 -->
<sql id="selectAll">
select * from
</sql>
<!--resultMap属性:指定定义的resultMap的id,如果这个resultMap在其它的mapper文件,前边需要加namespace-->
<select id="getAll" resultMap="roleMap" >
<include refid="selectAll"/> t_role
</select>
trim 属性
prefix:前缀
suffix:后缀
prefixOverrides:忽略第一个指定分隔符
suffixOverrides:会略最后一个分隔符
<select id="getRole2" resultType="role" parameterType="role" >
<include refid="selectAll"/>
t_role
<!--忽略拼接时的第一个and或者or-->
<trim prefix="WHERE" prefixOverrides="and | or">
<if test="id!=null and id!=''">
id=#{id}
</if>
<if test="roleName!=null and roleName!=''">
and roleName=#{roleName}
</if>
</trim>
</select>
<select id="getRole2" resultType="role" parameterType="role" >
<include refid="selectAll"/>
t_role
<where>
<trim prefixOverrides="and | or">
<if test="id!=null and id!=''">
id=#{id}
</if>
<if test="roleName!=null and roleName!=''">
and roleName=#{roleName}
</if>
</trim>
</where>
</select>
和 只会保留一条
<select id="getRole3" resultType="role" parameterType="role" >
<include refid="selectAll"/>
t_role
<choose>
<when test="id!=null and id!=''">
AND id=#{id}
</when>
<when test="roleName != null and roleName!=''">
AND roleName=#{roleName}
</when>
<when test="note!=null and note!=''">
AND remark=#{remark}
</when>
<otherwise>
AND createDate != null
</otherwise>
</choose>
</select>
<insert id="insert2" parameterType="List">
insert into t_role (roleName,note,createDate) values
<foreach collection="list" item="role" open="(" close=")" separator=",">
#{role.roleName},#{role.note},#{role.createDate}
</foreach>
</insert>
set 标签会自动去掉逗号
<update id="updateDpm" parameterType="queryVo">
UPDATE t_dpm
<set>
<if test="dpm.dpmName != null and dpm.dpmName !=''">
dpmName = #{dpm.dpmName},
</if>
<if test="dpm.dpmIntro != null and dpm.dpmIntro !=''">
dpmIntro = #{dpm.dpmIntro},
</if>
</set>
<where>
dpmId = #{dpm.dpmId}
</where>
</update>