mysql+mybatis使用实例

17 篇文章 1 订阅
4 篇文章 0 订阅

一,给搜索结果添加序号

 

<select id="selectBigShotRankings" resultType="com.alibaba.fastjson.JSONObject"> 
    <![CDATA[ 
    	select (@i:=@i+1)i,s.* from (
         (select   ta.id,ta.name,ta.head_img,ta.mobile,ti.end_time,ti.memeber_type,ta.register_time
  from  ts_user_account ta
       JOIN ts_member_info ti  ON ta.id = ti.account_id 
       where 1=1 and big_shot=1 and ti.end_time  <now()
order by memeber_type desc,ta.register_time asc)
UNION
(select ta.id,ta.name,ta.head_img,ta.mobile ,ti.end_time,ti.memeber_type,ta.register_time
  from  ts_user_account ta
       JOIN ts_member_info ti  ON ta.id = ti.account_id 
       where 1=1 and big_shot=1 and (ti.end_time is null or  ti.end_time  > now() )
order by memeber_type desc,ta.register_time asc)
)s,(select  @i:=0)it 
    ]]>
</select>

二,添加返回主键

 

<insert id="insertSelective" parameterType="com.ssm.modules.entity.TsUserAccount"  useGeneratedKeys="true" keyProperty="id" keyColumn="id">
    insert into ts_user_account
<trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="id != null">
        id,
      </if>
      <if test="name != null">
        name,
     </if>
 </trim>
 <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="id != null">
        #{id,jdbcType=INTEGER},
      </if>
      <if test="name != null">
        #{name,jdbcType=VARCHAR},
      </if>
 </trim>
 </insert>
TsUserAccount record
int i = tsUserAccountMapper.insertSelective(record);
Integer id=record.getId();//主键Id

三,公共代码提取

<sql id="select">
   select  字段,字段... from 表
</sql>

<select >
   select * from (<include refid="select"/>)s3
</select >

四,mysql 查看表是否存在

   table_name  表名   no_worry  数据库名     如果没有,查询结果table_name 字段为null

SELECT table_name FROM information_schema.TABLES WHERE table_name ='ts_user' and table_schema="no_worry";

 

 

 

 

 

 

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值