做爰全國網(wǎng)站金融網(wǎng)站推廣圳seo公司
參數(shù)處理+語句查詢
- 1、簡單單個參數(shù)
- 2、Map參數(shù)
- 3、實體類參數(shù)
- 4、多參數(shù)
- 5、@Param注解
- 6、語句查詢
- 6.1 返回一個實體類對象
- 6.2 返回多個實體類對象 List<>
- 6.3 返回一個Map對象
- 6.4 返回多個Map對象 List<Map>
- 6.5 返回一個大Map
- 6.6 結(jié)果映射
- 6.6.1 使用resultMap
- 6.6.2 駝峰式映射
1、簡單單個參數(shù)
簡單類型包括:
● byte short int long float double char
● Byte Short Integer Long Float Double Character
● String
● java.util.Date
● java.sql.Date
總而言之就是 mybaits可以自動匹配參數(shù)類型,之后通過setXXX來注入。
我們也可以顯示標(biāo)注類型,省去mybatis的類型匹配。
比如 char 類 我們可以通過parameterType 來告訴mybatis 參數(shù)類型是什么,其他基本數(shù)據(jù)類型都一樣,不在舉例。
<select id="selectbysex" resultType="student" parameterType="java.lang.Character">select *from t_student where sex=#{sex}</select>
2、Map參數(shù)
注意的是 我們傳的如果是map,則我們#{map的key值},不能是其他的。
<select id="selectBYmap" resultType="student">select * from t_student where name=#{namekey} and age=#{agekey}</select>
@Testpublic void testMap(){SqlSession sqlSession = MybatisUtils.openSession();StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);Map<String,Object> map=new HashMap<>();map.put("namekey","cky");map.put("agekey",18);mapper.selectBYmap(map).forEach(student -> System.out.println(student));sqlSession.close();}
3、實體類參數(shù)
注意:如果我們傳的是實體類,則#{},{}里應(yīng)該是實體類的屬性名,不能是其他。
<select id="selectByclass" resultType="student">select * from t_student where name=#{name} and age=#{age}</select>
@Testpublic 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、多參數(shù)
傳入多參數(shù)時,其實mybatis底層是幫我們封裝成了map集合。
使用arg
List<Student> selectNameandSex2(String name, Character sex);
<select id="selectNameandSex2" resultType="student">select * from t_student where name=#{arg0} and sex=#{arg1}</select>
@Testpublic 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 id="selectNameandSex2" resultType="student">select * from t_student where name=#{param1} and sex=#{param2}</select>
兩者聯(lián)合使用
<select id="selectNameandSex2" resultType="student">select * 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參數(shù)從1開始。
兩個都在map中。
5、@Param注解
如果我們想要使用自己標(biāo)注的名字,就要使用@Param注解。
List<Student> selectNameandSex(@Param("name1") String name,@Param("sex1") Character sex);
<select id="selectNameandSex" resultType="student">select * from t_student where name=#{name1} and sex=#{sex1}</select>
使用了param注解,底層也是幫我們封裝成了map集合,但是是將我們自己定義的名字封裝為key,且這里argx不能再用,但是paramx仍可以使用。
就相當(dāng)于幫我們封裝成
map(“param1”,“cky”);map(“param2”,18);map(“name1”,“cky”);map(“sex1”,18);
6、語句查詢
6.1 返回一個實體類對象
根據(jù)id查找時,我們查找的對象正好有對應(yīng)的實體類,則我們可以直接返回一個實體類對象
<select id="selectByid" resultType="car">select * from t_car where id=#{id}</select>
@Testpublic 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="selectAllCar" resultType="Car">select id,car_num,brand,guide_price,produce_time,car_type from t_car ;</select>
@Testpublic void tesr(){SqlSession sqlSession = MybatisUtils.openSession();//getMapper() 參數(shù)傳入我們要代理的接口類 之后底層 會調(diào)用javassist 自動幫助我們生成 實現(xiàn)類 并將實現(xiàn)類返回 我們可以直接調(diào)用接口類的方法CarMapper mapper = sqlSession.getMapper(CarMapper.class);List<Car> cars = mapper.selectAllCar();cars.forEach(car -> System.out.println(car));sqlSession.close();}
6.3 返回一個Map對象
如果我們返回的對象在我們的項目中沒有對應(yīng)的實體類的話,我們可以使用map
Map<String,Object> selectByID(int id);
<select id="selectByID" resultType="map">select * from t_car where id=#{id}</select>
@Testpublic void test1(){SqlSession sqlSession = MybatisUtils.openSession();//getMapper() 參數(shù)傳入我們要代理的接口類 之后底層 會調(diào)用javassist 自動幫助我們生成 實現(xiàn)類 并將實現(xiàn)類返回 我們可以直接調(diào)用接口類的方法CarMapper mapper = sqlSession.getMapper(CarMapper.class);Map<String, Object> map = mapper.selectByID(2);System.out.println(map);sqlSession.close();}
使用map接收時,其key就是數(shù)據(jù)庫的列名,并不是我們類的列名
6.4 返回多個Map對象 List
List<Map<String,Object>> selectAllCar();
<select id="selectAllCar" resultType="map">select * from t_car ;</select>
@Testpublic void tesr(){SqlSession sqlSession = MybatisUtils.openSession();//getMapper() 參數(shù)傳入我們要代理的接口類 之后底層 會調(diào)用javassist 自動幫助我們生成 實現(xiàn)類 并將實現(xiàn)類返回 我們可以直接調(diào)用接口類的方法CarMapper mapper = sqlSession.getMapper(CarMapper.class);List<Map<String, Object>> maps = mapper.selectAllCar();maps.forEach(car -> System.out.println(car));sqlSession.close();}
6.5 返回一個大Map
如果我們使用List
@MapKey("id")Map<Integer,Map<String,Object>> selectmyMap();
<select id="selectmyMap" resultType="map">select * from t_car</select>
@Testpublic void test2(){SqlSession sqlSession = MybatisUtils.openSession();//getMapper() 參數(shù)傳入我們要代理的接口類 之后底層 會調(diào)用javassist 自動幫助我們生成 實現(xiàn)類 并將實現(xiàn)類返回 我們可以直接調(diào)用接口類的方法CarMapper mapper = sqlSession.getMapper(CarMapper.class);Map<Integer, Map<String, Object>> integerMapMap = mapper.selectmyMap();System.out.println(integerMapMap);sqlSession.close();}
結(jié)果是一個大的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 結(jié)果映射
查詢結(jié)果的列名和java對象的屬性名對應(yīng)不上怎么辦?
● 第一種方式:as 給列起別名
● 第二種方式:使用resultMap進(jìn)行結(jié)果映射
● 第三種方式:是否開啟駝峰命名自動映射(配置settings)
不知道為什么 ,我沒有起過別名,也沒有進(jìn)行自動映射,但是如果我用一個實體類接收,他自動幫我轉(zhuǎn)成了實體類的屬性名。
6.6.1 使用resultMap
<!--resultMap:id:這個結(jié)果映射的標(biāo)識,作為select標(biāo)簽的resultMap屬性的值。type:結(jié)果集要映射的類??梢允褂脛e名。
-->
<resultMap id="carResultMap" type="car"><!--對象的唯一標(biāo)識,官方解釋是:為了提高mybatis的性能。建議寫上。--><id property="id" column="id"/><result property="carNum" column="car_num"/><!--當(dāng)屬性名和數(shù)據(jù)庫列名一致時,可以省略。但建議都寫上。--><!--javaType用來指定屬性類型。jdbcType用來指定列類型。一般可以省略。--><result property="brand" column="brand" javaType="string" jdbcType="VARCHAR"/><result property="guidePrice" column="guide_price"/><result property="produceTime" column="produce_time"/><result property="carType" column="car_type"/>
</resultMap><!--resultMap屬性的值必須和resultMap標(biāo)簽中id屬性值一致。-->
<select id="selectAllByResultMap" resultMap="carResultMap">select * from t_car
</select>
6.6.2 駝峰式映射
是否開啟駝峰命名自動映射
使用這種方式的前提是:屬性名遵循Java的命名規(guī)范,數(shù)據(jù)庫表的列名遵循SQL的命名規(guī)范。
Java命名規(guī)范:首字母小寫,后面每個單詞首字母大寫,遵循駝峰命名方式。
SQL命名規(guī)范:全部小寫,單詞之間采用下劃線分割。
比如以下的對應(yīng)關(guān)系:
如何啟用該功能,在mybatis-config.xml文件中進(jìn)行配置:
<!--放在properties標(biāo)簽后面-->
<settings><setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>