mybatis模糊查詢、分頁和別名配置的方法
mybatis模糊查詢(3種)
第一種
select * from user where username like "%" #{name} "%"
第二種
select * from user where username like "%${value}%"
第三種
<!--concat拼接字符串 mysql獨有的函數--> select * from user where username like concat("%",#{username},"%")
全網都這樣說,但具體怎實現注入,我也不知道怎么搞。
- #{}是預編譯處理,${}是字符串替換。 Mybatis在處理#{}時,會將sql中的#{}替換為?號,調用PreparedStatement的set方法來賦值;
- Mybatis在處理 $ {}時,就是把${}替換成變量的值。
- 使用#{}可以有效的防止SQL注入,提高系統(tǒng)安全性。
配置別名(typeAliases)
第一種方式:在mybatis-config.xml核心配置文件中的<configuration></configuration>
中添加:
<typeAlias type="com.lu.pojo.User" alias="User"/>
User可以用在任何使用 com.lu.pojo.User 的地方。
第二種方式:
也可以指定一個包名,MyBatis 會在包名下面搜索需要的 Java Bean,比如:
<typeAliases> <package name="com.lu.pojo"/> </typeAliases>
在沒有注解的情況下,會使用 Bean 的首字母小寫的非限定類名來作為它的別名。 比如com.lu.pojo.User 的別名為 user ;若有注解,則別名為其注解值。比如:
@Alias("user") public class User{ ... }
屬性名和字段名不一致問題
問題:數據庫中密碼是pwd表示,實體類中是password表示。查詢結果會是null。
第一種方式:在sql語句中用as起別名。
<select id="getUserById" resultType="com.lu.pojo.User"> select id,username, pwd as password from user where id = #{id} </select>
第二種方式: 使用 resultMap進行映射。
<!--結果集映射--> <resultMap id="userMap" type="com.lu.pojo.User"> <!--property是實體類中的屬性,column是數據庫中的字段 --> <result property="password" column="pwd" /> </resultMap> <select id="getUserById" resultMap="userMap"> select id,username, pwd from user where id = #{id} </select>
分頁查詢的3種方式
1、通過limit實現分頁
mapper接口:
//使用limit實現分頁 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>
測試方法:
@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、通過RowBounds實現分頁
mapper接口:
//通過RowBounds實現分頁 List<User> getUserByRowBounds(RowBounds rowBounds);
Mapper.xml中:
<select id="getUserByRowBounds" resultType="com.lu.pojo.User"> select * from user </select>
測試方法:
@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、通過分頁插件pagehelper實現分頁
pom.xml中導入依賴:
<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:
//通過pagehelper分頁插件實現 List<User> getUserByPageHelper();
mapper.xml:
<select id="getUserByPageHelper" resultType="com.lu.pojo.User"> select * from user </select>
測試方法:
在類最上邊先import,否則PageHelper不能用
import com.github.pagehelper.PageHelper;
再寫測試類:
@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插件。
點擊左上角的 File —> 點擊 Settings —> 選擇 Pulgins —> 輸入 Lombok 搜索—> 點擊 Install 進行安裝。如下圖
2、在項目中導入lombok的jar包
<dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> <scope>provided</scope> </dependency>
3、在實體類中添加注解
@NoArgsConstructor
:生成無參構造器;
@AllArgsConstructor
:生成全參構造器;
@Data
:作用于類上,是以下注解的集合:
@ToString @EqualsAndHashCode @Getter @Setter @RequiredArgsConstructor
到此這篇關于mybatis模糊查詢、分頁和別名配置的文章就介紹到這了,更多相關mybatis模糊查詢、分頁和別名配置內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot中@ControllerAdvice注解的使用方法
這篇文章主要介紹了SpringBoot中@ControllerAdvice注解的使用方法,這是一個增強的?Controller,對controller層做異常處理、數據預處理、全局數據綁定,?springboot?會自動掃描到,不需要調用,這個注解是spring?MVC提供的,在springboot中也可以使用,需要的朋友可以參考下2024-01-01Java實現Json字符串與Object對象相互轉換的方式總結
這篇文章主要介紹了Java實現Json字符串與Object對象相互轉換的方式,結合實例形式總結分析了java基于Json-Lib、Org.Json、Jackson、Gson、FastJson五種方式轉換json類型相關操作技巧,需要的朋友可以參考下2019-03-03