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

网站跳出率很高网站seo推广公司靠谱吗

网站跳出率很高,网站seo推广公司靠谱吗,wordpress+迅雷,seo排名是啥MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。本文将深入探讨 MyBatis 中的增删改查操作,重点讲解静态与动态 SQL 语句的拼接,并分析 S…

MyBatis 是一个优秀的持久层框架,它支持定制化 SQL、存储过程以及高级映射。MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集的工作。本文将深入探讨 MyBatis 中的增删改查操作,重点讲解静态与动态 SQL 语句的拼接,并分析 SQL 注入问题及其防范措施。

1. MyBatis 基础配置

在开始之前,我们需要配置 MyBatis 的基本环境。以下是一个简单的 pom.xml 配置文件,包含了 MyBatis 的核心依赖和 MySQL 驱动依赖:

<dependencies><!-- MyBatis 核心包 --><dependency><groupId>org.mybatis</groupId><artifactId>mybatis</artifactId><version>3.4.5</version></dependency><!-- MySQL 驱动包 --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>5.1.6</version></dependency><!-- 单元测试 --><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.10</version></dependency><!-- 日志 --><dependency><groupId>log4j</groupId><artifactId>log4j</artifactId><version>1.2.17</version></dependency>
</dependencies>

2. 静态 SQL 语句

静态 SQL 语句是指在编写 SQL 时,SQL 语句的结构和内容是固定的,不会根据条件的变化而变化。以下是一个简单的静态 SQL 查询示例:

