后台sql

一. xml

用or做条件


<if test="resultCode == 'FAIL'">
    AND
    (t2.result_code !='SUCCESS'  OR t2.result_code  IS NULL)
</if>

利用集合,做一个in 赛选

新建立dto,包含字段isDel and ids

从ids中选取id进行遍历删除的标记,也可serviceImpl写相关代码进行foreach.

    <!--更新刪除-->
    <update id="delete" parameterType="com.greenchoice.search.api.dto.SpecialLiveinfoDeleteDto" >
        update t_special_live_info
        <if test="isDel!= null">
            set is_del = #{isDel}
        </if>
        where id in
        <foreach collection="ids" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

在in,使用的时候,使用  in (${tag}) ,但有注入风险

xml条件查询,也可查询所有(模糊关键词,时间区间,关键字判断)

selectStatus代表时间区间:预告——直播中——直播结束

满足条件查询

<!--查询所有-->
    <select id="findByType" parameterType="com.greenchoice.search.api.dto.SpecialLiveinfoQueryDto" resultMap="BaseResultMap">
        SELECT * FROM t_special_live_info
        <where> 1=1 and is_del = 0
            <if test="liveType!=null">
                and live_type = #{liveType}
            </if>
            <if test="liveTitle!=null">
                and live_title LIKE "%"#{liveTitle}"%"
            </if>
            <if test="selectStatus==0">
                <![CDATA[ and DATE_FORMAT(live_start_time, '%Y-%m-%d') >=  DATE_FORMAT(#{nowDate}, '%Y-%m-%d')]]>
            </if>
            <if test="selectStatus==1">
                <![CDATA[ and DATE_FORMAT(live_start_time, '%Y-%m-%d') <  DATE_FORMAT(#{nowDate}, '%Y-%m-%d') and DATE_FORMAT(live_close_time, '%Y-%m-%d') >  DATE_FORMAT(#{nowDate}, '%Y-%m-%d')]]>
            </if>
            <if test="selectStatus==2">
                <![CDATA[ and DATE_FORMAT(live_close_time, '%Y-%m-%d') <=  DATE_FORMAT(#{nowDate}, '%Y-%m-%d')]]>
            </if>


            <if test="startTime!=null and startTime.trim() neq ''">
            and date_format(create_time,'%Y-%m-%d %H:%i:%s') &gt;= str_to_date(#{startTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test="endTime!=null and endTime.trim() neq ''">
            and date_format(create_time,'%Y-%m-%d %H:%i:%s') &lt;= str_to_date(#{endTime},'%Y-%m-%d %H:%i:%s')
            </if>
        </where>
    order by sort asc
    </select>

模糊查询推荐使用,以下形式;使用函数拼接  concat(‘%’,#{name}, ‘%’)

网文说到使用 '%${name}%' 也可,但不推荐使用

  • # 代表的是使用sql预编译方式,安全可靠-#{xx}会被替换成?
  • $ 代表着使用的是拼接方式,有SQL注入的风险

有些场景,并不能使用预编译方式,像一些代码重构,把表名/列名/排序等字段,动态传入(in like)的时候,不可避免的就需要SQL拼接的方式,SQL注入依然有搞头。

详情请参考此作者 参考文章

xml中条件的书写

<if test="goodsWStockQuery.sqlList != null and goodsWStockQuery.sqlList.size>0">  

<if test="goodsWStockQuery.keyWord != null and goodsWStockQuery.keyWord != ''">

<if test="tradeStatus == 'TRADE_CLOSED' ">

C.连表查询

左连接 右链接 内连接

    <!--订单商品便鱼订单地址连表查询-->
    <select id="findOrderGoodsAddress" resultType="com.greenchoice.order.api.dto.OrderGoodsAddressDto">
        SELECT
        t1.goods_name,t2.order_no,t2.contacts,t2.mobile,t2.create_user
        FROM
        t_order_goods t1
        INNER JOIN
        t_order_address t2
        ON
        t1.order_no = t2.order_no
        WHERE 1=1
        AND t1.order_no = t2.order_no
        <if test="goodsName !='' and goodsName != null">
            AND
            t1.goods_name=#{goodsName}+
        </if>
        <if test="orderNo !='' and orderNo != null">
            AND
            t2.order_no=#{orderNo}
        </if>
        <if test="contacts !='' and contacts != null">
            AND
            t2.contacts=#{contacts}
        </if>
        <if test="mobile !='' and mobile != null">
            AND
            t2.mobile=#{mobile}
        </if>
        <if test="createUser !='' and createUser != null">
            AND
            t2.create_user=#{createUser}
        </if>
    </select>

 新组成的的查询列表结构,需要新建立一个实体来封装接收。同字段的查询结构,会覆盖第一个,如两个表都有创建时间,需要获取到,然而字段又是一样的,这样只能封装到第一个,好像需要特殊指定才可以。

SELECT table1*,table2.CreateTime  CreateTime2 FROM t_pss_category WHERE### ;

可以这样子加个别名,在实体类中进行对应,将值封装到实体里CreateTime2 ,查询出来的值一般会按照数据库表的默认字段,进行显示,但当指定名后,会按照指定名进行显示,那么在查询出来进行封装的时候,也会按照你指定的别名进行封装。这样子就不会冲突,产生覆盖情况。

品类查询: 

品类数据库结构
品类ID1(00)编码0(为最高级)
品类ID2(0001)ID1+ID2品类ID1(00)编码
品类ID3(000101)ID1+ID2+ID3品类ID2(0001)编码

 

 

 

 

通过截取传递过来的CatCode商品编码,可以获取父类,当然子品类Code是基于父品类的CatCode上的,此查询用于最小级别查询所有级别。

    <!-- 通过品类编号查询该品类信息 -->
    <select id="findCategoryByCode" resultMap="getCategoryInfo">
        select a.*,
        (select cat_name from t_pss_category where mer_id = #{merId} and cat_code = substring(#{catCode},1,4)) parentCatName,      
        (select cat_name from t_pss_category where mer_id = #{merId} and cat_code = substring(#{catCode},1,2)) pparentCatName
        from t_pss_category a
        where 1=1 and a.is_delete = 0
        and a.cat_code = #{catCode}
        and mer_id = #{merId}
    </select>

 

二.Wrapper

1.查询

a.条件查询,也可查询所有

 @Override
    public List<GoodsEsLog> findAll(GoodsEsQuery goodsEsQuery) {
        QueryWrapper<GoodsEsLog> wrapper = new QueryWrapper<GoodsEsLog>();
        if (StringUtils.isNotBlank(goodsEsQuery.getLogsFlag())) {
            wrapper.eq("logs_flag", goodsEsQuery.getLogsFlag());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getLogsMsg())) {
            wrapper.eq("logs_msg", goodsEsQuery.getLogsMsg());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getLogsStatus())) {
            wrapper.eq("logs_status", goodsEsQuery.getLogsStatus());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getLogsType())) {
            wrapper.eq("logs_type", goodsEsQuery.getLogsType());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getParamIds())) {
            wrapper.eq("logs_paramIds", goodsEsQuery.getParamIds());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getLogsTime())) {
            wrapper.eq("date_format(logs_time,'%Y-%m-%d')", goodsEsQuery.getLogsTime());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getStartTime())) {
            wrapper.ge("date_format(logs_time,'%Y-%m-%d')", goodsEsQuery.getStartTime());
        }
        if (StringUtils.isNotBlank(goodsEsQuery.getEndTime())) {
            wrapper.le("date_format(logs_time,'%Y-%m-%d')", goodsEsQuery.getEndTime());
        }
        wrapper.orderByDesc("logs_time");
        List<GoodsEsLog> goodsEsLogList = goodsEsLogDao.selectList(wrapper);
        return goodsEsLogList;
    }

 注:关于时间范围条件查询,书写wapper,我测试到只能精确到日,不能精确到时分秒的范围查询,此需求使用的xml,Sql

三.注解

package cn.itcast.dao;
/*账户接口*/
@Repository
public interface AccountDao {
    //查询所有账户
    @Select("select * from account")
    public List<Account> findAll();
 
    //保存账户信息
    @Insert("insert into account (name,money) values(#{name},#{money})")
    public void saveAccount(Account account);
 
    //模糊查询
    @Select("select * from account where name like '%${name}%' ")
    public  List<Account> findAccountByNanem(Account account);
 
    //删除数据
    @Delete("delete  from account where id = #{id}")
    public void deleteAccountById(Account account);
 
    //修改数据
    @Update("update account set name=#{name},money=#{money} where id=#{id}")
    public void UpdateAccount(Account account);
 
    //查询数据byID
    //查询数据byID
    @Select("select * from account where id = #{id}")
    public Account findById(Account account);
}

四.数据库操作

1.表操作

1.1建表


CREATE TABLE Users(
    id INT(10) PRIMARY KEY AUTO_INCREMENT,
    username VARCHAR(20) ,
    Password Varchar(40),
    age INT(10),
    sex_id id(10),
    phone_number VARCHAR(50),
    login_id varchar(20),
    Location_id varchar(20)
)AUTO_INCREMENT=1;

1.2删除字段

alter table Users drop Log_id;

alter table Users modify Location_id Varchar(20);

alter table Location_login modify add id primary key;

drop table Location_login;

删除外键
ALTER TABLE Users drop foreign key sex_login11;

2.数据操作

2.1.插入

INSERT INTO Users VALUES (null,'张一', 'root',28, 1,'18845875861','001','001');
INSERT INTO Users VALUES (null,'张二','root', 27, 1,'18845875862','002','002');
INSERT INTO Users VALUES (null,'王大', 'root',22, 1,'18845875863','003','003');

2.2.删除

delete from Location_login where id=009;

2.3.修改

update Users set Login_id ='001' where id=1;

2.4.查询

select * from Users;

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值