1.基于注解的方式

  1. @Mapper
  2. public interface EtStuMapper {
  3. @Select("select * from et_stu where id = #{0}")
  4. public EtStu getById(String id);
  5. @Insert("insert into et_stu (upass,realname,stu_no,sex,sys_id,major_id) values (#{upass},#{realname},#{stu_no},#{sex},#{sys_id},#{major_id}) ")
  6. public void insert(EtStu o);
  7. @Update("<script>update et_stu "
  8. +"<trim prefix=\"set\" suffixOverrides=\",\">"
  9. +"<if test=\"realname != null and realname != ''\">realname = #{realname},</if>"
  10. +"<if test=\"stu_no != null and stu_no != ''\">stu_no = #{stu_no},</if>"
  11. +"<if test=\"sex != null and sex != ''\">sex = #{sex},</if>"
  12. +"<if test=\"upass != null and upass != ''\">upass = #{upass},</if>"
  13. +"<if test=\"sys_id != null and sys_id != ''\">sys_id = #{sys_id},</if>"
  14. +"<if test=\"major_id != null and major_id != ''\">major_id = #{major_id},</if>"
  15. +"</trim> where id=#{id}</script>")
  16. public void update(EtStu o);
  17. @Select("<script>select s.*,y.sys_name,m.major_name from et_stu s left join et_system y on y.id=s.sys_id "
  18. + "left join et_major m on m.id=s.major_id where 1=1"
  19. +"<if test=\"realname != null and realname !='' \"> and realname like concat('%',#{realname},'%') </if>"
  20. +"<if test=\"stu_no != null and stu_no !='' \"> and stu_no = #{stu_no} </if>"
  21. +"<if test=\"sys_id != null and sys_id !='' \"> and sys_id = #{sys_id} </if>"
  22. +"<if test=\"major_id != null and major_id !='' \"> and major_id = #{major_id} </if>"
  23. +"<if test=\"upass != null and upass !='' \"> and upass = #{upass} </if>"
  24. +"</script>")
  25. public List<EtStu> list(EtStu o);
  26. @Delete("delete from et_stu where id = #{0}")
  27. public void delete(String id);
  28. }

2.基于xml的


