国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

mybatis的動(dòng)態(tài)SQL和模糊查詢實(shí)例詳解

瀏覽:155日期:2023-10-19 16:04:13

現(xiàn)在以一個(gè)例子來介紹mybatis的動(dòng)態(tài)SQL和模糊查詢:通過多條件查詢用戶記錄,條件為姓名模糊匹配,并且年齡在某兩個(gè)值之間。

新建表d_user:

create table d_user( id int primary key auto_increment, name varchar(10), age int(3)); insert into d_user(name,age) values(’Tom’,12); insert into d_user(name,age) values(’Bob’,13); insert into d_user(name,age) values(’Jack’,18);

建表成功:

mybatis的動(dòng)態(tài)SQL和模糊查詢實(shí)例詳解

新建實(shí)體類User:

public class User { private Integer id; private String name; private Integer age;//getters and setters @Override public String toString() { return 'User [id=' + id + ', name=' + name + ', age=' + age + ']'; } public User(Integer id, String name, Integer age) { super(); this.id = id; this.name = name; this.age = age; } public User() { super(); }}

創(chuàng)建查詢條件實(shí)體類ConditionUser:

public class ConditionUser { private String name; private int minAge; private int maxAge;//getters and setters public ConditionUser(String name, int minAge, int maxAge) { super(); this.name = name; this.minAge = minAge; this.maxAge = maxAge; } public ConditionUser() { super(); }}

新建映射文件userMapper.xml:

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='com.mybatis.test7.userMapper'> <select parameterType='ConditionUser' resultType='User'> SELECT * FROM d_user WHERE age &gt;= #{minAge} AND age &lt;= #{maxAge} <if test='name!=null'> AND name LIKE CONCAT(CONCAT(’%’,#{name}),’%’)</if> </select></mapper>

編寫測(cè)試類:

public class Test { private SqlSessionFactory sessionFactory; private SqlSession session; @Before public void init(){ //讀取配置文件 String resource = 'conf.xml'; InputStream is = this.getClass().getClassLoader().getResourceAsStream(resource); //創(chuàng)建SqlSessionFactory和SqlSession sessionFactory = new SqlSessionFactoryBuilder().build(is); session = sessionFactory.openSession(); } @After public void free(){ session.commit(); session.close(); } @org.junit.Test public void getUser() { String statement = 'com.mybatis.test7.userMapper'+'.getUser'; ConditionUser conditionUser = new ConditionUser('o', 13, 18); List<User> list = session.selectList(statement, conditionUser); System.out.println(list); }}

運(yùn)行結(jié)果:

mybatis的動(dòng)態(tài)SQL和模糊查詢實(shí)例詳解

注意:

1. 在配置文件中編寫sql語句時(shí),為防止大于號(hào)和小于號(hào)在表示大小關(guān)系和表示標(biāo)簽符號(hào)之間產(chǎn)生混淆,所以通常用&gt;和&lt;來代替sql語句中大于號(hào)和小于號(hào)。

2. 在SQL語句中添加動(dòng)態(tài)SQL標(biāo)簽if的原因是,當(dāng)在后臺(tái)獲取的name屬性值為null時(shí),防止生成where name like %null%的條件判斷語句,正確的邏輯應(yīng)該是,當(dāng)傳來的name屬性值為null時(shí),取消此篩選條件,即不使用where name like ?的判斷條件。在mybatis中,可用的動(dòng)態(tài)SQL標(biāo)簽有:if,choose(when,otherwise),trim(where,set),foreach。

3. 在使用模糊查詢時(shí),拼接%+#{name}+%的方法有如下幾種:

(1).像上述例子中一樣,在SQL語句中使用CONCAT關(guān)鍵字。

(2).使用${}代替#{}:

<?xml version='1.0' encoding='UTF-8' ?><!DOCTYPE mapper PUBLIC '-//mybatis.org//DTD Mapper 3.0//EN' 'http://mybatis.org/dtd/mybatis-3-mapper.dtd'><mapper namespace='com.mybatis.test7.userMapper'> <select parameterType='ConditionUser' resultType='User'> SELECT * FROM d_user WHERE age &gt;= #{minAge} AND age &lt;= #{maxAge} <if test='name!=null'> AND name LIKE ’%${name}%’</if> </select></mapper>

注意,默認(rèn)情況下,使用#{}語法,MyBatis會(huì)產(chǎn)生PreparedStatement語句,并且安全地設(shè)置PreparedStatement參數(shù),這個(gè)過程中MyBatis會(huì)進(jìn)行必要的安全檢查和轉(zhuǎn)義。例如:

執(zhí)行SQL:select * from emp where name = #{employeeName}

參數(shù):employeeName=>Smith

解析后執(zhí)行的SQL:select * from emp where name = ?

執(zhí)行SQL:Select * from emp where name = ${employeeName}

參數(shù):employeeName傳入值為:Smith

解析后執(zhí)行的SQL:Select * from emp where name =Smith

綜上所述,${}方式可能會(huì)引發(fā)SQL注入的問題,同時(shí)也會(huì)影響SQL語句的預(yù)編譯,所以從安全性和性能的角度出發(fā),應(yīng)盡量使用#{}。當(dāng)需要直接插入一個(gè)不做任何修改的字符串到SQL語句中,例如在ORDER BY后接一個(gè)不添加引號(hào)的值作為列名,這時(shí)候就需要使用${}。

(3).在程序中拼接。

總結(jié)

到此這篇關(guān)于mybatis的動(dòng)態(tài)SQL和模糊查詢的文章就介紹到這了,更多相關(guān)mybatis動(dòng)態(tài)SQL模糊查詢內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Mybatis 數(shù)據(jù)庫
相關(guān)文章:
主站蜘蛛池模板: 舒城县| 黎城县| 尼玛县| 乌拉特后旗| 兰考县| 福贡县| 贵港市| 沾益县| 邻水| 漠河县| 北海市| 华池县| 原平市| 洛阳市| 澄江县| 重庆市| 静乐县| 新营市| 堆龙德庆县| 沈丘县| 桐城市| 昭通市| 西吉县| 竹溪县| 望谟县| 浦城县| 恩施市| 隆回县| 石狮市| 通山县| 石狮市| 涞源县| 宿州市| 龙海市| 玉龙| 肇庆市| 旌德县| 庆城县| 徐汇区| 同仁县| 禹城市|