mybatis模糊查詢、分頁(yè)和別名配置的方法
mybatis模糊查詢(3種)
第一種
select * from user where username like "%" #{name} "%"
第二種
select * from user where username like "%${value}%"
第三種
<!--concat拼接字符串 mysql獨(dú)有的函數(shù)--> select * from user where username like concat("%",#{username},"%")
全網(wǎng)都這樣說(shuō),但具體怎實(shí)現(xiàn)注入,我也不知道怎么搞。
- #{}是預(yù)編譯處理,${}是字符串替換。 Mybatis在處理#{}時(shí),會(huì)將sql中的#{}替換為?號(hào),調(diào)用PreparedStatement的set方法來(lái)賦值;
- Mybatis在處理 $ {}時(shí),就是把${}替換成變量的值。
- 使用#{}可以有效的防止SQL注入,提高系統(tǒng)安全性。
配置別名(typeAliases)
第一種方式:在mybatis-config.xml核心配置文件中的<configuration></configuration>
中添加:
<typeAlias type="com.lu.pojo.User" alias="User"/>
User可以用在任何使用 com.lu.pojo.User 的地方。
第二種方式:
也可以指定一個(gè)包名,MyBatis 會(huì)在包名下面搜索需要的 Java Bean,比如:
<typeAliases> <package name="com.lu.pojo"/> </typeAliases>
在沒(méi)有注解的情況下,會(huì)使用 Bean 的首字母小寫(xiě)的非限定類名來(lái)作為它的別名。 比如com.lu.pojo.User 的別名為 user ;若有注解,則別名為其注解值。比如:
@Alias("user") public class User{ ... }
屬性名和字段名不一致問(wèn)題
問(wèn)題:數(shù)據(jù)庫(kù)中密碼是pwd表示,實(shí)體類中是password表示。查詢結(jié)果會(huì)是null。
第一種方式:在sql語(yǔ)句中用as起別名。
<select id="getUserById" resultType="com.lu.pojo.User"> select id,username, pwd as password from user where id = #{id} </select>
第二種方式: 使用 resultMap進(jìn)行映射。
<!--結(jié)果集映射--> <resultMap id="userMap" type="com.lu.pojo.User"> <!--property是實(shí)體類中的屬性,column是數(shù)據(jù)庫(kù)中的字段 --> <result property="password" column="pwd" /> </resultMap> <select id="getUserById" resultMap="userMap"> select id,username, pwd from user where id = #{id} </select>
分頁(yè)查詢的3種方式
1、通過(guò)limit實(shí)現(xiàn)分頁(yè)
mapper接口:
//使用limit實(shí)現(xiàn)分頁(yè) List<User> getUserByLimit(Map<String,Integer> map);
Mapper.xml中:
<select id="getUserByLimit" parameterType="map" resultType="com.lu.pojo.User"> select * from user limit #{offset}, #{pageSize} </select>
測(cè)試方法:
@Test public void getUserByLimit(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); Map<String, Integer> map = new HashMap<String, Integer>(); map.put("offset",1); map.put("pageSize",2); List<User> users = userMapper.getUserByLimit(map); for (User user : users) { System.out.println(user); } sqlSession.close(); }
2、通過(guò)RowBounds實(shí)現(xiàn)分頁(yè)
mapper接口:
//通過(guò)RowBounds實(shí)現(xiàn)分頁(yè) List<User> getUserByRowBounds(RowBounds rowBounds);
Mapper.xml中:
<select id="getUserByRowBounds" resultType="com.lu.pojo.User"> select * from user </select>
測(cè)試方法:
@Test public void getUserByRowBounds(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); RowBounds rowBounds = new RowBounds(1, 2); List<User> users = userMapper.getUserByRowBounds(rowBounds); for (User user : users) { System.out.println(user); } sqlSession.close(); }
3、通過(guò)分頁(yè)插件pagehelper實(shí)現(xiàn)分頁(yè)
pom.xml中導(dǎo)入依賴:
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>4.1.6</version> </dependency>
mybatis-config.xml核心配置文件中添加:
<plugins> <plugin interceptor="com.github.pagehelper.PageHelper"> <property name="dialect" value="mysql"/> </plugin> </plugins>
mapper:
//通過(guò)pagehelper分頁(yè)插件實(shí)現(xiàn) List<User> getUserByPageHelper();
mapper.xml:
<select id="getUserByPageHelper" resultType="com.lu.pojo.User"> select * from user </select>
測(cè)試方法:
在類最上邊先import,否則PageHelper不能用
import com.github.pagehelper.PageHelper;
再寫(xiě)測(cè)試類:
@Test public void getUserByPageHelper(){ SqlSession sqlSession = MybatisUtils.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); PageHelper.startPage(1,3); List<User> users = userMapper.getUserByPageHelper(); for (User user : users) { System.out.println(user); } sqlSession.close(); }
使用Lombok步驟
1、在IDEA中安裝Lombok插件。
點(diǎn)擊左上角的 File —> 點(diǎn)擊 Settings —> 選擇 Pulgins —> 輸入 Lombok 搜索—> 點(diǎn)擊 Install 進(jìn)行安裝。如下圖
2、在項(xiàng)目中導(dǎo)入lombok的jar包
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency>
3、在實(shí)體類中添加注解
@NoArgsConstructor
:生成無(wú)參構(gòu)造器;
@AllArgsConstructor
:生成全參構(gòu)造器;
@Data
:作用于類上,是以下注解的集合:
@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
到此這篇關(guān)于mybatis模糊查詢、分頁(yè)和別名配置的文章就介紹到這了,更多相關(guān)mybatis模糊查詢、分頁(yè)和別名配置內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot自定義redis-starter的實(shí)現(xiàn)
這篇文章主要介紹了springboot自定義redis-starter的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10永中文檔在線轉(zhuǎn)換服務(wù)Swagger調(diào)用說(shuō)明
這篇文章主要為大家介紹了永中文檔在線轉(zhuǎn)換服務(wù)Swagger調(diào)用說(shuō)明,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-06-06SpringBoot中@ControllerAdvice注解的使用方法
這篇文章主要介紹了SpringBoot中@ControllerAdvice注解的使用方法,這是一個(gè)增強(qiáng)的?Controller,對(duì)controller層做異常處理、數(shù)據(jù)預(yù)處理、全局?jǐn)?shù)據(jù)綁定,?springboot?會(huì)自動(dòng)掃描到,不需要調(diào)用,這個(gè)注解是spring?MVC提供的,在springboot中也可以使用,需要的朋友可以參考下2024-01-01SpringBoot調(diào)用第三方接口的幾種方式小結(jié)
在項(xiàng)目中調(diào)用第三方接口時(shí),確實(shí)需要根據(jù)項(xiàng)目的技術(shù)棧、架構(gòu)規(guī)范以及具體的業(yè)務(wù)需求來(lái)選擇最適合的調(diào)用方式,下面我們就介紹幾種調(diào)用第三方接口的實(shí)現(xiàn)方式以及代碼示例,需要的朋友可以參考下2024-07-07Java實(shí)現(xiàn)Json字符串與Object對(duì)象相互轉(zhuǎn)換的方式總結(jié)
這篇文章主要介紹了Java實(shí)現(xiàn)Json字符串與Object對(duì)象相互轉(zhuǎn)換的方式,結(jié)合實(shí)例形式總結(jié)分析了java基于Json-Lib、Org.Json、Jackson、Gson、FastJson五種方式轉(zhuǎn)換json類型相關(guān)操作技巧,需要的朋友可以參考下2019-03-03Java 定時(shí)器的多種實(shí)現(xiàn)方式
本文介紹了Java中定時(shí)器的多種實(shí)現(xiàn)方式,有此需求的朋友可以根據(jù)實(shí)際選擇適合自己的方式2021-06-06