前言
在Mapper方法上加注解,废弃xml配置方式
@Select @Mapper public interface OrderMapper { /** * 订单查询 * @return */ @Select("SELECT\n" + "\ta.id,\n" + "\ta.orderNo\n" + "FROM\n" + "\tgoods_order a") @Results(id="orderMap",value = { @Result(column = "id",property = "id",id = true), @Result(column = "orderNo",property = "orderNo"), @Result(column = "id",property = "listOrderDetail", many = @Many(select = "getOrderDetailByOrderId",fetchType = FetchType.LAZY)) }) List<Order> queryOrder(); @Select("select id orderDetailId,order_id orderId,good_name goodName from goods_order_detail where id = #{orderId}") List<OrderDetail> getOrderDetailByOrderId(@Param("orderId") Integer orderId); } @Update("<script>" + "update t_emp" + " <set >" + " <if test=\"empName != null\" >" + " emp_name = #{empName,jdbcType=VARCHAR},\n" + " </if>" + " <if test=\"empTel != null\" >" + " emp_tel = #{empTel,jdbcType=VARCHAR}," + " </if>" + " <if test=\"empEducation != null\" >" + " emp_education = #{empEducation,jdbcType=VARCHAR}," + " </if>" + " <if test=\"empBirthday != null\" >" + " emp_birthday = #{empBirthday,jdbcType=DATE}," + " </if>" + " <if test=\"fkDeptId != null\" >" + " fk_dept_id = #{fkDeptId,jdbcType=INTEGER}," + " </if>" + " </set>" + " where emp_id = #{empId,jdbcType=INTEGER}" + "</script>") int updateByPrimaryKeySelective(Employee record);
参考:http://www.yanhongzhi.com/post/mybatis-annotation.html#toc-ba6