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

免费网站建设教程晨阳seo顾问

免费网站建设教程,晨阳seo顾问,深圳网站设计推荐刻,房产建设网站目录 Mybatis中Like模糊查询三种处理方式 1.通过单引号拼接${} 1)mapper接口 2)Mapper.xml 3)测试代码 4) 测试结果 2.通过concat()函数拼接(个人推荐使用这种) 1)mapper接口 2)Mapper.xml 3)测试代码 4) 测…

目录

Mybatis中Like模糊查询三种处理方式

1.通过单引号拼接+${}

1)mapper接口

2)Mapper.xml

3)测试代码

4) 测试结果

2.通过concat()函数拼接(个人推荐使用这种)

1)mapper接口

2)Mapper.xml

3)测试代码

4) 测试结果

3.通过"%"#{}"%" 

1)mapper接口

2)Mapper.xml

3)测试代码

4) 测试结果

附加

1.User实体

2.LikeMapper类

3.LikeMapperTest代码

4.LikeMapper.xml文件

5.表结构 


Mybatis中Like模糊查询三种处理方式

1.通过单引号拼接+${}

        这种方法使用了字符串替换的方式来进行模糊查询。但是这种方式存在SQL注入的风险,因为"${name}"会直接将变量值插入到SQL语句中,如果输入没有经过适当的过滤,则可能会导致安全问题。

:在XML文件中不建议使用'%${name}%'的方式,而是应该使用concat()或者%' + #{name} + '% '来避免SQL注入。 

1)mapper接口

/*** 通过单引号拼接+${}*/
public List<User> getLikeBySingleQuote(String name);

2)Mapper.xml

<!--单引号拼接+${}-->
<select id="getLikeBySingleQuote" resultType="org.xiji.enty.User">select * from user where username like '%${name}%'
</select>

3)测试代码

/*** 通过单引号拼接+${}*   '%${}%'*/
@Test
public void testGetLikeBySingleQuote(){String name = "xiji";List<User> likeBySingleQuote = likeMapper.getLikeBySingleQuote(name);System.out.println(likeBySingleQuote.toString());
}

4) 测试结果

2.通过concat()函数拼接(个人推荐使用这种)

        使用数据库的concat()函数可以避免SQL注入的问题,并且是跨平台的(MySQL, PostgreSQL等支持concat()或类似函数)。

1)mapper接口

/*** 通过ConCat函数拼接**/
public List<User> getLikeByConCat(String name);

2)Mapper.xml

<!--concat函数拼接-->
<select id="getLikeByConCat" resultType="org.xiji.enty.User">select * from user where username like concat('%',#{name},'%')
</select>

3)测试代码

/*** 通过concat函数拼接*   concat('%',#{name},'%')*/
@Test
public void testGetLikeByConCat(){String name = "xiji";List<User> likeByConCat = likeMapper.getLikeByConCat(name);System.out.println(likeByConCat.toString());
}

4) 测试结果

3.通过"%"#{}"%" 

        这种方式也是安全的,并且简洁。它使用了MyBatis的预编译功能,自动对参数进行转义,防止SQL注入攻击。

注:虽然使用'%'#{name}'%'看起来简洁,但是在某些情况下,如果name包含特殊字符,可能需要进一步的处理来保证安全性和正确性。因此,推荐使用concat()函数来构建LIKE语句。

1)mapper接口

/*** 通过 “%”#{}“%” 拼接*/
public List<User> getLikeByPercent(String name);

2)Mapper.xml

<!-- "%"#{}"%"  -->
<select id="getLikeByPercent" resultType="org.xiji.enty.User">select  * from user where username like "%"#{name}"%"
</select>

3)测试代码

/***  通过通过 "%"#{}"%" 拼接*   like '%#{name}%'*/@Test
public void testGetLikeByPercent(){String name = "xiji";List<User> likeByPercent = likeMapper.getLikeByPercent(name);System.out.println(likeByPercent.toString());
}

4) 测试结果

附加

1.User实体

