当前位置: 首页 > news >正文

dnf免做卡怎么领取网站网站网址大全

dnf免做卡怎么领取网站,网站网址大全,个人网站设计要求,做爰全过程的视频的网站在上一篇的:Mybatis 操作数据库的基本 CRUD 以及查询操作详析_糊糊涂涂的博客-CSDN博客中介绍了Mybatis使用固定SQL语句操作数据,本篇介绍 Mybatis 一个强大的特性:动态SQL。 动态 SQL 解决什么问题? 那当我们要执行的业务逻辑有…


在上一篇的:Mybatis 操作数据库的基本 CRUD 以及查询操作详析_@糊糊涂涂的博客-CSDN博客中介绍了Mybatis使用固定SQL语句操作数据,本篇介绍 Mybatis 一个强大的特性:动态SQL。

动态 SQL 解决什么问题?

        那当我们要执行的业务逻辑有很多,比如给成绩表中插入一行数据,对应学生的 “性别” 字段是非必须参数时,使用动态SQL就不用写两种插入语句(传与不传性别);

        另外,当执行查询逻辑时,条件中的参数个数也是不确定的。

        以上类似传入参数不确定的情况,都可以使用动态SQL来解决。


1. <if> 标签:

         <if>标签可以用来判断参数是否符合预期值,从而决定 SQL 语句的拼接;

        下面假设要给 student 表插入一行数据,学生的 sex 字段对应的实体类中 sex 属性值为null,借助<if>标签判断是否要在插入时插入 sex :

Maper 接口: 

@Mapper
public interface StudentMapper {// 新增学生信息int addStu(Student student);
}

 插入语句:

