mybatis的mysql参数传递参数_6、SpringBoot+Mybatis整合------参数传递

开发工具:STS

前言:

在调用mybatis的查询条件时,之前,遇到需要验证多个参数的查询时,往往需要把所有参数都绑定到一个实体中去,然后调用获取。

现在,我们来详细描述mybatis传递参数的细节。

一、单个参数:

1.定义mapper接口:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm.mapper;2

3 importjava.util.List;4

5 importcom.xm.pojo.Student;6

7 public interfaceStudentMapper {8

9

10 /**

11 * 根据name查询12 *@paramname13 *@return

14 */

15 publicStudent getByName(String name);16

17 }

StudentMapper.java

2.实现mapper映射:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4

5

6

7

8

9 select * from student where name=#{name}10

11

StudentMapper.xml

3.定义测试用例:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm;2

3 importorg.junit.Test;4 importorg.junit.runner.RunWith;5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.boot.test.context.SpringBootTest;7 importorg.springframework.test.context.junit4.SpringRunner;8

9 importcom.xm.mapper.StudentMapper;10 importcom.xm.pojo.Student;11

12 @RunWith(SpringRunner.class)13 @SpringBootTest14 public classStudentTest {15 @Autowired16 privateStudentMapper studentMapper;17

18 @Test19 public voidselectStudent() {20

21 Student student = studentMapper.getByName("郭小明");22 System.out.println(student);23

24 }25

26

27 }

StudentTest.java

4.测试结果:

(1)数据库查询结果:

df96ef4a883e063f0985bd2c93e81edd.png

(2)测试结果输出:

8c0ad50d5f606313993d4a7f619628d9.png

注意在mapper映射中,单个参数类型,也可以不写 parameterType="",参数名可以随意写,比如#{names}

实现mapper映射:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4

5

6

7

8

9

10 select * from student where name=#{names}11

12

StudentMapper.xml

二、多个参数

1.根据参数名查询

(1)定义mapper接口:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm.mapper;2

3 importjava.util.List;4

5 importcom.xm.pojo.Student;6

7 public interfaceStudentMapper {8

9

10 /**

11 * 根据用户名和id同时查询12 *@paramid13 *@paramname14 *@return

15 */

16 publicStudent getStudentByIdAndName(Integer id,String name);17

18 }

StudentMapper.java

(2)实现mapper映射:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4

5

6

7

8 select * from student where name=#{name} and id=#{id}9

10

StudentMapper.xml

(3)定义测试用例:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm;2

3 importorg.junit.Test;4 importorg.junit.runner.RunWith;5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.boot.test.context.SpringBootTest;7 importorg.springframework.test.context.junit4.SpringRunner;8

9 importcom.xm.mapper.StudentMapper;10 importcom.xm.pojo.Student;11

12 @RunWith(SpringRunner.class)13 @SpringBootTest14 public classStudentTest {15 @Autowired16 privateStudentMapper studentMapper;17

18

19 @Test20 public voidselectStudent() {21

22 /*Student student = studentMapper.getByName("郭小明");*/

23 Student student = studentMapper.getStudentByIdAndName(1, "郭小明");24 System.out.println(student);25

26 }27

28

29 }

StudentTest.java

(4)测试结果:

7638b723868204d0516b98156d296c15.png

说明:这种简单的根据参数名传参是失败的

2.根据参数key值获取,获取规则为param1,param2,param3.........:

(1)实现mapper映射:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4

5

6

7

8

9 select * from student where name=#{param2} and id=#{param1}10

11

StudentMapper.xml

(2)测试结果:

9a4b44adaca126881e2f9d0342aeb801.png

说明:针对这种情况,如果参数较多的情况下,获取准确的参数名更好一些

3.绑定参数名

(1)定义mapper接口:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm.mapper;2

3 importjava.util.List;4

5 importorg.apache.ibatis.annotations.Param;6

7 importcom.xm.pojo.Student;8

9 public interfaceStudentMapper {10

11

12

13 /**

14 * 根据用户名和id同时查询15 *@paramid16 *@paramname17 *@return

18 */

19 public Student getStudentByIdAndName(@Param("id")Integer id,@Param("name")String name);20

21 }

StudentMapper.java

(2)实现mapper映射:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 <?xml version="1.0" encoding="UTF-8"?>

2

3

4

5

6

7

8 select * from student where name=#{name} and id=#{id}9

10

11

StudentMapper.xml

(3)测试结果:

84823fc619784485c49572961bec9cdf.png

说明:针对参数较多,且参数都属于同一个pojo类的时候,可以把参数先封装入实体,再获取

4.封装实体参数:

(1)定义mapper接口:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm.mapper;2

3 importjava.util.List;4

5 importorg.apache.ibatis.annotations.Param;6

7 importcom.xm.pojo.Student;8

9 public interfaceStudentMapper {10

11

12

13 /**

14 * 根据用户名和id同时查询15 *@paramid16 *@paramname17 *@return

18 */

19 publicStudent getStudentByIdAndName(Student student);20

21 }

StudentMapper.java

(2)定义测试用例:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

1 packagecom.xm;2

3 importorg.junit.Test;4 importorg.junit.runner.RunWith;5 importorg.springframework.beans.factory.annotation.Autowired;6 importorg.springframework.boot.test.context.SpringBootTest;7 importorg.springframework.test.context.junit4.SpringRunner;8

9 importcom.xm.mapper.StudentMapper;10 importcom.xm.pojo.Student;11

12 @RunWith(SpringRunner.class)13 @SpringBootTest14 public classStudentTest {15 @Autowired16 privateStudentMapper studentMapper;17

18 @Test19 public voidselectStudent() {20

21 /*Student student = studentMapper.getByName("郭小明");*/

22 /*Student student = studentMapper.getStudentByIdAndName(1, "郭小明");*/

23 Student student = newStudent();24 student.setName("郭小明");25 student.setId(1);26 Student student2 =studentMapper.getStudentByIdAndName(student);27 System.out.println(student2);28

29 }30

31

32 }

StudentMapper.java

(3)测试结果:

90b1b4493e907c4e0d057625436219df.png

2018-07-02

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值