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

创建官方网站新东方留学机构官网

创建官方网站,新东方留学机构官网,外包网站怎么做seo,平台型网站开发一、引言 目前互联网大厂在搭建后端Java服务时,常使用Springboot搭配Mybatis/Mybatis-plus的框架。Mybatis/Mybatis-plus之所以能成为当前国内主流的持久层框架,与其本身的优点有关:支持定制动态 SQL、存储过程及高级映射,简化数…

一、引言

目前互联网大厂在搭建后端Java服务时,常使用Springboot搭配Mybatis/Mybatis-plus的框架。Mybatis/Mybatis-plus之所以能成为当前国内主流的持久层框架,与其本身的优点有关:支持定制动态 SQL、存储过程及高级映射,简化数据库操作。

可能有人会问, 为什么要用动态SQL,在开发过程中把SQL写死不是比较方便、更加不容易出错吗?其实这是由开发过程中具体业务需求决定的,比如在用户注册场景下,用户注册填写信息时,会有必填字段和非必填字段,不同用户注册时传给后端的参数有区别,对应的插入用户表的SQL也不一样,因此,在这些的场景下开发人员需要使用动态SQL来完成。本文首先介绍Springboot项目中SQL映射方式,最后介绍动态SQL和支持动态SQL的核心标签。

欢迎关注工 众号:ItBeeCoder,查看更多高质量技术文章,发送“后端”获取资料

二、SQL映射方式

1、XML映射文件,该文件通常位于 resources/自定义的包路径/mapper 目录。

示例:

  <!-- UserMapper.xml --><mapper namespace="com.example.UserMapper"><select id="selectUserById" resultType="User">SELECT * FROM user WHERE id = #{id}</select></mapper>

2、注解映射

适用场景:简单 SQL 可直接在接口方法上使用注解。

示例:

public interface UserMapper {@Insert("INSERT INTO user(name) VALUES(#{name})")@Options(useGeneratedKeys = true, keyProperty = "id")void insertUser(User user);}

欢迎关注工 众号:ItBeeCoder,查看更多高质量技术文章,发送“后端”获取资料

三、动态SQL及常用标签

在 MyBatis中,动态SQL允许开发人员根据不同的条件构建不同的 SQL 语句。执行原理为,使用OGNL从SQL参数对象中计算表达式的值,根据表达式的值动态拼接SQL,以此来完成动态SQL的功能。动态SQL中常用的核心标签有:、、、、、等。

1、动态 SQL

  • 以下SQL为XML文件中复杂动态SQL的示例:
  <select id="searchUsers" resultType="User">SELECT * FROM user<where><if test="name != null">AND name LIKE CONCAT('%', #{name}, '%')</if><if test="roles != null">AND role IN<foreach item="role" collection="roles" open="(" separator="," close=")">#{role}</foreach></if></where></select>

2、常用标签

1)<if> 标签

a)用途:条件判断,满足条件时包含 SQL 片段。

b)示例

<select id="findUser" resultType="User">SELECT * FROM userWHERE 1=1<if test="name != null">AND name = #{name}</if></select>
2)<where> 标签

a)用途:自动添加 WHERE 关键字,并去除首条多余的 AND/OR

b)示例

<select id="selectByStudent" resultMap="BaseResultMap" parameterType="com.xxx.entity.Student">select<include refid="Base_Column_List" />from student<where><if test="name != null and name !=''">and name like concat('%', #{name}, '%')</if><if test="sex != null">and sex=#{sex}</if></where></select>

说明:以上SQL中,当条件都不满足时:此时 SQL 中应该要不能有 where , 否则导致出错。当 if 有条件满足时:SQL 中需要有 where, 且第一个成立的 if 标签下的 and | or 等要去掉,这时候,我们可以使用 where 标签。

3)<set> 标签

a)用途:主要用于SQL的UPDATE命令中,自动添加 SET 关键字,并去除末尾多余的逗号。

b)示例:

 <update id="updateUser">UPDATE user<set><if test="name != null">name = #{name},</if><if test="age != null">age = #{age},</if></set>WHERE id = #{id}</update>
4)<foreach> 标签

a)用途:遍历集合(如 IN 查询、批量插入)。

b)该标签的属性:

  - collection:集合参数名。- item:遍历元素的变量名。- open/close:包裹结果的前缀/后缀。- separator:元素间的分隔符。

c)示例:

<!-- IN 查询 -->SELECT * FROM user WHERE id IN<foreach item="id" collection="ids" open="(" separator="," close=")">#{id}</foreach><!-- 批量插入 -->INSERT INTO user (name) VALUES<foreach item="user" collection="list" separator=",">(#{user.name})</foreach>

注意:在使用标签时,最好在标签前加上标签,判断列表是否为null或者有数据,如果列表没数据,不加标签直接使用标签会报错。第一个查询SQL的动态SQL建议写法:

 SELECT * FROM user 
