MySQL(mysql)增删查改常用语句

  Mysbatis

like concat('%/',#{datePath,jdbcType=VARCHAR},'/%')

            concat函数:
               第一个参数 ,就是'%',其中%后边可以加上一些常量字符比如  / 。
               第二参数,基本上就是传递过来的参数 。
               第三个参数,是结尾的 '%', %前边可以加一定的常量字符比如  / 。

 

${_parameter} 和 #{_parameter} 是两种取值方式: 

“$” 是保持原生态,给什么就拼接什么 。 ”#“防止sql注入。

注解Insert插入数据返回自增id?

@Mapper
public interface UserMapper
{
	@Insert("insert into tbl_user (name, age) values (#{name}, #{age})")
	@Options(useGeneratedKeys=true, keyProperty="userId", keyColumn="id")
	void insertUser(User user);
} 

插入user到数据库时返回自增ID。数据插入成功后,id值被反填到user对象中,调用getUserId()就可以获取。

返回表字段和对象字段如何对应?

resultMap会去调用Bean的默认构造函数,然后将所有的成员变量和查询结果的列名形成映射,当你显示地指定了一个构造函数,然而它又不能将查询结果和显示指定的构造函数的参数形成映射的时候就会抛出异常

设置resultmap,或者指定对应的字段为驼峰命名法来对应。更新mybatis配置文件

/**
     * select info by taskId
     */
    @Select({
            "<script>",
            "select * from host_agentInstall where id=#{id}",
            "</script>"
    })
    @Results({
            @Result(column = "id", property = "id", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "install_ips", property = "installIps", jdbcType = JdbcType.VARCHAR),
            @Result(column = "insight_installDetail", property = "insightInstallDetail", jdbcType = JdbcType.LONGVARCHAR),
            @Result(column = "install_time", property = "installTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "is_installed", property = "isInstalled", jdbcType = JdbcType.INTEGER)
    })
    AgentInstallInfo selectInstallInfoById( Integer id);

mybatis 注解@update批量更新写法(解决多条语句批量更新报错问题)

 @Update({
            "<script>",
            "update host_info",
            "<trim prefix=\"set\" prefixOverrides=\",\">",
                    "<trim prefix ='is_managed = case' suffix='end'>" ,
                    "<foreach collection ='infoList' item ='info' index = 'index'> " ,
                    "when host_ip = #{info,jdbcType=VARCHAR} then 1 " ,
                    "</foreach>" ,
            "</trim> " ,
            "</trim> " ,
            "<where>",
            "<foreach collection='infoList' item='info' index='index' open='(' separator=')or(' close=' )'>",
            "host_ip=#{info,jdbcType=VARCHAR}",
            "</foreach>",
            "</where>",
            "</script>"
    })

上述语句mybatis解析结果:update host_info  set is_managed = case when host_ip = ? then 1    when host_ip = ? then 1 end    WHERE ( host_ip=? )or( host_ip=?  )

实际上是将多个记录sql 整合成一条语句插入,也是一种批量更新的写法

Mysql 外键关联

子表字段关联主表主键。形成外键,具有数据一致性(更新删除是一致,子表存在引用,则父表一起删除更新)。

DROP TABLE IF EXISTS `host_used_dir`;
CREATE TABLE host_used_dir (
  host_id int(11) NOT NULL COMMENT 'host_info表id',
  dir_name varchar(128) NOT NULL COMMENT '挂载目录路径',
  total_size int(11) NULL COMMENT '目录总空间,单位M',
  used_size int(11) NULL COMMENT '已使用空间,单位M'
)ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=UTF8_BIN;
alter table host_used_dir add constraint FK_ID foreign key (host_id) references host_info (id) ON DELETE CASCADE;
alter table host_used_dir add constraint FK_ID foreign key (host_id) references host_info (id) ON UPDATECASCADE;

Mysql left join right join

  "select a.*,b.host_ip from host_used_dir  as a left join host_info as b on a.host_id=b.id"

left join指将右表的数据加到左边的表查询结果,right join 则相反

Mabatis 批量条件查询

   "<script>",
            "select a.*,b.host_ip,b.host_ip,b.room_id,b.city_id from host_used_dir  as a left join host_info as b on a.host_id=b.id where b.host_ip in",
            "<foreach collection='ips' item='ip' index='index' open='(' separator=',' close=' )'>",
            "#{ip}",
            "</foreach>",
            "</script>"

批量联表查询,注意where中查询条件为and,和","相对应

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值