mybatis记录
<insert id="insertTeacherGroup"> INSERT INTO grouplink(TEACHER_ID,CLASS1_ID) VALUES (#{teacherId},#{class1Id}) </insert>
1、如果有多个参数,未使用BO,需要在dao层加 @Param(对于的配置文件中的占位符)
public void insertTeacherGroup(@Param("teacherId")Integer teacherId, @Param("class1Id") Integer class1Id);
2、使用MyBatis往数据库中插入一条记录后,需要返回该条记录的自增主键值。
需要添加 useGeneratedKeys="true" keyProperty="id"
<insert id="insertTeacher" parameterType="Teacher" useGeneratedKeys="true" keyProperty="id"> INSERT INTO teacher(TEACHER_NAME) VALUES (#{teacherName}) </insert>
3、当查询一对多(例子:bo 中List<Class1> classes)情况下 使用<collection>标签书写。
<resultMap type="Teacher" id="teachersResult"> <id property="id" column="ID" /> <result property="teacherName" column="TEACHER_NAME" /> <collection property="classes" ofType="Class1"> <result property="class1Name" column="CLASS1_NAME" /> </collection> </resultMap>
4、动态模糊查询 bo: resultType="Address" 模糊查询 concat(concat('%',#{val}),'%')
<select id="getLikeAddress" resultType="Address" resultMap="likeAddressResult"> SELECT * FROM address WHERE USER_CODE=#{userCode} <if test="val!=null"> AND ADDRESS_NAME LIKE concat(concat('%',#{val}),'%') </if> </select> <!-- end 模糊查询常用地址 (可能是多个) -->
浙公网安备 33010602011771号