是插入语句带返回值的。设置了主键自增长,此时返回值为0或1,0为失败,1为成功 update语句返回也是一样,0是失败,1是成功,他不用设置什么,默认就会返回的呢。 ```xml <?xml version=”1.0” encoding=”UTF-8” ?> <!DOCTYPE mapper PUBLIC “-//mybatis.org//DTD Mapper 3.0//EN” “http://mybatis.org/dtd/mybatis-3-mapper.dtd“ > id,uid,see_num,title,context,cts,cfrom,cdesc,img insert into cm_news (uid,see_num,title,context,cts,cfrom,cdesc,img) values(#{uid},#{see_num},#{title},#{context},#{cts},#{cfrom},#{cdesc},#{img}) update cm_news uid=#{uid}, see_num=#{see_num}, title=#{title}, context=#{context}, cfrom=#{cfrom}, cdesc=#{cdesc}, img=#{img}, where id=#{id}
  1. <delete id="delete" parameterType="java.lang.Integer">
  2. delete from cm_news where id=#{id}

  1. <a name="Mp61e"></a>
  2. # 3.多个关键词模糊查询
  3. ```xml
  4. <select id="datalistPage" parameterType="page" resultType="pd">
  5. select
  6. <include refid="Field"></include>
  7. from
  8. <include refid="tableName"></include>
  9. where 1=1
  10. <if test="pd.keywords!= null and pd.keywords != ''"><!-- 关键词检索 -->
  11. and(
  12. BOOK_NAME LIKE CONCAT('%',#{pd.keywords},'%')
  13. OR BOOK_AUTHO LIKE CONCAT('%',#{pd.keywords},'%')
  14. OR BOOK_TYPE LIKE CONCAT('%',#{pd.keywords},'%')
  15. OR BOOK_PRESS LIKE CONCAT('%',#{pd.keywords},'%')
  16. OR BOOK_NUM LIKE CONCAT('%',#{pd.keywords},'%')
  17. )
  18. </if>
  19. </select>

4.多表关联查询

  1. <select id="getOperationLogsPage" resultType="com.pactera.crm.dto.OperationLogDto">
  2. select
  3. a.operation_log_id as operationLogId,
  4. a.operation_detail as operationDetail,
  5. a.user_id as userId,
  6. a.operation_time as operationTime,
  7. a.operation_module as operationModule,
  8. a.create_at as createAt,
  9. a.update_at as updateAt,
  10. a.create_user_id as createUserId,
  11. a.update_user_id as updateUserId,
  12. b.name as userName
  13. from t_operation_log a
  14. inner join (
  15. select
  16. user_id,name
  17. from
  18. t_user
  19. <where>
  20. <if test="userName!=null and userName!=''">
  21. name like concat('%',#{userName},'%')
  22. </if>
  23. </where>
  24. ) b on a.user_id = b.user_id
  25. where 1=1
  26. <if test="operation_module!=null and operation_module!=''">
  27. and a.operation_module like concat('%',#{operation_module},'%')
  28. </if>
  29. <if test="operation_detail!=null and operation_detail!=''">
  30. and a.operation_detail like concat('%',#{operation_detail},'%')
  31. </if>
  32. <if test="operation_createAt!=null and operation_updateAt!=null and operation_createAt!='' and operation_updateAt !=''">
  33. and date_format(a.operation_time,'%Y-%m-%d') between date_format(#{operation_createAt},'%Y-%m-%d') and date_format(#{operation_updateAt},'%Y-%m-%d')
  34. </if>
  35. </select>

5.大于小于范围查询

  1. <if test="ticket_left != null and ticket_right != null">
  2. and ticket &gt; #{ticket_left} and ticket &lt;= #{ticket_right}
  3. </if>

6.两表关联查询封装到实体里面的实体

  1. <resultMap id="collectionCarMap" type="com.jf3q.demo.bean.OrderInfo">
  2. <id property="id" column="id"></id>
  3. <result property="buyUserId" column="user_id"></result>
  4. <result property="carId" column="car_id"></result>
  5. <result property="createTime" column="create_time"></result>
  6. <result property="payTime" column="pay_time"></result>
  7. <result property="orderCode" column="order_code"></result>
  8. <result property="orderPrice" column="order_price"></result>
  9. <result property="seeState" column="see_state"></result>
  10. <result property="payState" column="pay_state"></result>
  11. <result property="isok" column="isok"></result>
  12. <association property="carinfo" column="car_id" javaType="com.jf3q.demo.bean.Carinfo">
  13. <id property="id" column="id"></id>
  14. <result property="userId" column="user_id"></result>
  15. <result property="brand" column="brand"></result>
  16. <result property="title" column="title"></result>
  17. <result property="carPrice" column="car_price"></result>
  18. <result property="carYear" column="car_year"></result>
  19. <result property="mileage" column="mileage"></result>
  20. <result property="gearbox" column="gearbox"></result>
  21. <result property="carImg" column="car_img"></result>
  22. <result property="carContent" column="car_content"></result>
  23. <result property="certification" column="certification"></result>
  24. <result property="carCity" column="car_city"></result>
  25. </association>
  26. </resultMap>
  27. <select id="getList" resultMap="collectionCarMap" parameterType="com.jf3q.demo.bean.OrderInfo">
  28. SELECT
  29. a.*, b.*,c.phone as buyUserPhone,c.nickname as buyUserNickname
  30. FROM
  31. orderinfo a
  32. LEFT JOIN carinfo b on a.car_id=b.id
  33. LEFT JOIN userinfo c on a.buy_user_id=c.id
  34. WHERE 1=1
  35. <if test="buyUserPhone!= null and buyUserPhone != ''">
  36. and c.phone like concat('%',#{buyUserPhone},'%')
  37. </if>
  38. <if test="buyUserNickname!= null and buyUserNickname != ''">
  39. and c.nickname like concat('%',#{buyUserNickname},'%')
  40. </if>
  41. <if test="buyUserId!= null ">
  42. and a.buy_user_id= #{buyUserId}
  43. </if>
  44. <if test="saleUserId!= null ">
  45. and a.sale_user_id= #{saleUserId}
  46. </if>
  47. <if test="carId!= null ">
  48. and a.car_id= #{carId}
  49. </if>
  50. <if test="seeState!= null ">
  51. and a.see_state= #{seeState}
  52. </if>
  53. <if test="payState!= null ">
  54. and a.pay_state= #{payState}
  55. </if>
  56. <if test="isok!= null ">
  57. and a.isok= #{isok}
  58. </if>
  59. </select>

订单实体类

  1. @TableId(type = IdType.AUTO)
  2. private Integer id;
  3. private String orderCode;//订单编号
  4. private Integer carId;//汽车ID
  5. private Integer buyUserId;//买车用户ID
  6. private Integer saleUserId;//卖车用户ID
  7. private Double orderPrice;//订单金额
  8. private String createTime;//下单时间
  9. private String payTime;//支付时间
  10. private Integer seeState;//看车状态:0待看车 1已看车
  11. private Integer payState;//支付状态0待支付,1已支付
  12. private Integer isok;//出售状态(-1 待成交 0未成交,1已成交)
  13. @TableField(exist = false)
  14. private String buyUserPhone;//用户手机号
  15. @TableField(exist = false)
  16. private String buyUserNickname;//用户昵称
  17. @TableField(exist = false)
  18. private Carinfo carinfo;//购买的车

汽车实体类

  1. public class Carinfo {
  2. @TableId(type = IdType.AUTO)
  3. private Integer id;
  4. private Integer userId;//用户ID
  5. private String title;
  6. private String brand;
  7. private Double carPrice;//价格;(总得售价)
  8. private Double depositPrice;//定金
  9. private String carYear;//出厂年份;
  10. private String mileage;//里程;
  11. private Integer gearbox;//变速箱(0手自一体 1手动档 2自动挡)
  12. private String carImg;//车的封面;
  13. private String carContent;//车的详细信息;
  14. private String carArchives;//车的档案信息(维修保养记录)
  15. private String createTime;//发布时间;
  16. private Integer state;//状态;(0下架 1上架)
  17. private Integer certification;//是否认证,1是0否
  18. private String carCity;//所在城市(精确到区)
  19. private String address;//看车地址
  20. private String imgs;//车的照片(多张)
  21. @TableField(exist = false)
  22. private String userNickName;
  23. @TableField(exist = false)
  24. private String userPhone;
  25. @TableField(exist = false)
  26. private Boolean isCollection;//登陆者是否收藏
  27. }

参考文章联表查询
https://www.cnblogs.com/yuqingya/p/12012604.html

插入的时候返回主键key

  1. @Insert("INSERT INTO ...")
  2. @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
  3. void insertOrder(Order order);