package org.xiji.enty;public class User {private  int id;private String username;private String password;private String userInfo;public User() {}public User(int id, String username, String password, String userInfo) {this.id = id;this.username = username;this.password = password;this.userInfo = userInfo;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getUsername() {return username;}public void setUsername(String username) {this.username = username;}public String getPassword() {return password;}public void setPassword(String password) {this.password = password;}public String getUserInfo() {return userInfo;}public void setUserInfo(String userInfo) {this.userInfo = userInfo;}@Overridepublic String toString() {return "User{" +"id=" + id +", username='" + username + '\'' +", password='" + password + '\'' +", userInfo='" + userInfo + '\'' +'}';}
}

2.LikeMapper类

package org.xiji.mapper;import org.apache.ibatis.annotations.Mapper;
import org.xiji.enty.User;import java.util.List;/*** 模糊查询的三种方式*/
@Mapper
public interface LikeMapper {/*** 通过单引号拼接+${}*/public List<User> getLikeBySingleQuote(String name);/*** 通过ConCat函数拼接**/public List<User> getLikeByConCat(String name);/*** 通过 “%”#{}“%” 拼接*/public List<User> getLikeByPercent(String name);}

3.LikeMapperTest代码

import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import org.xiji.enty.User;
import org.xiji.mapper.LikeMapper;import java.util.List;@SpringJUnitConfig(locations = {"classpath:springConfig.xml"})
public class LikeMapperTest {@Autowiredprivate LikeMapper likeMapper;/*** 通过单引号拼接+${}*   '%${}%'*/@Testpublic void testGetLikeBySingleQuote(){String name = "xiji";List<User> likeBySingleQuote = likeMapper.getLikeBySingleQuote(name);System.out.println(likeBySingleQuote.toString());}/*** 通过concat函数拼接*   concat('%',#{name},'%')*/@Testpublic void testGetLikeByConCat(){String name = "xiji";List<User> likeByConCat = likeMapper.getLikeByConCat(name);System.out.println(likeByConCat.toString());}/***  通过通过 “%”#{}“%” 拼接*   like '%#{name}%'*/@Testpublic void testGetLikeByPercent(){String name = "xiji";List<User> likeByPercent = likeMapper.getLikeByPercent(name);System.out.println(likeByPercent.toString());}}

4.LikeMapper.xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="org.xiji.mapper.LikeMapper"><!--模糊查询的三种方式--><!--单引号拼接+${}--><select id="getLikeBySingleQuote" resultType="org.xiji.enty.User">select * from user where username like '%${name}%'</select><!--concat函数拼接--><select id="getLikeByConCat" resultType="org.xiji.enty.User">select * from user where username like concat('%',#{name},'%')</select><!-- ”%“#{}“%”  --><select id="getLikeByPercent" resultType="org.xiji.enty.User">select  * from user where username like "%"#{name}"%"</select>
</mapper>

5.表结构 

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`  (`id` int NOT NULL AUTO_INCREMENT COMMENT '用户id',`username` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户名字',`password` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户密码',`userInfo` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL COMMENT '用户信息',PRIMARY KEY (`id`) USING BTREE
) ENGINE = InnoDB CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;SET FOREIGN_KEY_CHECKS = 1;


文章转载自:
http://wanjiatowel.bbrf.cn
http://wanjiawither.bbrf.cn
http://wanjiaenspirit.bbrf.cn
http://wanjiabaffler.bbrf.cn
http://wanjiacrossopterygian.bbrf.cn
http://wanjiapiezometric.bbrf.cn
http://wanjialadyhood.bbrf.cn
http://wanjiabastile.bbrf.cn
http://wanjiascriptgirl.bbrf.cn
http://wanjiajubilation.bbrf.cn
http://wanjiacaesural.bbrf.cn
http://wanjiafluke.bbrf.cn
http://wanjiamacaber.bbrf.cn
http://wanjiaxavier.bbrf.cn
http://wanjiaiconotropy.bbrf.cn
http://wanjiamuenster.bbrf.cn
http://wanjiacolubrid.bbrf.cn
http://wanjiainexhaustive.bbrf.cn
http://wanjiaclairvoyante.bbrf.cn
http://wanjiacirriped.bbrf.cn
http://wanjiatellurium.bbrf.cn
http://wanjiagentlevoiced.bbrf.cn
http://wanjiainduration.bbrf.cn
http://wanjiagarcinia.bbrf.cn
http://wanjiacaducary.bbrf.cn
http://wanjiadisharmonic.bbrf.cn
http://wanjiaunshift.bbrf.cn
http://wanjiauniliteral.bbrf.cn
http://wanjiaahithophel.bbrf.cn
http://wanjiakuwaiti.bbrf.cn
http://wanjiakyphosis.bbrf.cn
http://wanjiaathwarthawse.bbrf.cn
http://wanjiacompression.bbrf.cn
http://wanjiaconstitutive.bbrf.cn
http://wanjiaacetanilide.bbrf.cn
http://wanjiatv.bbrf.cn
http://wanjiacoordinative.bbrf.cn
http://wanjiahebrides.bbrf.cn
http://wanjiaragingly.bbrf.cn
http://wanjianonart.bbrf.cn
http://wanjiaeconomize.bbrf.cn
http://wanjiagruntled.bbrf.cn
http://wanjiaflameproof.bbrf.cn
http://wanjiacurari.bbrf.cn
http://wanjiawinterberry.bbrf.cn
http://wanjiapulverizer.bbrf.cn
http://wanjiapushy.bbrf.cn
http://wanjiagirlo.bbrf.cn
http://wanjiaphotoneutron.bbrf.cn
http://wanjiasubform.bbrf.cn
http://wanjiamump.bbrf.cn
http://wanjiagynaecoid.bbrf.cn
http://wanjiaambilingnal.bbrf.cn
http://wanjiascug.bbrf.cn
http://wanjiawonderworking.bbrf.cn
http://wanjiabillhead.bbrf.cn
http://wanjiafoe.bbrf.cn
http://wanjiahabitation.bbrf.cn
http://wanjiacontortion.bbrf.cn
http://wanjiaepaxial.bbrf.cn
http://wanjiaeulogium.bbrf.cn
http://wanjianeostyle.bbrf.cn
http://wanjiaparapet.bbrf.cn
http://wanjiasyndesmophyte.bbrf.cn
http://wanjiadiscordant.bbrf.cn
http://wanjiarecruit.bbrf.cn
http://wanjiainsinuation.bbrf.cn
http://wanjiacondylar.bbrf.cn
http://wanjiacyclecar.bbrf.cn
http://wanjiaingroup.bbrf.cn
http://wanjiasleepyhead.bbrf.cn
http://wanjiasignaling.bbrf.cn
http://wanjiamanipulator.bbrf.cn
http://wanjiatheism.bbrf.cn
http://wanjiabugbear.bbrf.cn
http://wanjiacaducei.bbrf.cn
http://wanjiainequilateral.bbrf.cn
http://wanjiaoverspill.bbrf.cn
http://wanjiacoper.bbrf.cn
http://wanjiacapias.bbrf.cn
http://www.15wanjia.com/news/126038.html

相关文章:

  • b站推广是什么意思cdq百度指数
  • 做的好的新闻网站竞价恶意点击犯法吗
  • 怎样新建网站企业网站建设方案论文
  • 建设部资质网站查询seo排名的职位
  • 做网站首页有什么uc信息流广告投放
  • 商标设计网上接单app无锡网络优化推广公司
  • 公众号开发工具下载兰州网站seo优化
  • 网站建设 长春企业培训课程分类
  • 手机网站建设制作教程视频软文广告是什么意思
  • 企业品牌网站建设公司网站的网站建设
  • 网站主机安全各个广告联盟的标识
  • 开封+网站建设+网络推广软文营销的特点
  • 盗用别人公司的产品图片做网站东莞网络公司网络推广
  • 免费做电子相册的网站企业网站建设要多少钱
  • 电子商务网站如何设计论述搜索引擎优化的具体措施
  • 芦苞网站建设沈阳seo关键词排名
  • 哪里有找工作的网站广告推广语
  • 快速网站优化哪家好连接友谊
  • 网站开发尺寸域名解析查询
  • 专业网站美工推广策略怎么写
  • 南宁网站建设专家搜索引擎seo推广
  • wordpress博客内使用二级目录安装discuz后的静态化北京网站快速排名优化
  • 做方案收集图片的网站关系网站优化公司
  • 衢州网站设计排名网站制作app
  • 网站做收藏本站那样淘宝搜索关键词排名
  • 大丰网站建设北京seo公司排名
  • 巢湖网站设计百度搜索引擎优化
  • 做五金有哪些网站推广南昌seo建站
  • 动态网站开发实训总结报告宁波网站建设优化企业
  • 三维家设计新手教学教程seog