首页 > 代码库 > mysql-myibatis-整理

mysql-myibatis-整理

==================================== insert ==========================================
语句1

<insert id="insertEcsUsers" parameterType="com.goldenvista.kjt.entity.message.EcsUsers" useGeneratedKeys="true" keyProperty="userId">
insert into 
ecshop_new.ecs_users
(
reg_time,
mobile_phone,
is_validated
)
values
(
unix_timestamp(NOW()) - 8*3600,
#{mobilePhone},
0
)
</insert>

语句2

<insert id="insertEcsOrderInfo" parameterType="com.goldenvista.kjt.entity.message.EcsOrderInfoM" useGeneratedKeys="true" keyProperty="orderId">
insert
into kjt.ecs_order_info
(
order_sn,
user_id,
password2
<if test="empId != null">
,emp_id
</if>
)
values
(
#{orderSn,jdbcType=VARCHAR},
#{userId,jdbcType=DECIMAL},
#{password2,jdbcType=VARCHAR}
<if test="empId != null">
,#{empId}
</if>
)
</insert>

知识点:
1、Mybatis主键自动生成: 
在mysql、sql server等支持主键自动增长的数据库中,mybatis插入时,对自动生成的字段的写法:

<insert id=”insertAuthor” parameterType=”传参类型,可以是bean/map” useGeneratedKeys=”true” keyProperty=“主键id”> 
insert into
(uername, password, email ) 
vlaues
(#{username}, #{password}, #{email}) 
<insert>

2、可以用控制语句控制是否插入某字段
<if test="empId != null">
,emp_id
</if>

3、在插入时可以指定该字段的字段类型
#{orderSn,jdbcType=VARCHAR}

4、参数类型一般是相应的bean

5、mysql的日期函数

FROM_UNIXTIME(dateSerial,partten)
UNIX_TIMESTAMP(date)
NOW()

mysql> SELECT UNIX_TIMESTAMP() ; (获得当前时间序列)
->1249524739

mysql> SELECT FROM_UNIXTIME( 1249488000, %Y年%m月%d ) 
->2007年11月20

mysql> SELECT UNIX_TIMESTAMP(2009-08-06) ;
->1249488000

mysql> SELECT * FROM `student` WHERE regTime > UNIX_TIMESTAMP(curdate()) //今天所有学生注册记录。

==================================== update ==========================================

语句1

<update id="updateEcsUsersOpenId" parameterType="java.util.Map">
update 
ecshop_new.ecs_users u 
set 
u.kjt_open_id = #{openId} 
where 
(u.user_name = #{userName} or u.mobile_phone = #{userName})
and 
u.password = #{password}
</update>

语句2

<update id="updateOrderStatus" parameterType="java.util.Map">
update 
kjt.ecs_order_info og 
set 
og.order_status=6,
og.password2 = #{password},
og.store_receive_date =UNIX_TIMESTAMP()
WHERE 
og.order_sn = #{orderSn}
</update>

知识点:
1 或条件查询连接一定要加上括号,否则查询或更改就会出错
where (u.user_name = #{userName} or u.mobile_phone = #{userName}) and u.password = #{password}
where u.user_name = #{userName} or u.mobile_phone = #{userName} and u.password = #{password}
这两个语句的查询结果是不一样的

2 参数类型一般是 java.util.Map
parameterType="java.util.Map"

==================================== select ==========================================

语句1:

<select id="querySaleEmployee" resultType="com.goldenvista.kjt.entity.message.EcsUsers">
select 
e.emp_id as empId,
e.emp_name as empName,
e.sheng_code as empShengCode,
e.shi_code as empShiCode,
e.xian_code as empXianCode,
(select r1.region_name from kjt.ecs_region r1 where r1.region_id = e.sheng_code) as empShengName,
(select r2.region_name from kjt.ecs_region r2 where r2.region_id = e.shi_code) as empShiName,
(select r3.region_name from kjt.ecs_region r3 where r3.region_id = e.xian_code) as empXianName
from kjt.sale_employee_info e 
where 1 = 1
<if test="empId != null">
and e.emp_id = #{empId}
</if>
</select>

1 两表联查(最外层单表)
(select r1.region_name from kjt.ecs_region r1 where r1.region_id = e.sheng_code) as empShengName
通过 r1.region_id = e.sheng_code 从 ecs_region 表获取省名称

2 resultType
查到的数据的类型
as empXianName 的 empXianName 必须和 resultType 的数据类型的字段名一致

语句2:

<select id="queryEcsUsersInfo" resultType="com.goldenvista.kjt.entity.message.EcsUsers">
select 
u.user_id as userId,
ifnull(e.emp_id,‘‘) as empId,
ifnull(e.sheng_code,‘‘) as empShengCode,
ifnull((select r1.region_name from kjt.ecs_region r1 where r1.region_id = e.sheng_code),‘‘) as empShengName
from 
ecshop_new.ecs_users u 
left join 
kjt.emp_user_info eu on u.user_id = eu.user_id 
left join 
kjt.sale_employee_info e on eu.emp_id = e.emp_id 
where 
u.is_validated = 0
<if test="openId != null">
and (u.kjt_open_id = #{openId} or u.user_id = #{openId})
</if>
<if test="userName != null">
and (u.user_name = #{userName} or u.mobile_phone = #{userName})
</if> 
<if test="password != null">
and u.password = #{password}
</if>
</select>

1 外层多表联查LEFT JOIN 内层两表联查
实例:当查询某销售本月的销售额的时候,如果不使用left join 连接,那么查询结果不包含销售额为0/null的销售代表的记录
但如果使用left join 则将销售代表个人信息表放在最前面,会得到全部销售的销售额,为0/null的就显示0/null

语句3

<select id="queryOrderInfoByOrderSn" parameterType="java.util.Map" resultType="com.goldenvista.kjt.entity.message.EcsOrderInfo">
select 
o.order_id as orderId,
CASE 
WHEN o.pay_status = 2 THEN (o.money_paid + o.surplus + o.cash_money + o.post_money)
ELSE o.order_amount
END as orderAmount,
o.surplus as surplus,
from_unixtime(o.add_time) as addTime,
o.password2 as password2
from 
kjt.ecs_order_info o
where 
o.order_sn = #{orderSn}
</select>

知识点:

① SQL Select语句完整的执行顺序: 

1、from子句组装来自不同数据源的数据; 
2、where子句基于指定的条件对记录行进行筛选;  
3group by子句将数据划分为多个分组;  
4、使用聚集函数进行计算; 
5、使用having子句筛选分组;  
6、计算所有的表达式;  
7、使用order by对结果集进行排序。

举例:

1.select 列列表 from 表列表名/视图列表名 where 条件.          
2.select 列列表 from 表列表名/视图列表名 where 条件 group by (列列表) having 条件 
    3.select 列列表 from 表列表名/视图列表名 where 条件 group by (列列表) having 条件 order by 列列表 
    4.select 列列表 from 表1 join 表2 on 表1.列1=表2.列1...join 表n on 表n.列1=表(n-1).列1 where 表1.条件 and 表2.条件...表n. 执行顺序: 
     
分析:
1. 先where 后select(先选出符合where子句的元组,再在元组中抽取指定的列组成二维表)
2. 先where 再group 再having 后select
3. 先where 再group 再having 再select 后order          
4. 先join 再where 后select

综上执行顺序为:
join - where - group by - having - select - order by

② 逻辑处理(多选择分支语句)
CASE 
WHEN o.pay_status = 1 THEN (o.money_paid + o.surplus)
WHEN o.pay_status = 2 THEN (o.money_paid + o.surplus + o.cash_money + o.post_money)
ELSE o.order_amount
END as orderAmount

对最终筛选出来的元组进行最后的逻辑处理,因此是在select里进行处理的

③ 时间函数
from_unixtime(o.add_time) as addTime

语句4

<select id="queryEcsGoods" parameterType="java.math.BigDecimal" resultType="com.goldenvista.kjt.entity.message.EcsGoodsInfo">
select 
goods_id as goodsId,
goods_number as goodsNumber
from kjt.ecs_goods 
where goods_id = #{goodsId}
</select>

根据主键查询 参数类型为 BigDecimal
parameterType="java.math.BigDecimal"

语句5

<!-- 查询用户今天消费的记录 -->
<select id="queryUserConsumeDailyByCardId" parameterType="java.lang.String" resultType="com.goldenvista.kjt.entity.mapping.EcsUserConsumeDaily">
select
c.id as id,
c.identify_no as identifyNo,
c.real_name as realName,
c.consume_money as consumeMoney,
c.tax as tax,
c.consume_date as consumeDate
from 
ecshop_new.ecs_user_consume_daily c
where 
c.identify_no = #{cardIdNo}
and 
c.consume_date = curdate()
</select>

参数类型:parameterType="java.lang.String"
“今天”:c.consume_date = curdate()

语句6

<!-- 根据userId查询用户最后一个订单所属货柜 -->
<select id="queryCabinetByUserId" resultType="com.goldenvista.kjt.entity.message.EcsOrderInfoM">
select 
t1.order_id as orderId,
t1.order_sn as orderSn,
t1.station_no as stationNo,
t1.station_name as stationName,
t1.cabinet_no1 as cabinetNo1,
t1.box_no as boxNo 
from 
kjt.ecs_order_info t1, ecshop_new.ecs_users t2
where 
t1.user_id = t2.user_id
and 
t1.user_id = #{userId}
and 
t1.shipping_id = 9
and 
t1.cabinet_no1 is not null
order by
t1.add_time desc
</select>

注意:null 和 "" 和 0 是不一样的,is not null 用于查询不为 null 的元组

语句7

<!-- 按销量从高到低查询商品 -->
<select id="queryGoodsByNumInfo" parameterType="java.util.Map" resultType="com.goldenvista.kjt.entity.message.KjtGoodsInfoModel" >
SELECT 
g.GOODS_ID as goodsID,
g.goods_sn as goodsSn,
g.tax     as taxPrice,
g.MEASURE_UNIT     as goodsUnit,
g.IS_WEIGHT     as isWeight,
g.shop_price     as goodsPrice,
g.GOODS_NAME as goodsName,
g.GOODS_IMG as goodsImg,
g.CAT_ID     as catId,
g.STORE_ID     as storeId,
SUM(o.GOODS_NUMBER) as goodsSum 
FROM
kjt.ecs_order_goods o,kjt.ecs_goods g
WHERE 
o.GOODS_ID = g.GOODS_ID 
GROUP BY 
g.GOODS_ID
ORDER BY 
goodsSum DESC
</select>

sum() 聚集函数常用于统计金额和数量 有聚集函数必定有 group by 子句 且group by 的字段最好使用不常改变的id类字段

SUM(o.GOODS_NUMBER) as goodsSum 
GROUP BY g.GOODS_ID

语句8 

<select id="queryEmployeeList" parameterType="java.util.Map" resultType="com.goldenvista.kjt.entity.message.SaleEmployeeInfo">
SELECT
s.emp_id as empId,
s.emp_name as empName
FROM 
kjt.sale_employee_info s
WHERE 
s.status = 1
<if test="empName != null">
and (s.mobile like CONCAT(%,#{empName},%) or s.emp_name like CONCAT(%,#{empName},%))
</if>
order by s.emp_id desc
</select>

模糊查询
s.mobile like CONCAT(%,#{empName},%)

语句9 

<!-- 统计销售每月发展的会员数 -->
<select id="queryPersonNumMonth" parameterType="java.math.BigDecimal" resultType="java.math.BigDecimal">
select
count(*)
from 
ecshop_new.ecs_users u, kjt.emp_user_info e
where 
u.user_id = e.user_id
and 
e.emp_id = #{empId}
and 
year(from_unixtime(u.reg_time + 3600*8)) = year(now())
and 
month(from_unixtime(u.reg_time + 3600*8)) = month(now())
</select>

日期函数
year()
month()

语句10

<!-- 统计销售每月的销售额 -->
<select id="querySaleMoneyMonth" parameterType="java.math.BigDecimal" resultType="java.math.BigDecimal">
select
sum(o.money_paid + o.surplus + o.cash_money + o.post_money)
from kjt.ecs_order_info o
where o.emp_id = #{empId}
and year(from_unixtime(o.add_time)) = year(now())
and month(from_unixtime(o.add_time)) = month(now())
and o.pay_status = 2
</select>

语句11

<!-- 统计某段时间内销售代表发展的会员数 -->
<select id="queryPersonNumByDate" parameterType="java.util.Map" resultType="java.math.BigDecimal">
select
count(*)
from ecshop_new.ecs_users u, kjt.emp_user_info e
where u.user_id = e.user_id
and e.emp_id = #{empId}
<if test="startDate != null">
and from_unixtime(u.reg_time + 3600*8) >= #{startDate}
</if>
<if test="endDate != null">
<![CDATA[
and from_unixtime(u.reg_time + 3600*8) <= date_add(#{endDate}, interval 1 day)
]]>
</if>
</select>

1 时间段查询

2 日期加减计算
DATE_ADD(date,INTERVAL expr type)
DATE_SUB(date,INTERVAL expr type)

date 是一个 DATETIME 或DATE值,用来指定起始时间。 
expr 是一个表达式,用来指定从起始日期添加或减去的时间间隔值。
type 为关键词,它指示了表达式被解释的方式。

===============================================================
mysql> SELECT 1997-12-31 23:59:59 + INTERVAL 1 SECOND;
-> 1998-01-01 00:00:00
mysql> SELECT DATE_ADD(1997-12-31 23:59:59, INTERVAL 1 SECOND);
-> 1998-01-01 00:00:00
===============================================================
mysql> SELECT INTERVAL 1 DAY + 1997-12-31;
-> 1998-01-01
mysql> SELECT DATE_ADD(1997-12-31 23:59:59,INTERVAL 1 DAY);
-> 1998-01-01 23:59:59
===============================================================
mysql> SELECT 1998-01-01 - INTERVAL 1 SECOND;
-> 1997-12-31 23:59:59
===============================================================

语句 12 

<!-- 分页查询订单列表 -->
<select id="queryKjtOrderList" resultType="com.goldenvista.kjt.entity.mapping.KjtStoreOrderInfo">
SELECT 
ROW_.*
FROM (
select 
t1.order_id as orderId,
t1.order_sn as orderSn,
CASE 
WHEN t1.pay_status = 2 
THEN (t1.money_paid + t1.surplus + t1.cash_money + t1.post_money)
ELSE t1.order_amount
END as orderTaxPrice,
t1.goods_amount as orderPrice
from 
kjt.ecs_order_info t1, ecshop_new.ecs_users t2
where 
t1.user_id = t2.user_id
<if test="userId != null">
and t1.user_id = #{userId}
</if>
order by t1.add_time desc
<![CDATA[
) ROW_
limit #{startRecord},#{pageSize}
]]>
</select>

分页使用 limit 起始索引 每页条数

语句13 多表联合查询

select 
max(s.emp_id) as empId,
max(s.emp_name) as empName,
ifnull(max(g.goods_name),-) as goodsName,
ifnull(SUM(g.goods_number*g.goods_price),0) as totalMoney
from 
kjt.sale_employee_info s 
LEFT JOIN 
kjt.ecs_order_info o on s.emp_id = o.emp_id
LEFT JOIN 
kjt.ecs_order_goods g on o.order_id = g.order_id
where 1=1
group by 
g.goods_id,s.emp_id
order by 
SUM(g.goods_number*g.goods_price) DESC

============================ 拓展知识点 =========================

① Mybatis主键自动生成: 

在MYSQL、sql server等支持主键自动增长的数据库中!mybatis插入时,对自动生成的字段的写法: 
<insert id=”insertAuthor” parameterType=”类型” useGeneratedKeys=”true” keyProperty=“自动生成的字段名”> 
insert into 
数据表名(uername, password, email ) 
vlaues
(#{username}, #{password}, #{email}) 
<insert>    

对不支持自动生成功能的数据库,mybatis提供以下写法,不过,此写法生成的ID是随机的
<insert  id=”insertAuthor” parameterType=“类型”> 
<selectKey resultType="java.lang.Long" order="AFTER" keyProperty="id">   
        SELECT LAST_INSERT_ID() AS id   
    </selectKey> 
insert into 
       数据表名(id, username, password, email ) 
values  
(#{id},#{username},#{password},#{email}) 
</insert> 

② Mybaits-SQL语句包含: 
<sql id=”Columns”> select/update/delete/insert 等操作</sql>
<select id=”selectUser” parameterType=int” resultType=”hashMap”>    
select  
<include  refid=”Columns”/>       
from  
表名
where 
id=#{id} 
</select>

③ Mybatis动态SQL语句 

a、if语句 
select 字段名 
from  表名 
where  state=1<if test =”字段名!=null>          
AND 条件      
</if>

b、choose, when , otherwise 
    select 字段名 from 表名  
where  state=1<choose>  
        <when test=”字段名!=null>
And 条件          
</when> 
        <when test=”条件表达式”>             
And 条件          
</when>          
<otherwise >               
And 条件           
</otherwise>        
</choose> 

c、trim, where , set      

1, where 
Select 字段名 from 表名             
<where>               
条件           
</where> 
注 : 加 <where> 后则确保一定是 where 开头

2, set 
Update  表名          
<set> 
        <if test=”条件”>
字段名=#{参数}
</if>
</set> 
    Where  条件 

d、foreach 通常构建在in条件中 
Select  字段名  from 表名             
Where 字段名 in  
<foreach  item=”参数名” index=index” collection=”list” Open=”(” separator=”,” close=”)” >
#{参数名} 
</foreach>   

e、作用例:批量删除  
     <delete id = "delete" parameterType = "java.util.List">   
        <![CDATA[   
           delete from tests where id in
]]>   
         <foreach collection="list" item = "要删除的id" open="(" separator="," close=")"> 
#{要删除的id}   
          </foreach>   
</delete>  

f、模糊查询: 
select 字段名 from 表名 where  字段名 like  "%"  #{参数}  "%" 

④ sql元素

Sql元素用来定义一个可以复用的SQL 语句段,供其它语句调用。

<!-- 复用sql语句 查询student表所有字段 -->
<sql id="selectStudentAll">
SELECT ST.STUDENT_ID,
ST.STUDENT_NAME,
ST.STUDENT_SEX,
ST.STUDENT_BIRTHDAY,
ST.CLASS_ID
FROM STUDENT_TBL ST 
</sql>

这样,在select的语句中就可以直接引用使用了

<!-- 查询学生,根据id -->   
<select id="getStudent" parameterType="String" resultMap="studentResultMap">   
<include refid="selectStudentAll"/>   
WHERE ST.STUDENT_ID = #{studentID}
</select>   

⑤ parameters 
MyBatis可以使用的基本数据类型和Java的复杂数据类型。         
基本数据类型包括String,int,date等。但是使用基本数据类型,只能提供一个参数,所以需要使用Java实体类或Map类型做参数类型。
通过#{}可以直接得到其属性。