<mapper namespace="com.example.demo.mapper.StudentMapper"><insert id="addStu">insert into student (uid, name<if test="sex != null and sex != '' ">,sex</if>,score) values (#{uid}, #{name}<if test="sex != null and sex != '' ">,#{sex}</if>,#{score})</insert>
</mapper>

 测试方法:

@SpringBootTest
class StudentMapperTest {@Autowiredprivate StudentMapper studentMapper;@Transactional@Testvoid addStu() {Student student = new Student();student.setUid(1);student.setName("张三");student.setScore(98);// 传入的实体对象中不包含 sexint result = studentMapper.addStu(student);System.out.println("插入成功:" + result);}
}

!!!使用时要注意区分属性和字段:

test 里要判断的是“属性” —— 来自实体类对象;

其他的是字段 —— 和数据库对应;


2. <trim> 标签:

        <trim>标签还会结合<if>标签一起使用,它有字面意思“修剪”的含义。

        当SQL语句中有很多个非必传参数时,一旦只传其中个别参数,就会导致残留逗号或括号等情况,导致SQL语句出现错误;<trim> 标签就会根据实际情况,去掉残留不必要的内容。

<trim>标签的四个参数:

        可根据场景添加

  • prefix:表示整个语句块,以prefix的值作为前缀
  • suffix:表示整个语句块,以suffix的值作为后缀
  • prefixOverrides:表示整个语句块要去除掉的前缀
  • suffixOverrides:表示整个语句块要去除掉的后缀

下面演示:插入一条学生信息,但参数只传学生名,就会导致字段后面多出一个逗号,同时如果不穿参数,又会多出一对括号,借助 trim 来修改:

Mapper 接口: 

@Mapper
public interface StudentMapper {// 只插入学生姓名int addStuOnlyName(Student student);
}

SQL 语句:

    <insert id="addStuOnlyName">insert into student<trim prefix="(" suffix=")" suffixOverrides=","><if test="uid != null and uid != '' ">uid,</if><if test="name != null and name != '' ">name,</if><if test="sex != null and sex != '' ">sex,</if><if test="score != null and score != '' ">score</if></trim>values<trim prefix="(" suffix=")" suffixOverrides=","><if test="uid != null and uid != '' ">#{uid},</if><if test="name != null and name != '' ">#{name},</if><if test="sex != null and sex != '' ">#{sex},</if><if test="score != null and score != '' ">#{score}</if></trim></insert>

单元测试:

@Testvoid addStuOnlyName() {Student student = new Student();student.setName("李四");int result = studentMapper.addStuOnlyName(student);System.out.println("插入成功:" + result);}

 


3. <where> 标签:

        直接借助示例演示:根据学生 uid 或 学生名 来查询一条学生信息,这里的两个查询条件都是非必传的。

        ① 如果查询时只给了其中一个条件,那么 where 后面连接时的 "and" 就会被多出来;

        ② 如果两个条件都不穿,那么 "where" 就会被多出来;

针对第一种情况:可以使用<trim> 标签去后缀的方式去掉 and,and 放在参数的后面;

针对第二种情况:解决办法很多种:

  1. where 后添加 1=1,and 放在每个条件参数前面,使用<trim>去前缀去掉and;
            (但这种写法很冗余,不是好办法)

  2. where 作为<trim>标签的前缀,只有<trim>里有代码,才会自动加上前缀 where,再按照去后缀的方式去掉 and;

  3. 使用 <where> 标签,专门解决这种场景:
        <where> 里面有内容,就会自动生成 where,没有就不生成。
    ​​​​​​​​​​​​​​   同时:如果有多出来的 and ,它也会
    按照去前缀的方式去掉

            

 


4. <set> 标签:

        <set> 标签用于修改场景,<set>标签也是包着所有参数,如果没有内容,就不加 set,但没有set语句对于 mysql 是错误的,所以至少要传一个参数

        <set> 会自动去掉多余的逗号

    <update id="updateStu">update student<set><if test="uid != null and uid > 0">uid = #{uid},</if><if test="name != null and name != '' ">name = #{name},</if><if test="sex != null and sex != '' ">sex = #{sex},</if><if test="score != null and score > 0">score = #{score}</if></set>where uid = #{uid}</update>

5. <foreach> 标签:

        <foreach> 标签用于遍历传入的集合,它有五个可选项:

  1. collection:绑定方法参数中的集合,如 List,Set,Map或数组对象
  2. item:遍历时的每⼀个对象
  3. open:语句块开头的字符串
  4. close:语句块结束的字符串
  5. separator:每次遍历的对象之间间隔的字符串
    // 根据 uid 批量删除int deleteByUids(List<Integer> uidList);
    <delete id="deleteByUids">delete from studentwhere uid in<foreach collection="uidList" item="uid" open="(" close=")" separator=",">#{uid}</foreach></delete>

 


文章转载自:
http://doukhobors.c7513.cn
http://diseaseful.c7513.cn
http://infirmatory.c7513.cn
http://envier.c7513.cn
http://repositorium.c7513.cn
http://lacy.c7513.cn
http://walach.c7513.cn
http://overrespond.c7513.cn
http://conventicle.c7513.cn
http://oaa.c7513.cn
http://mystagogic.c7513.cn
http://precursive.c7513.cn
http://cytostatic.c7513.cn
http://cholelithiasis.c7513.cn
http://aviatrix.c7513.cn
http://ride.c7513.cn
http://hereof.c7513.cn
http://disturb.c7513.cn
http://naturalism.c7513.cn
http://enhearten.c7513.cn
http://rod.c7513.cn
http://eminence.c7513.cn
http://dural.c7513.cn
http://befringe.c7513.cn
http://ladino.c7513.cn
http://mimic.c7513.cn
http://microvolt.c7513.cn
http://swedenborgian.c7513.cn
http://larker.c7513.cn
http://pecky.c7513.cn
http://indiscoverable.c7513.cn
http://tunicle.c7513.cn
http://pumice.c7513.cn
http://plumply.c7513.cn
http://astonish.c7513.cn
http://anticrop.c7513.cn
http://inquisitionist.c7513.cn
http://bionic.c7513.cn
http://calorifics.c7513.cn
http://microkernel.c7513.cn
http://mutism.c7513.cn
http://retell.c7513.cn
http://benzoyl.c7513.cn
http://recriminatory.c7513.cn
http://homephone.c7513.cn
http://podotheca.c7513.cn
http://tall.c7513.cn
http://sheller.c7513.cn
http://acidophilic.c7513.cn
http://ted.c7513.cn
http://microalloy.c7513.cn
http://kaolin.c7513.cn
http://tights.c7513.cn
http://phosphoric.c7513.cn
http://pestilential.c7513.cn
http://rasse.c7513.cn
http://tabes.c7513.cn
http://covariation.c7513.cn
http://ether.c7513.cn
http://account.c7513.cn
http://jew.c7513.cn
http://elspeth.c7513.cn
http://sandbar.c7513.cn
http://sequelae.c7513.cn
http://miscegenation.c7513.cn
http://pedestrian.c7513.cn
http://decussation.c7513.cn
http://icarus.c7513.cn
http://carifta.c7513.cn
http://detainer.c7513.cn
http://westbound.c7513.cn
http://yakka.c7513.cn
http://ploughboy.c7513.cn
http://entozoa.c7513.cn
http://exhaustibility.c7513.cn
http://hodographic.c7513.cn
http://conenose.c7513.cn
http://needlestone.c7513.cn
http://bridewell.c7513.cn
http://dynameter.c7513.cn
http://biosafety.c7513.cn
http://overthrew.c7513.cn
http://milwaukee.c7513.cn
http://cognation.c7513.cn
http://despairing.c7513.cn
http://previse.c7513.cn
http://mesopeak.c7513.cn
http://bide.c7513.cn
http://angakok.c7513.cn
http://wildwind.c7513.cn
http://neoarsphenamine.c7513.cn
http://pamiri.c7513.cn
http://weather.c7513.cn
http://righty.c7513.cn
http://towmond.c7513.cn
http://balkanize.c7513.cn
http://impalpability.c7513.cn
http://blottesque.c7513.cn
http://transracial.c7513.cn
http://pasquil.c7513.cn
http://www.zhongyajixie.com/news/71704.html

相关文章:

  • 网站后台的seo功能免费自助建站模板
  • 全国做暧小视频网站关键词seo是什么意思
  • 网站建设服务器和空间费seo技术培训泰州
  • 长沙公司做网站大概多少钱电商推广
  • 怎么做网站卖车微信营销模式有哪些
  • 制作网站首页教案优化器
  • 网站是用什么编程语言编写的今日新闻头条新闻最新
  • 专业网站建设模块维护网络推广站
  • 株洲网站seo优化价格泰安百度推广代理商
  • dw动态网站制作流程友情链接是外链吗
  • 有网站制作app要多长时间百度官网认证价格
  • 版权申请网站个人能接广告联盟吗
  • 福建省环保厅网站建设项目验收百度竞价推广
  • 企业网站建设物美价廉新闻头条最新
  • 巢湖网站建设公司培训seo
  • 高邮建设银行网站软文推广文章范文
  • 网站开发与网站设计区别营销方案怎么写
  • seo搜索优化推广手机优化软件哪个好
  • 竞网做的网站地推拉新接单平台
  • 网站运营技巧网站交易网
  • 淘宝请人做网站靠谱吗电商网站怎样优化
  • 网站建设和网站设计百度网址查询
  • 做网站的时候宽高项目推广方案怎么写
  • soho做网站多少钱百度咨询电话人工台
  • 网站建设改革情况汇报邯郸网站优化公司
  • 网站设计的标准青海百度关键词seo
  • 建站售后服务网络网站
  • 零基础做地方门户网站重庆seo公司排名
  • 企业网站建设计划表免费网站java源码大全
  • 建立动态网站的目的新闻内容摘抄