您好,登錄后才能下訂單哦!
本篇內(nèi)容介紹了“Springboot整合MyBatis參數(shù)傳值的方法”的有關(guān)知識(shí),在實(shí)際案例的操作過(guò)程中,不少人都會(huì)遇到這樣的困境,接下來(lái)就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!
環(huán)境:springboot2.3.9.RELEASE + MyBatis + MySQL
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.1.4</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.3.0</version> </dependency>
spring: datasource: driverClassName: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/testjpa?serverTimezone=GMT%2B8 username: root password: 123123 type: com.zaxxer.hikari.HikariDataSource hikari: minimumIdle: 10 maximumPoolSize: 200 autoCommit: true idleTimeout: 30000 poolName: MasterDatabookHikariCP maxLifetime: 1800000 connectionTimeout: 30000 connectionTestQuery: SELECT 1 --- spring: jpa: generateDdl: false hibernate: ddlAuto: update openInView: true show-sql: true --- pagehelper: helperDialect: mysql reasonable: true pageSizeZero: true offsetAsPageNum: true rowBoundsWithCount: true --- mybatis: type-aliases-package: com.pack.domain mapper-locations: - classpath:/mappers/*.xml
主要是數(shù)據(jù)源 + MyBatis starter + pagehelper相關(guān)配置。
方式1:
List<Users> queryUsers1(String idNo, String username) ;
<select id="queryUsers1" resultMap="usersMapper"> SELECT * FROM bc_users T where T.ID_NO = #{param1} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{param2}), '%') </select>
根據(jù)方法的參數(shù)順序param*。
或者:
<select id="queryUsers1" resultMap="usersMapper"> SELECT * FROM bc_users T where T.ID_NO = #{idNo} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{username}), '%') </select>
直接寫參數(shù)名稱。
方式2:
通過(guò)@Param注解指明參數(shù)的名稱
List<Users> queryUsers2(@Param("no")String idNo, @Param("un")String username) ;
<select id="queryUsers2" resultMap="usersMapper"> SELECT * FROM bc_users T where T.ID_NO = #{no} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{un}), '%') </select>
方式3:
通過(guò)Map傳參
List<Users> queryUsers3(Map<String, Object> params) ;
<select id="queryUsers3" resultMap="usersMapper" parameterType="hashmap"> SELECT * FROM bc_users T where T.ID_NO = #{id_no} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{user_name}), '%') </select>
這里的#{xxx} 就是存入Map中的Key。
方式4:
通過(guò)對(duì)象傳參
List<Users> queryUsers4(UsersDTO params) ;
public class UsersDTO extends ParamsDTO { private String idNo ; private String username ; public String getIdNo() { return idNo; } public void setIdNo(String idNo) { this.idNo = idNo; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } }
<select id="queryUsers4" resultMap="usersMapper" parameterType="com.pack.utils.UsersDTO"> SELECT * FROM bc_users T where T.ID_NO = #{idNo} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{username}), '%') </select>
這里的#{xxx} 對(duì)象中必須有該屬性對(duì)應(yīng)的getter方法。
方式5:
List集合參數(shù)
List<Users> queryUsers5(Collection<String> params) ;
<select id="queryUsers5" resultMap="usersMapper" parameterType="com.pack.utils.UsersDTO"> SELECT * FROM bc_users T where T.id in <foreach collection="params" open="(" separator="," close=")" item="id"> #{id} </foreach> </select>
@Mapper public interface UsersMapper { List<Users> queryUsers1(String idNo, String username) ; List<Users> queryUsers2(@Param("no")String idNo, @Param("un")String username) ; List<Users> queryUsers3(Map<String, Object> params) ; List<Users> queryUsers4(UsersDTO params) ; List<Users> queryUsers5(Collection<String> params) ; }
<mapper namespace="com.pack.mapper.UsersMapper"> <resultMap type="com.pack.domain.Users" id="usersMapper"> <id column="id" property="id"/> <id column="username" property="username"/> <id column="real_name" property="realName"/> <id column="create_time" property="createTime"/> <id column="status" property="status"/> <id column="authority" property="authority"/> <id column="id_no" property="idNo"/> </resultMap> <select id="queryUsers1" resultMap="usersMapper"> SELECT * FROM bc_users T where T.ID_NO = #{idNo} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{username}), '%') </select> <select id="queryUsers2" resultMap="usersMapper"> SELECT * FROM bc_users T where T.ID_NO = #{no} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{un}), '%') </select> <select id="queryUsers3" resultMap="usersMapper" parameterType="hashmap"> SELECT * FROM bc_users T where T.ID_NO = #{id_no} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{user_name}), '%') </select> <select id="queryUsers4" resultMap="usersMapper" parameterType="com.pack.utils.UsersDTO"> SELECT * FROM bc_users T where T.ID_NO = #{idNo} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{username}), '%') </select> <select id="queryUsers5" resultMap="usersMapper" parameterType="com.pack.utils.UsersDTO"> SELECT * FROM bc_users T where T.id in <foreach collection="params" open="(" separator="," close=")" item="id"> #{id} </foreach> </select> </mapper>
@Resource private UsersMapper usersMapper ; @GetMapping("/q1") public Object q1(UsersDTO params) { return R.success(Pager.query(params, () -> { return usersMapper.queryUsers1(params.getIdNo(), params.getUsername()) ; })) ; } @GetMapping("/q2") public Object q2(UsersDTO params) { return R.success(Pager.query(params, () -> { return usersMapper.queryUsers2(params.getIdNo(), params.getUsername()) ; })) ; } @GetMapping("/q3") public Object q3(UsersDTO params) { return R.success(Pager.query(params, () -> { Map<String, Object> ps = new HashMap<>() ; ps.put("id_no", params.getIdNo()) ; ps.put("user_name", params.getUsername()) ; return usersMapper.queryUsers3(ps) ; })) ; } @GetMapping("/q4") public Object q4(UsersDTO params) { return R.success(Pager.query(params, () -> { return usersMapper.queryUsers4(params) ; })) ; } @GetMapping("/q5") public Object q5(@RequestBody List<String> ids) { return R.success(Pager.query(new UsersDTO(), () -> { return usersMapper.queryUsers5(ids) ; })) ; }
“Springboot整合MyBatis參數(shù)傳值的方法”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注億速云網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。