<if test = "ids != null and ids.size > 0">WHERE id IN
<foreach item="id" collection="ids" open="(" separator="," close=")">#{id}
</foreach>
</if>
5)<choose>/<when>/<otherwise> 标签

a)用途:多条件分支(功能类似于switch-case)。

b)示例:

<select id="findUser" resultType="User">SELECT * FROM user<where><choose><when test="name != null">AND name = #{name}</when><when test="email != null">AND email = #{email}</when><otherwise>AND is_active = 1</otherwise></choose></where></select>
6)<trim> 标签

a)用途:自定义字符串修剪(可替代 标签 或 标签)。

b)属性:

  - prefix:添加前缀。- prefixOverrides:去除首部匹配的字符。- suffix:添加后缀。- suffixOverrides:去除尾部匹配的字符。

c)示例:

<!-- 替代 <where> --><trim prefix="WHERE" prefixOverrides="AND |OR ">
<if test="name != null and name != ``">
AND name = #{name}
</if></trim><!-- 替代 <set> --><trim prefix="SET" suffixOverrides=","><if test="name != null">name = #{name},</if></trim>
7)<bind> 标签

a)用途:创建变量并绑定到上下文,用于复杂表达式或重复逻辑。

b)示例:

  <select id="searchUser"><bind name="pattern" value="'%' + keyword + '%'" />  <!-- 拼接模糊查询参数 -->SELECT * FROM userWHERE name LIKE #{pattern}</select>

欢迎关注工 众号:ItBeeCoder,查看更多高质量技术文章,发送“后端”获取资料

四、使用注意事项

1)OGNL表达式中:test 属性中使用的表达式语言,支持复杂逻辑,例如:==, !=, &&, || 等操作。例如:

<if test="name != null and name != ''">  <!-- 同时检查非空和非空字符串 -->

2)参数处理:

  • mapper接口中抽象方法的集合参数需通过 @Param 命名
List<User> findUsersByIds(@Param("ids") List<Integer> ids);
  • 空值检查需显式处理(如 test="name != null and name != ''")。

3)动态 SQL 性能:避免写过度复杂的动态SQL,过度复杂的SQL可能影响数据库执行计划,导致查询性能差。

4)动态表名/列名不建议使用 ${},建议使用#{},以防止 SQL 注入。

5)在Springboot搭配使用Mybatis时,为了能将Mapper接口上加了@Mapper@Dao的Bean注入到spring容器中,需要在启动类中添加@MapperScan注解,@MapperScan注解中的包路径名称为mapper接口的路径。如下所示:

  @SpringBootApplication@MapperScan("com.example.mapper")public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);// 其它业务代码}}

欢迎关注工 众号:ItBeeCoder,查看更多高质量技术文章,发送“后端”获取资料

五、一些最佳实践

1、在Mybatis的XML文件中对于某些频繁用到的SQL,为了避免重复写以及重复写导致的出错,可以引用某个常用的SQL,如下即为引用SQL的做法:

<sql id="all">
select * from user
</sql><select id="selectUserByUid" resultType="user">
<include refid="all"/>
where uid = #{uid}
</select><select id="selectIf" resultType="user">
<include refid="all"/>
<where>
<if test="username != null">
username = #{username}
</if>
</where>
</select>

2、判断list集合是否包含指定数据

<if test="list.contains('0')">
#{逻辑}
</if>

3、XML的SQL中比较符号的写法

gt 对应 >
gte 对应 >=
lt 对应 <(会报错 相关联的 "test" 属性值不能包含 '<' 字符)
lte 对应 <=(会报错 相关联的 "test" 属性值不能包含 '<' 字符)
<![CDATA[ sql 语句 ]]>
<![CDATA[ >= ]]>

4、MyBatis的XML中使用内部类的方式

内部类需要使用$符号连接,而不是点.,以下为正确写法:

com.xxx.model.SMSESBResult$ReceiveResult$ResultInfo

六、写在最后的话

总之,MyBatis的动态SQL功能允许开发者根据不同条件灵活构建SQL语句,避免手动拼接字符串,另外,常用查询列和查询SQL还可以借助动态SQL实现复用,提高XML中SQL代码的可维护性和安全性。



欢迎关注工 众号:ItBeeCoder,查看更多高质量技术文章,发送“后端”获取资料


又到了金三银四求职季,我整理了一些互联网大厂的面试题,有需要的可关注工 众号:ItBeeCoder,发送“后端”获取

在这里插入图片描述
在这里插入图片描述