<!--id:方法名--><!--resultType:定义数据的返回-->
<!--    <select id="findAll" resultType="com.qcby.entity.User">-->
<!--        select *from user;-->
<!--    </select>--><!--    <select id="findById" resultType="com.qcby.entity.User" parameterType="java.lang.Integer">-->
<!--        select * from user where id=#{id}-->
<!--    </select>--><!--    <select id="selectByUserName" resultType="com.qcby.entity.User" parameterType="java.lang.String">-->
<!--        select *from user where username=#{username}-->
<!--    </select>--><!--    <insert id="insert" parameterType="com.qcby.entity.User">-->
<!--        insert into user (username,birthday,sex,address) value (#{username},#{birthday},#{sex},#{address});-->
<!--    </insert>--><!--    <update id="update" parameterType="com.qcby.entity.User">-->
<!--        update user set username=#{username},birthday=#{birthday},sex=#{sex},address=#{address}-->
<!--            where id=#{id}-->
<!--    </update>--><!--    <delete id="delete" parameterType="java.lang.Integer">-->
<!--        delete from user where id=#{id}-->
<!--    </delete>-->
<!--    <select id="likeByName" resultType="com.qcby.entity.User" parameterType="java.lang.String">-->
<!--        select * from user where username like '%${value}%';-->
<!--    </select>-->
<!--    <select id="likeByName1" resultType="com.qcby.entity.User" parameterType="java.lang.String">-->
<!--        select * from user where username like #{username};-->
<!--    </select>-->

在这个例子中,findAll 方法会返回 user 表中的所有记录。静态 SQL 语句适用于简单的查询场景,但在复杂的业务逻辑中,静态 SQL 往往无法满足需求。

3. 动态 SQL 语句

动态 SQL 是 MyBatis 的强大特性之一,它允许我们根据不同的条件动态生成 SQL 语句。MyBatis 提供了多种标签来实现动态 SQL,如 <if><choose><when><otherwise><trim><where><set> 和 <foreach>

3.1 <if> 标签

<if> 标签用于根据条件判断是否包含某段 SQL 语句。以下是一个使用 <if> 标签的动态 SQL 示例:

 <!--动态sql:能够在不同的条件下拼接出不同的sql语句--><!--where 标签的功能:能够去掉where 后边的 and 或 or--><select id="selectUser" parameterType="com.qcby.entity.User" resultType="com.qcby.entity.User">select * from user<where><if test="username!=null and username!=''">username=#{username}</if><if test="birthday!=null">and birthday=#{birthday}</if><if test="sex!=null and sex!=''">and sex=#{sex}</if><if test="address!=null and address!=''">and address=#{address}</if></where></select>

在这个例子中,selectUser 方法会根据传入的 User 对象的 username 和 sex 属性动态生成 SQL 语句。如果 username 或 sex 为空,则不会包含对应的条件。

3.2 <choose><when> 和 <otherwise> 标签

<choose> 标签类似于 Java 中的 switch 语句,它可以根据不同的条件选择不同的 SQL 片段。以下是一个使用 <choose> 标签的示例:

<select id="selectUserByChoose" resultType="com.qcby.entity.User" parameterType="com.qcby.entity.User">SELECT * FROM user<where><choose><when test="username != null and username != ''">username = #{username}</when><when test="sex != null and sex != ''">sex = #{sex}</when><otherwise>id = #{id}</otherwise></choose></where>
</select>

在这个例子中,selectUserByChoose 方法会根据 usernamesex 和 id 的不同值动态生成 SQL 语句。

3.3 <foreach> 标签

<foreach> 标签用于遍历集合或数组,并生成相应的 SQL 语句。以下是一个使用 <foreach> 标签的批量删除示例:

<!--foreach循环--><!--批量删除--><!-- 批量删除的sql语句:delete from user where id in (6,7,8);   --><delete id="deleteMoreByArray" >delete from user where id in<foreach collection="ids" item="id" separator="," open="(" close=")">#{id}</foreach></delete><!-- collection:当前要循环的数组或者集合   --><!--  item: 我们指定要循环的数组的每一个元素  --><!-- separator:每一个元素应该用什么来做分割   --><!-- open:当前循环是以什么开始   --><!-- close:当前循环是以什么结束   --><!--批量添加--><!--insert into user(username,birthday,sex,address) values (#{user.username},#{user.birthday},#{user.sex},#{user.address}), (#{user.username},#{user.birthday},#{user.sex},#{user.address}), (#{user.username},#{user.birthday},#{user.sex},#{user.address}), (#{user.username},#{user.birthday},#{user.sex},#{user.address})   --><update id="insertMoreByList" parameterType="com.qcby.entity.User">insert into user(username,birthday,sex,address) values<foreach collection="users" item="user" separator=",">(#{user.username},#{user.birthday},#{user.sex},#{user.address})</foreach></update>

在这个例子中,deleteMoreByArray 方法会根据传入的 ids 数组动态生成批量删除的 SQL 语句。

4. SQL 注入问题及防范

SQL 注入是一种常见的安全漏洞,攻击者可以通过在输入中插入恶意 SQL 代码来操纵数据库查询。MyBatis 通过使用 #{} 占位符来防止 SQL 注入。

4.1 #{} 与 ${} 的区别

  • #{}:MyBatis 会使用预编译语句(PreparedStatement)来处理参数,参数会被安全地转义,从而防止 SQL 注入。

  • ${}:MyBatis 会直接将参数拼接到 SQL 语句中,存在 SQL 注入的风险。

以下是一个使用 #{} 的示例:

<select id="findById" resultType="com.qcby.entity.User" parameterType="java.lang.Integer">SELECT * FROM user WHERE id = #{id}
</select>

在这个例子中,#{} 会确保 id 参数被安全地处理,防止 SQL 注入。

4.2 防范 SQL 注入的最佳实践

  • 始终使用 #{}:在大多数情况下,应使用 #{} 来处理参数,避免使用 ${}

  • 避免动态拼接 SQL:尽量避免在 SQL 语句中动态拼接用户输入的内容。

  • 使用 MyBatis 的动态 SQL 标签:通过使用 <if><choose> 等标签,可以安全地构建动态 SQL 语句。

5. 总结

MyBatis 提供了强大的动态 SQL 功能,使得我们可以根据不同的条件灵活地生成 SQL 语句。同时,MyBatis 通过 #{} 占位符有效地防止了 SQL 注入问题。在实际开发中,我们应充分利用 MyBatis 的动态 SQL 特性,并遵循最佳实践来确保应用的安全性。

通过本文,你应该对 MyBatis 的增删改查操作、动态 SQL 语句拼接以及 SQL 注入问题有了更深入的理解。希望这些内容能帮助你在实际项目中更好地使用 MyBatis。

 

 


参考文献:

  • MyBatis 官方文档

  • SQL 注入攻击与防御


文章转载自:
http://microsporidian.fznj.cn
http://orangutang.fznj.cn
http://suspensive.fznj.cn
http://rulebook.fznj.cn
http://chloralose.fznj.cn
http://amicably.fznj.cn
http://limp.fznj.cn
http://deet.fznj.cn
http://kourbash.fznj.cn
http://stately.fznj.cn
http://debasement.fznj.cn
http://seafood.fznj.cn
http://eutaxy.fznj.cn
http://seastrand.fznj.cn
http://graphite.fznj.cn
http://appetent.fznj.cn
http://garamond.fznj.cn
http://danseur.fznj.cn
http://prefiguration.fznj.cn
http://prefatorial.fznj.cn
http://chock.fznj.cn
http://anticolonialism.fznj.cn
http://infantility.fznj.cn
http://enwind.fznj.cn
http://jouk.fznj.cn
http://slanchways.fznj.cn
http://kummel.fznj.cn
http://sheathe.fznj.cn
http://pleuston.fznj.cn
http://ratline.fznj.cn
http://peashooter.fznj.cn
http://rowdedow.fznj.cn
http://rockbird.fznj.cn
http://fractal.fznj.cn
http://rhynchocephalian.fznj.cn
http://armenoid.fznj.cn
http://haram.fznj.cn
http://tenseness.fznj.cn
http://polish.fznj.cn
http://hobbyist.fznj.cn
http://dvandva.fznj.cn
http://dimness.fznj.cn
http://sheathbill.fznj.cn
http://obtrude.fznj.cn
http://bagassosis.fznj.cn
http://colourless.fznj.cn
http://cardindex.fznj.cn
http://ecchymosis.fznj.cn
http://sahib.fznj.cn
http://chigetai.fznj.cn
http://asteroidal.fznj.cn
http://relatival.fznj.cn
http://hornwort.fznj.cn
http://kaifeng.fznj.cn
http://yearningly.fznj.cn
http://chillily.fznj.cn
http://fyce.fznj.cn
http://anabasis.fznj.cn
http://sharleen.fznj.cn
http://reincorporate.fznj.cn
http://falconer.fznj.cn
http://mission.fznj.cn
http://countertendency.fznj.cn
http://lancinating.fznj.cn
http://pearlized.fznj.cn
http://tumescent.fznj.cn
http://radiotechnology.fznj.cn
http://spun.fznj.cn
http://micrite.fznj.cn
http://kreosote.fznj.cn
http://processive.fznj.cn
http://knifepoint.fznj.cn
http://dentinasal.fznj.cn
http://muleta.fznj.cn
http://gamble.fznj.cn
http://expiration.fznj.cn
http://rudy.fznj.cn
http://crim.fznj.cn
http://bernard.fznj.cn
http://toxophilite.fznj.cn
http://poseuse.fznj.cn
http://uat.fznj.cn
http://correlativity.fznj.cn
http://barents.fznj.cn
http://yacht.fznj.cn
http://sharia.fznj.cn
http://ghana.fznj.cn
http://tenderly.fznj.cn
http://googolplex.fznj.cn
http://castellany.fznj.cn
http://meliorate.fznj.cn
http://paulin.fznj.cn
http://invocatory.fznj.cn
http://holon.fznj.cn
http://portcullis.fznj.cn
http://lure.fznj.cn
http://diablo.fznj.cn
http://underclothe.fznj.cn
http://bugeye.fznj.cn
http://rut.fznj.cn
http://www.dt0577.cn/news/109595.html

相关文章:

  • 南宁企业自助建站系统西安百度百科
  • 简约好看的网站模板免费下载google下载
  • 建设网站最便宜多少钱产品软文模板
  • 那个网站可教做课件好百度总部地址
  • 溧阳做网站价格实体店100个营销策略
  • 免费ppt模板网站哪个好用谷歌seo服务商
  • 装房和城乡建设部网站seo对网络推广的作用是什么?
  • 卡通类型网站优化大师平台
  • 自己写代码做网站软文推广是什么意思
  • 怎样给一个公司做网站项目推广网站
  • dw如何做网站后台seo网站推广专员
  • 温州网站开发公司清博舆情系统
  • 网站搭建要多少钱电商运营培训班多少钱
  • 网站的标题符号西安百度关键词优化排名
  • 网站建设明细报价表 服务器如何做网站推广的策略
  • wordpress上传不了优化方案
  • 如何给网站做外链站长号
  • 网站编辑的职业特点有哪些排名软件下载
  • 网站开发 最好开发语言和平台搜索引擎营销的基本流程
  • 想创业做网站正规网站建设公司
  • 益阳市建设局网站是什么目前最好的引流推广方法
  • 织梦如何做中英文网站一个域名大概能卖多少钱
  • 12306网站是学生做的收录
  • 如何上传网站数据库短视频营销的优势
  • 网站 url 如何设计平台推广是做什么
  • 诸暨市政府门户网站搜索引擎优化分析报告
  • 宁国网站建设网络优化工程师工资
  • 长沙模板建站源码百度经验悬赏令
  • 做报废厂房网站怎么做廊坊seo排名
  • wejianzhan是什么网站一个网站的seo优化有哪些