动态sql使用
一般我们会涉及多个动态查询条件,一般我们是通过 where 1 = 1,这样可以处理where后面对应全空的情况,我们可以使用标签,该标签可以自动处理。
<where> <if test="name != nulll"> and name like concat('%',trim(#{name,jdbcType=VARCHAR}),'%') </if> </where>
重复的sql片段
定义: <sql id="Base_Column_List" > id, name, url, priority, logo, img </sql> 引用: <include refid="Base_Column_List" />
插入时主键返回
<insert id="insert" parameterType="org.andy.shop.model.CourseInfo" > insert into course_info (id, cname, caddress) values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR}) <selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer"> select last_insert_id() </selectKey> </insert>
或
<insert id="insert" parameterType="org.andy.shop.model.CourseInfo" useGeneratedKeys="true" keyProperty="id"> insert into course_info (id, cname, caddress) values (#{id,jdbcType=INTEGER}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR}) </insert>
非自增主键:但我们的ID为uuid字符类型的32为长度时,我们使用mysql的uuid()查询主键,是在查询之后在插入。
<insert id="insert" parameterType="org.andy.shop.model.CourseInfo" > <selectKey keyProperty="id" order="BEFORE" resultType="java.lang.String"> select uuid() </selectKey> insert into course_info (id, cname, caddress) values (#{id,jdbcType=VARCHAR}, #{cname,jdbcType=VARCHAR}, #{caddress,jdbcType=VARCHAR}) </insert>
使用oracle数据库时:使用 序列名.nextval()函数获取。
mybatis使用技巧总结
最新推荐文章于 2024-05-26 16:37:28 发布