参数处理+语句查询
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 id="selectbysex" resultType="student" parameterType="java.lang.Character">
select *from t_student where sex=#{sex}
</select>
2、Map参数
注意的是 我们传的如果是map,则我们#{map的key值},不能是其他的。
<select id="selectBYmap" resultType="student">
select * from t_student where name=#{namekey} and age=#{agekey}
</select>
@Test
public 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、实体类参数
注意:如果我们传的是实体类,则#{},{}里应该是实体类的属性名,不能是其他。
<select id="selectByclass" resultType="student">
select * from t_student where name=#{name} and age=#{age}
</select>
@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);