本篇内容介绍了“Springboot整合MyBatis参数传值的方法”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
环境: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
主要是数据源 + MyBatis starter + pagehelper相关配置。
方式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>
根据方法的参数顺序param*。
或者:
<select id="queryUsers1" resultMap="usersMapper"> SELECT * FROM bc_users T where T.ID_NO = #{idNo} AND T.USERNAME LIKE CONCAT(CONCAT('%',#{username}), '%') </select>
直接写参数名称。
方式2:
通过@Param注解指明参数的名称
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:
通过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:
通过对象传参
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} 对象中必须有该属性对应的getter方法。
方式5:
List集合参数
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参数传值的方法”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。