1.基于注解的方式
@Mapper
public interface EtStuMapper {
@Select("select * from et_stu where id = #{0}")
public EtStu getById(String id);
@Insert("insert into et_stu (upass,realname,stu_no,sex,sys_id,major_id) values (#{upass},#{realname},#{stu_no},#{sex},#{sys_id},#{major_id}) ")
public void insert(EtStu o);
@Update("<script>update et_stu "
+"<trim prefix=\"set\" suffixOverrides=\",\">"
+"<if test=\"realname != null and realname != ''\">realname = #{realname},</if>"
+"<if test=\"stu_no != null and stu_no != ''\">stu_no = #{stu_no},</if>"
+"<if test=\"sex != null and sex != ''\">sex = #{sex},</if>"
+"<if test=\"upass != null and upass != ''\">upass = #{upass},</if>"
+"<if test=\"sys_id != null and sys_id != ''\">sys_id = #{sys_id},</if>"
+"<if test=\"major_id != null and major_id != ''\">major_id = #{major_id},</if>"
+"</trim> where id=#{id}</script>")
public void update(EtStu o);
@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 "
+ "left join et_major m on m.id=s.major_id where 1=1"
+"<if test=\"realname != null and realname !='' \"> and realname like concat('%',#{realname},'%') </if>"
+"<if test=\"stu_no != null and stu_no !='' \"> and stu_no = #{stu_no} </if>"
+"<if test=\"sys_id != null and sys_id !='' \"> and sys_id = #{sys_id} </if>"
+"<if test=\"major_id != null and major_id !='' \"> and major_id = #{major_id} </if>"
+"<if test=\"upass != null and upass !='' \"> and upass = #{upass} </if>"
+"</script>")
public List<EtStu> list(EtStu o);
@Delete("delete from et_stu where id = #{0}")
public void delete(String id);
}
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“ >
<delete id="delete" parameterType="java.lang.Integer">
delete from cm_news where id=#{id}
<a name="Mp61e"></a>
# 3.多个关键词模糊查询
```xml
<select id="datalistPage" parameterType="page" resultType="pd">
select
<include refid="Field"></include>
from
<include refid="tableName"></include>
where 1=1
<if test="pd.keywords!= null and pd.keywords != ''"><!-- 关键词检索 -->
and(
BOOK_NAME LIKE CONCAT('%',#{pd.keywords},'%')
OR BOOK_AUTHO LIKE CONCAT('%',#{pd.keywords},'%')
OR BOOK_TYPE LIKE CONCAT('%',#{pd.keywords},'%')
OR BOOK_PRESS LIKE CONCAT('%',#{pd.keywords},'%')
OR BOOK_NUM LIKE CONCAT('%',#{pd.keywords},'%')
)
</if>
</select>
4.多表关联查询
<select id="getOperationLogsPage" resultType="com.pactera.crm.dto.OperationLogDto">
select
a.operation_log_id as operationLogId,
a.operation_detail as operationDetail,
a.user_id as userId,
a.operation_time as operationTime,
a.operation_module as operationModule,
a.create_at as createAt,
a.update_at as updateAt,
a.create_user_id as createUserId,
a.update_user_id as updateUserId,
b.name as userName
from t_operation_log a
inner join (
select
user_id,name
from
t_user
<where>
<if test="userName!=null and userName!=''">
name like concat('%',#{userName},'%')
</if>
</where>
) b on a.user_id = b.user_id
where 1=1
<if test="operation_module!=null and operation_module!=''">
and a.operation_module like concat('%',#{operation_module},'%')
</if>
<if test="operation_detail!=null and operation_detail!=''">
and a.operation_detail like concat('%',#{operation_detail},'%')
</if>
<if test="operation_createAt!=null and operation_updateAt!=null and operation_createAt!='' and operation_updateAt !=''">
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')
</if>
</select>
5.大于小于范围查询
<if test="ticket_left != null and ticket_right != null">
and ticket > #{ticket_left} and ticket <= #{ticket_right}
</if>
6.两表关联查询封装到实体里面的实体
<resultMap id="collectionCarMap" type="com.jf3q.demo.bean.OrderInfo">
<id property="id" column="id"></id>
<result property="buyUserId" column="user_id"></result>
<result property="carId" column="car_id"></result>
<result property="createTime" column="create_time"></result>
<result property="payTime" column="pay_time"></result>
<result property="orderCode" column="order_code"></result>
<result property="orderPrice" column="order_price"></result>
<result property="seeState" column="see_state"></result>
<result property="payState" column="pay_state"></result>
<result property="isok" column="isok"></result>
<association property="carinfo" column="car_id" javaType="com.jf3q.demo.bean.Carinfo">
<id property="id" column="id"></id>
<result property="userId" column="user_id"></result>
<result property="brand" column="brand"></result>
<result property="title" column="title"></result>
<result property="carPrice" column="car_price"></result>
<result property="carYear" column="car_year"></result>
<result property="mileage" column="mileage"></result>
<result property="gearbox" column="gearbox"></result>
<result property="carImg" column="car_img"></result>
<result property="carContent" column="car_content"></result>
<result property="certification" column="certification"></result>
<result property="carCity" column="car_city"></result>
</association>
</resultMap>
<select id="getList" resultMap="collectionCarMap" parameterType="com.jf3q.demo.bean.OrderInfo">
SELECT
a.*, b.*,c.phone as buyUserPhone,c.nickname as buyUserNickname
FROM
orderinfo a
LEFT JOIN carinfo b on a.car_id=b.id
LEFT JOIN userinfo c on a.buy_user_id=c.id
WHERE 1=1
<if test="buyUserPhone!= null and buyUserPhone != ''">
and c.phone like concat('%',#{buyUserPhone},'%')
</if>
<if test="buyUserNickname!= null and buyUserNickname != ''">
and c.nickname like concat('%',#{buyUserNickname},'%')
</if>
<if test="buyUserId!= null ">
and a.buy_user_id= #{buyUserId}
</if>
<if test="saleUserId!= null ">
and a.sale_user_id= #{saleUserId}
</if>
<if test="carId!= null ">
and a.car_id= #{carId}
</if>
<if test="seeState!= null ">
and a.see_state= #{seeState}
</if>
<if test="payState!= null ">
and a.pay_state= #{payState}
</if>
<if test="isok!= null ">
and a.isok= #{isok}
</if>
</select>
订单实体类
@TableId(type = IdType.AUTO)
private Integer id;
private String orderCode;//订单编号
private Integer carId;//汽车ID
private Integer buyUserId;//买车用户ID
private Integer saleUserId;//卖车用户ID
private Double orderPrice;//订单金额
private String createTime;//下单时间
private String payTime;//支付时间
private Integer seeState;//看车状态:0待看车 1已看车
private Integer payState;//支付状态0待支付,1已支付
private Integer isok;//出售状态(-1 待成交 0未成交,1已成交)
@TableField(exist = false)
private String buyUserPhone;//用户手机号
@TableField(exist = false)
private String buyUserNickname;//用户昵称
@TableField(exist = false)
private Carinfo carinfo;//购买的车
汽车实体类
public class Carinfo {
@TableId(type = IdType.AUTO)
private Integer id;
private Integer userId;//用户ID
private String title;
private String brand;
private Double carPrice;//价格;(总得售价)
private Double depositPrice;//定金
private String carYear;//出厂年份;
private String mileage;//里程;
private Integer gearbox;//变速箱(0手自一体 1手动档 2自动挡)
private String carImg;//车的封面;
private String carContent;//车的详细信息;
private String carArchives;//车的档案信息(维修保养记录)
private String createTime;//发布时间;
private Integer state;//状态;(0下架 1上架)
private Integer certification;//是否认证,1是0否
private String carCity;//所在城市(精确到区)
private String address;//看车地址
private String imgs;//车的照片(多张)
@TableField(exist = false)
private String userNickName;
@TableField(exist = false)
private String userPhone;
@TableField(exist = false)
private Boolean isCollection;//登陆者是否收藏
}
参考文章联表查询
https://www.cnblogs.com/yuqingya/p/12012604.html
插入的时候返回主键key
@Insert("INSERT INTO ...")
@Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id")
void insertOrder(Order order);