MyBatis中的数据类型
简单类型包括:
java中的基本数据类型 (不包括boolean):byte、 short、 int、 long、 float、 double、 char
Java中的包装类型(不包括Boolean):Byte、 Short、 Integer、 Long、Float、 Double、 Character
Java当中的字符串类型:String
Java工具类中的日期类型:java.util.Date
MySql当中的日期类型:java.sql.Date
单个简单类型的参数:
在接口文件当中:
/**
* 当接口中的方法形参数只有一个(单个参数),并且参数的数据类型都是简单类型(上面列出的几种)。
* 根据id查询、name查询、birth查询、sex查询
*/
List<Student> selectById(Long id);
List<Student> selectByName(String name);
List<Student> selectByBirth(Date birth);
List<Student> selectBySex(Character sex);
在映射文件中:
<select id="selectById" resultType="Student" parameterType="long">
select * from t_student where id = #{id}
<!--
上面的sql语句写成下面的也是可以的,只是这样写读起来很费劲,应该见名知意,为此建议写成#{id}
select * from t_student where id = #{asdf}
-->
</select>
<select id="selectByName" resultType="student">
select * from t_student where name = #{name, javaType=String, jdbcType=VARCHAR}
</select>
<select id="selectByBirth" resultType="student">
select * from t_student where birth = #{birth}
</select>
<select id="selectBySex" resultType="student">
select * from t_student where sex = #{sex}
</select>
几点说明:
1、在#{}中所写的内容任意,但最好是见名知意;
2、在<select>标签中有一个属性是parameterType,是用这个属性来指定接口中方法的参数类型,这个属性值详细写应该是类型的全限定名称,简写可以写该类型的别名,MyBatis中内置了很多别名;另外mybatis框架自身带有类型自动推断机制,所以大部分情况下parameterType属性都是可以省略不写的(为此上面映射文件的sql语句中parameterType属性都可以省略)。
3、其实SQL映射⽂件中的配置⽐较完整的写法是:
<select id="selectByName" resultType="student" parameterType="java.lang.String">
select * from t_student where name = #{name, javaType=String, jdbcType=VARCHAR}
</select>
其中sql语句中的javaType(Java实体类中属性的数据类型),jdbcType(MySql中对应字段的数据类型),以及select标签中的parameterType属性,都是⽤来帮助mybatis进⾏类型确定的。不过这些配置多数是可以省略的。因为mybatis它有强⼤的⾃动类型推断机制。
javaType:可以省略
jdbcType:可以省略
parameterType:可以省略
单个Map类型的参数:
接口中的方法如下:
/**
* 保存学生信息,通过Map参数。以下是单个参数。但是参数的类型不是简单类型。是Map集合。
* @param map
* @return
*/
int insertStudentByMap(Map<String, Object> map);
在映射文件当中:
<!-- 在#{}中写的是map集合当中的key -->
<insert id="insertStudentByMap">
insert into t_student(id,name,age,sex,birth,height) values(null,#{姓名},#{年龄},#{性别},#{生日},#{身高})
</insert>
测试代码:
@Test
public void testInsertStudentByMap(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
Map<String,Object> map = new HashMap<>();
map.put("姓名", "赵六");
map.put("年龄", 20);
map.put("身高", 1.81);
map.put("性别", '男');
map.put("生日", new Date());
mapper.insertStudentByMap(map);
sqlSession.commit();
sqlSession.close();
}
单个POJO类型(实体类)参数:
接口中的方法:
/**
* 保存学生信息,通过POJO参数。Student是单个参数。但是不是简单类型。
* @param student
* @return
*/
int insertStudentByPOJO(Student student);
映射文件中:
<!-- 在#{}中写的是实体类的属性名 -->
<insert id="insertStudentByPOJO">
insert into t_student(id,name,age,sex,birth,height) values(null,#{name},#{age},#{sex},#{birth},#{height})
</insert>
测试代码:
@Test
public void testInsertStudentByPOJO(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
// POJO对象
Student student = new Student();
student.setName("张飞");
student.setAge(50);
student.setSex('女');
student.setBirth(new Date());
student.setHeight(10.0);
mapper.insertStudentByPOJO(student);
sqlSession.commit();
sqlSession.close();
}
多个参数:
接口中的方法:
/**
* 这是多参数。
* 根据name和sex查询Student信息。
* 如果是多个参数的话,mybatis框架底层是怎么做的呢?
* mybatis框架会自动创建一个Map集合。并且Map集合是以这种方式存储参数的:
* map.put("arg0", name);
* map.put("arg1", sex);
* ...
* 和
* map.put("param1", name);
* map.put("param2", sex);
* ...
* 以上两种形式同时存在,所以出现了可以混合使用的情况.
* @param name
* @param sex
* @return
*/
List<Student> selectByNameAndSex(String name, Character sex);
映射文件中:
<!--
注意:低版本的mybatis中,使用的是:#{0}、#{1}、#{2}...
高版本的mybatis中,使用的是:
第一种:
#{arg0} arg0 是第⼀个参数
#{arg1} arg1 是第⼆个参数
...
第二种:
#{param1} param1是第⼀个参数
#{param2} param2是第⼆个参数
…
-->
<select id="selectByNameAndSex" resultType="Student">
<!-- 以下这三种使用都没问题 -->
<!--select * from t_student where name = #{arg0} and sex = #{arg1}-->
<!--select * from t_student where name = #{param1} and sex = #{param2}-->
select * from t_student where name = #{arg0} and sex = #{param2}
</select>
测试代码:
@Test
public void testSelectByNameAndSex(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectByNameAndSex("张三", '男');
students.forEach(student -> System.out.println(student));
sqlSession.close();
}
使用@Param注解(命名参数)
接口文件中:
/**
* Param注解。
* mybatis框架底层的实现原理:
* map.put("name", name);
* map.put("sex", sex);
*
* @param name
* @param sex
* @return
*/
List<Student> selectByNameAndSexTwo(@Param("name") String name, @Param("sex") Character sex);
映射文件中:
<select id="selectByNameAndSexTwo" resultType="Student">
<!--使用了@Param注解之后,arg0和arg1失效了-->
<!--select * from t_student where name = #{arg0} and sex = #{arg1}-->
<!--使用了@Param注解之后,param1和param2还可以用-->
<!--select * from t_student where name = #{param1} and sex = #{param2}-->
select * from t_student where name = #{name} and sex = #{sex}
</select>
测试代码:
@Test
public void testSelectByNameAndSexTwo(){
SqlSession sqlSession = SqlSessionUtil.openSession();
StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
List<Student> students = mapper.selectByNameAndSex2("张三", '男');
students.forEach(student -> System.out.println(student));
sqlSession.close();
}