参数处理+语句查询
1、简单单个参数2、Map参数3、实体类参数4、多参数5、@Param注解6、语句查询6.1 返回一个实体类对象6.2 返回多个实体类对象 List<>6.3 返回一个Map对象6.4 返回多个Map对象 List
1、简单单个参数
简单类型包括: ● byte short int long float double char ● Byte Short Integer Long Float Double Character ● String ● java.util.Date ● java.sql.Date
总而言之就是 mybaits可以自动匹配参数类型,之后通过setXXX来注入。 我们也可以显示标注类型,省去mybatis的类型匹配。 比如 char 类 我们可以通过parameterType 来告诉mybatis 参数类型是什么,其他基本数据类型都一样,不在举例。
select *from t_student where sex=#{sex}
2、Map参数
注意的是 我们传的如果是map,则我们#{map的key值},不能是其他的。
select * from t_student where name=#{namekey} and age=#{agekey}
@Test
public void testMap(){
SqlSession sqlSession = MybatisUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map
map.put("namekey","cky");
map.put("agekey",18);
mapper.selectBYmap(map).forEach(student -> System.out.println(student));
sqlSession.close();
}
3、实体类参数
注意:如果我们传的是实体类,则#{},{}里应该是实体类的属性名,不能是其他。
select * from t_student where name=#{name} and age=#{age}
@Test
public void testClass(){
SqlSession sqlSession = MybatisUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Student student=new Student();
student.setAge(18);
student.setId(10L);
student.setBirth(new Date());
student.setHeight(1.65);
student.setSex('女');
student.setName("c");
mapper.selectByclass(student).forEach(stu -> System.out.println(stu));
sqlSession.close();
}
4、多参数
传入多参数时,其实mybatis底层是帮我们封装成了map集合。
使用arg
List
select * from t_student where name=#{arg0} and sex=#{arg1}
@Test
public void testarg(){
SqlSession sqlSession = MybatisUtils.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
mapper.selectNameandSex2("cky",'女').forEach(student -> System.out.println(student));
sqlSession.close();
}
使用param
select * from t_student where name=#{param1} and sex=#{param2}
两者联合使用
select * from t_student where name=#{param1} and sex=#{arg1}
这里例子 就等同于帮我们封装了一个map集合 map(“arg0”,“cky”);map(“arg1”,18);map(“param1”,“cky”);map(“param2”,18); args从0开始,param参数从1开始。 两个都在map中。
5、@Param注解
如果我们想要使用自己标注的名字,就要使用@Param注解。
List
select * from t_student where name=#{name1} and sex=#{sex1}
使用了param注解,底层也是帮我们封装成了map集合,但是是将我们自己定义的名字封装为key,且这里argx不能再用,但是paramx仍可以使用。 就相当于帮我们封装成 map(“param1”,“cky”);map(“param2”,18);map(“name1”,“cky”);map(“sex1”,18);
6、语句查询
6.1 返回一个实体类对象
根据id查找时,我们查找的对象正好有对应的实体类,则我们可以直接返回一个实体类对象
select * from t_car where id=#{id}
@Test
public void testid1(){
SqlSession sqlSession = MybatisUtils.openSession();
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Car car = mapper.selectByid(2);
System.out.println(car);
sqlSession.close();
}
6.2 返回多个实体类对象 List<>
select id,car_num,brand,guide_price,produce_time,car_type from t_car ;
@Test
public void tesr(){
SqlSession sqlSession = MybatisUtils.openSession();
//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List
cars.forEach(car -> System.out.println(car));
sqlSession.close();
}
6.3 返回一个Map对象
如果我们返回的对象在我们的项目中没有对应的实体类的话,我们可以使用map
Map
select * from t_car where id=#{id}
@Test
public void test1(){
SqlSession sqlSession = MybatisUtils.openSession();
//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Map
System.out.println(map);
sqlSession.close();
}
使用map接收时,其key就是数据库的列名,并不是我们类的列名
6.4 返回多个Map对象 List
List
select * from t_car ;
@Test
public void tesr(){
SqlSession sqlSession = MybatisUtils.openSession();
//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
List
maps.forEach(car -> System.out.println(car));
sqlSession.close();
}
6.5 返回一个大Map
如果我们使用List的话,我们如果想找一个id=x的map就需要遍历找匹配值,我们可以定义一个大的Map,其key是每一个返回结果的id值,其value是每一个查询结果。
@MapKey("id")
Map
select * from t_car
@Test
public void test2(){
SqlSession sqlSession = MybatisUtils.openSession();
//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法
CarMapper mapper = sqlSession.getMapper(CarMapper.class);
Map
System.out.println(integerMapMap);
sqlSession.close();
}
结果是一个大的map {2={car_num=1000, id=2, guide_price=1000000.00, produce_time=2000-11-11, brand=宝马100, car_type=燃油车}, 3={car_num=102, id=3, guide_price=40.30, produce_time=2014-10-05, brand=丰田mirai, car_type=氢能源}, 4={car_num=102, id=4, guide_price=40.30, produce_time=2014-10-05, brand=丰田mirai, car_type=氢能源}, 7={car_num=1002, id=7, guide_price=100.00, produce_time=2023-03-28, brand=五菱11, car_type=电车}, 8={car_num=1000, id=8, guide_price=100.00, produce_time=2024-04-09, brand=1, car_type=dianche}, 9={car_num=1000, id=9, guide_price=100.00, produce_time=2024-04-09, brand=1, car_type=dianche}}
6.6 结果映射
查询结果的列名和java对象的属性名对应不上怎么办? ● 第一种方式:as 给列起别名 ● 第二种方式:使用resultMap进行结果映射 ● 第三种方式:是否开启驼峰命名自动映射(配置settings)
不知道为什么 ,我没有起过别名,也没有进行自动映射,但是如果我用一个实体类接收,他自动帮我转成了实体类的属性名。
6.6.1 使用resultMap
select * from t_car
6.6.2 驼峰式映射
是否开启驼峰命名自动映射 使用这种方式的前提是:属性名遵循Java的命名规范,数据库表的列名遵循SQL的命名规范。 Java命名规范:首字母小写,后面每个单词首字母大写,遵循驼峰命名方式。 SQL命名规范:全部小写,单词之间采用下划线分割。 比如以下的对应关系: 如何启用该功能,在mybatis-config.xml文件中进行配置:
精彩文章
发表评论