文章转载自:
http://vitellogenic.sqLh.cn
http://unio.sqLh.cn
http://pluriglandular.sqLh.cn
http://insole.sqLh.cn
http://taste.sqLh.cn
http://argenteous.sqLh.cn
http://beatism.sqLh.cn
http://actionist.sqLh.cn
http://karnaugh.sqLh.cn
http://laboratorial.sqLh.cn
http://draughtsman.sqLh.cn
http://interplanetary.sqLh.cn
http://scyphistoma.sqLh.cn
http://indelicacy.sqLh.cn
http://coloured.sqLh.cn
http://reloan.sqLh.cn
http://pyrometamorphism.sqLh.cn
http://csf.sqLh.cn
http://quetzalcoatl.sqLh.cn
http://orrin.sqLh.cn
http://kilomegcycle.sqLh.cn
http://peal.sqLh.cn
http://underexpose.sqLh.cn
http://transferability.sqLh.cn
http://labrid.sqLh.cn
http://refractive.sqLh.cn
http://daimler.sqLh.cn
http://ambroid.sqLh.cn
http://chaetopod.sqLh.cn
http://mothproof.sqLh.cn
http://antimissile.sqLh.cn
http://usual.sqLh.cn
http://aspectual.sqLh.cn
http://correctitude.sqLh.cn
http://whetstone.sqLh.cn
http://infobahn.sqLh.cn
http://macropterous.sqLh.cn
http://gossipy.sqLh.cn
http://formerly.sqLh.cn
http://homogamy.sqLh.cn
http://xylocarp.sqLh.cn
http://staphyloplasty.sqLh.cn
http://incitant.sqLh.cn
http://limewater.sqLh.cn
http://prejudgment.sqLh.cn
http://aciduric.sqLh.cn
http://provocation.sqLh.cn
http://gentlepeople.sqLh.cn
http://based.sqLh.cn
http://shopworker.sqLh.cn
http://boney.sqLh.cn
http://idiodynamics.sqLh.cn
http://absurdity.sqLh.cn
http://pollinize.sqLh.cn
http://rhodinal.sqLh.cn
http://oxalacetic.sqLh.cn
http://concessionaire.sqLh.cn
http://semiblind.sqLh.cn
http://electrogenic.sqLh.cn
http://dodgeball.sqLh.cn
http://singletree.sqLh.cn
http://pierian.sqLh.cn
http://strontium.sqLh.cn
http://laker.sqLh.cn
http://ranchman.sqLh.cn
http://chemoautotrophic.sqLh.cn
http://semigroup.sqLh.cn
http://grundyism.sqLh.cn
http://nonterminating.sqLh.cn
http://quinquefoil.sqLh.cn
http://stut.sqLh.cn
http://spiciness.sqLh.cn
http://noncontact.sqLh.cn
http://unreeve.sqLh.cn
http://aragon.sqLh.cn
http://benedictory.sqLh.cn
http://predator.sqLh.cn
http://settltment.sqLh.cn
http://taxaceous.sqLh.cn
http://seignory.sqLh.cn
http://annihilative.sqLh.cn
http://heteronymously.sqLh.cn
http://pda.sqLh.cn
http://rockered.sqLh.cn
http://pseudopod.sqLh.cn
http://hhs.sqLh.cn
http://libel.sqLh.cn
http://pocketknife.sqLh.cn
http://saree.sqLh.cn
http://cerated.sqLh.cn
http://guam.sqLh.cn
http://palliative.sqLh.cn
http://turbofan.sqLh.cn
http://prompt.sqLh.cn
http://acini.sqLh.cn
http://monocotyledon.sqLh.cn
http://honkey.sqLh.cn
http://swoose.sqLh.cn
http://whichever.sqLh.cn
http://dipstick.sqLh.cn
http://www.15wanjia.com/news/81496.html

相关文章:

  • 专业网站设计联系电话免费友情链接网
  • 国内 上市网站建设公司模板建站网页
  • 买个域名后怎么做网站免费建网站知乎
  • oa管理系统项目文档中国十大seo公司
  • 广州软件开发廊坊seo网络推广
  • 网站设计公司石家庄国内新闻
  • 潍坊做外贸网站网络营销的10个特点
  • 上海网站开发设计公司贵州seo和网络推广
  • dreamweaver网站制作教程互联网营销师证书查询入口
  • 网站开发网页gif设计公司徐州seo外包
  • 慈溪做无痛同济 网站公司网页制作
  • 网站申请手游代理平台哪个好
  • 合肥的网站建设百度搜索排行榜风云榜
  • 上海网站建设 分类广告百度搜索推广官网
  • 做搜狗pc网站优化首惠州抖音seo策划
  • 一家只做代购的网站青岛 google seo
  • 做网站卖广告位赚钱谷歌优化技巧
  • vba可以做网站自动填现代营销手段有哪些
  • 深圳门户网站建设seo搜索引擎优化总结报告
  • 网站 流量攻击怎么办最全bt搜索引擎入口
  • wordpress图片添加音乐seo公司是做什么的
  • 那个网站专门做二手衣服的seo优化外包顾问
  • 用sql做简单的博客网站宝鸡seo
  • discuz怎么做网站地图国内seo公司排名
  • 做网站怎么做谷歌优化排名公司
  • 嘉兴网站建设品牌升级培训学校资质办理条件
  • 南京科技网站设计费用中国免费广告网
  • 做鞋用什么网站好求几个好看的关键词
  • 做游戏代练网站怎么做网站优化
  • 网络设计的步骤包括网站优化网络推广seo