做外汇看什么网站,wordpress数据库教程,向雅虎提交网站,长沙优化官网收费标准参数处理#xff0b;语句查询 1、简单单个参数2、Map参数3、实体类参数4、多参数5、Param注解6、语句查询6.1 返回一个实体类对象6.2 返回多个实体类对象 List6.3 返回一个Map对象6.4 返回多个Map对象 ListMap6.5 返回一个大Map6.6 结果映射6.6.1 使用resultM… 参数处理语句查询 1、简单单个参数2、Map参数3、实体类参数4、多参数5、Param注解6、语句查询6.1 返回一个实体类对象6.2 返回多个实体类对象 List6.3 返回一个Map对象6.4 返回多个Map对象 ListMap6.5 返回一个大Map6.6 结果映射6.6.1 使用resultMap6.6.2 驼峰式映射 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 idselectbysex resultTypestudent parameterTypejava.lang.Characterselect *from t_student where sex#{sex}/select2、Map参数
注意的是 我们传的如果是map则我们#{map的key值}不能是其他的。 select idselectBYmap resultTypestudentselect * from t_student where name#{namekey} and age#{agekey}/selectTestpublic void testMap(){SqlSession sqlSession MybatisUtils.openSession();StudentMapper mapper sqlSession.getMapper(StudentMapper.class);MapString,Object mapnew HashMap();map.put(namekey,cky);map.put(agekey,18);mapper.selectBYmap(map).forEach(student - System.out.println(student));sqlSession.close();}3、实体类参数
注意如果我们传的是实体类则#{}{}里应该是实体类的属性名不能是其他。 select idselectByclass resultTypestudentselect * from t_student where name#{name} and age#{age}/selectTestpublic void testClass(){SqlSession sqlSession MybatisUtils.openSession();StudentMapper mapper sqlSession.getMapper(StudentMapper.class);Student studentnew 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 ListStudent selectNameandSex2(String name, Character sex);select idselectNameandSex2 resultTypestudentselect * from t_student where name#{arg0} and sex#{arg1}/selectTestpublic 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 idselectNameandSex2 resultTypestudentselect * from t_student where name#{param1} and sex#{param2}/select两者联合使用 select idselectNameandSex2 resultTypestudentselect * from t_student where name#{param1} and sex#{arg1}/select这里例子 就等同于帮我们封装了一个map集合 map(“arg0”,“cky”);map(“arg1”,18);map(“param1”,“cky”);map(“param2”,18); args从0开始param参数从1开始。 两个都在map中。
5、Param注解
如果我们想要使用自己标注的名字就要使用Param注解。 ListStudent selectNameandSex(Param(name1) String name,Param(sex1) Character sex);select idselectNameandSex resultTypestudentselect * from t_student where name#{name1} and sex#{sex1}/select使用了param注解底层也是帮我们封装成了map集合但是是将我们自己定义的名字封装为key且这里argx不能再用但是paramx仍可以使用。 就相当于帮我们封装成 map(“param1”,“cky”);map(“param2”,18);map(“name1”,“cky”);map(“sex1”,18);
6、语句查询
6.1 返回一个实体类对象
根据id查找时我们查找的对象正好有对应的实体类则我们可以直接返回一个实体类对象 select idselectByid resultTypecarselect * from t_car where id#{id}/selectTestpublic 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 idselectAllCar resultTypeCarselect id,car_num,brand,guide_price,produce_time,car_type from t_car ;/selectTestpublic void tesr(){SqlSession sqlSession MybatisUtils.openSession();//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法CarMapper mapper sqlSession.getMapper(CarMapper.class);ListCar cars mapper.selectAllCar();cars.forEach(car - System.out.println(car));sqlSession.close();}6.3 返回一个Map对象
如果我们返回的对象在我们的项目中没有对应的实体类的话我们可以使用map MapString,Object selectByID(int id);select idselectByID resultTypemapselect * from t_car where id#{id}/selectTestpublic void test1(){SqlSession sqlSession MybatisUtils.openSession();//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法CarMapper mapper sqlSession.getMapper(CarMapper.class);MapString, Object map mapper.selectByID(2);System.out.println(map);sqlSession.close();}使用map接收时其key就是数据库的列名并不是我们类的列名
6.4 返回多个Map对象 List ListMapString,Object selectAllCar();select idselectAllCar resultTypemapselect * from t_car ;/selectTestpublic void tesr(){SqlSession sqlSession MybatisUtils.openSession();//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法CarMapper mapper sqlSession.getMapper(CarMapper.class);ListMapString, Object maps mapper.selectAllCar();maps.forEach(car - System.out.println(car));sqlSession.close();}6.5 返回一个大Map
如果我们使用List的话我们如果想找一个idx的map就需要遍历找匹配值我们可以定义一个大的Map其key是每一个返回结果的id值其value是每一个查询结果。 MapKey(id)MapInteger,MapString,Object selectmyMap();select idselectmyMap resultTypemapselect * from t_car/selectTestpublic void test2(){SqlSession sqlSession MybatisUtils.openSession();//getMapper() 参数传入我们要代理的接口类 之后底层 会调用javassist 自动帮助我们生成 实现类 并将实现类返回 我们可以直接调用接口类的方法CarMapper mapper sqlSession.getMapper(CarMapper.class);MapInteger, MapString, Object integerMapMap mapper.selectmyMap();System.out.println(integerMapMap);sqlSession.close();}结果是一个大的map {2{car_num1000, id2, guide_price1000000.00, produce_time2000-11-11, brand宝马100, car_type燃油车}, 3{car_num102, id3, guide_price40.30, produce_time2014-10-05, brand丰田mirai, car_type氢能源}, 4{car_num102, id4, guide_price40.30, produce_time2014-10-05, brand丰田mirai, car_type氢能源}, 7{car_num1002, id7, guide_price100.00, produce_time2023-03-28, brand五菱11, car_type电车}, 8{car_num1000, id8, guide_price100.00, produce_time2024-04-09, brand1, car_typedianche}, 9{car_num1000, id9, guide_price100.00, produce_time2024-04-09, brand1, car_typedianche}}
6.6 结果映射
查询结果的列名和java对象的属性名对应不上怎么办 ● 第一种方式as 给列起别名 ● 第二种方式使用resultMap进行结果映射 ● 第三种方式是否开启驼峰命名自动映射配置settings
不知道为什么 我没有起过别名也没有进行自动映射但是如果我用一个实体类接收他自动帮我转成了实体类的属性名。
6.6.1 使用resultMap
!--resultMap:id这个结果映射的标识作为select标签的resultMap属性的值。type结果集要映射的类。可以使用别名。
--
resultMap idcarResultMap typecar!--对象的唯一标识官方解释是为了提高mybatis的性能。建议写上。--id propertyid columnid/result propertycarNum columncar_num/!--当属性名和数据库列名一致时可以省略。但建议都写上。--!--javaType用来指定属性类型。jdbcType用来指定列类型。一般可以省略。--result propertybrand columnbrand javaTypestring jdbcTypeVARCHAR/result propertyguidePrice columnguide_price/result propertyproduceTime columnproduce_time/result propertycarType columncar_type/
/resultMap!--resultMap属性的值必须和resultMap标签中id属性值一致。--
select idselectAllByResultMap resultMapcarResultMapselect * from t_car
/select6.6.2 驼峰式映射
是否开启驼峰命名自动映射 使用这种方式的前提是属性名遵循Java的命名规范数据库表的列名遵循SQL的命名规范。 Java命名规范首字母小写后面每个单词首字母大写遵循驼峰命名方式。 SQL命名规范全部小写单词之间采用下划线分割。 比如以下的对应关系 如何启用该功能在mybatis-config.xml文件中进行配置
!--放在properties标签后面--
settingssetting namemapUnderscoreToCamelCase valuetrue/
/settings