MyBatis之自定义数据类型转换器
- 前言
- 1.parameterType
- 2.resultType
- 3.resultMap
- 实例代码
- 总结
前言
今天我们来学习Mapper.xml(编写SQL的)文件中,增删改查标签中,使用parameterType属性指定传递参数类型,resultType属性指定返回结果类型,resultMap标签完成当数据库表字段和Java实体类属性名或类型不一致时的手动映射,另外,SQL文中,where条件中只有一个参数时,推荐使用基本类型(int,short,char,byte,float,double,long,boolean)+String,当有多个参数时,使用Java实体类,接下来,我们来讲讲具体的用法。
1.parameterType
定义在增删改查标签中的属性,指定SQL文中where条件的参数类型,可以是基本数据类型(8个)+String+对象类型
例如,根据id查询name,如果数据库中定义的字段id是int类型,就可以指定parameterType的属性值是int,可以参看下边的代码。
< select id=“queryPersonNameById” parameterType=“int” resultType=“String”>
常用类型也可以参看下表
2.resultType
也是定义在增删改查标签中的属性,用于指定查询结果或增删改执行结果的类型,同样可以是基本数据类型+String+对象类型,这里需要注意的是,当查询结果是多条时,无需写List,MyBatis底层会自动为我们生成List<数据类型>,我们只需指定类型即可。
同样是上边的例子,根据id查询name,name在数据库中是varchar类型,resultType就指定成String类型,类型不区分大小写,尽量保持和Java一致,使用大写。
< select id=“queryPersonNameById” parameterType=“int” resultType=“String”>
3.resultMap
当实体类的属性名和数据库表字段名或类型不一致时使用
例如,Person表中,定义的字段是id和name
Java实体类中,定义的是pid和pname,这种情况下,使用resultMap手动完成数据库和Java实体类属性的映射。
它是单独的< resultMap>标签,定义id,通常使用在查询标签中
例如下边的代码
id标签完成数据库主键的映射
result标签完成数据库非主键字段的映射
property属性指定java实体类中的属性名,column是数据库字段名
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.mapper.PersonMapper">
<resultMap type="person" id="personMapping">
<id property="pid" column="id"/>
<result property="pname" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex"/>
</resultMap>
<select id="queryAllPersonUseResultMap" resultMap="personMapping">
select * from person
</select>
</mapper>
resultMap常用在数据库表字段和Java实体类类型不一致时,进行手动映射,
通常项目中使用别名的形式,进行映射
例如,下边实例代码中,pid和pname是id和name的别名,MyBatis会自动为我们完成Java实体类映射
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.mapper.PersonMapper">
<select id="queryAllPerson" resultType="person">
select id pid, name pname, age, sex from person
</select>
</mapper>
实例代码
PersonMapper.xml代码如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="xxx.xxx.mapper.PersonMapper">
<resultMap type="person" id="personMapping">
<id property="pid" column="id"/>
<result property="pname" column="name"/>
<result property="age" column="age"/>
<result property="sex" column="sex" javaType="boolean" jdbcType="INTEGER"/>
</resultMap>
<select id="queryPersonNameById" parameterType="int" resultType="string">
select name pname from person where id = #{id}
</select>
<select id="queryPersonCount" resultType="int">
select count(1) from person
</select>
<select id="queryAllPerson" resultType="person">
select id pid, name pname, age, sex from person
</select>
<select id="queryAllPersonUseResultMap" resultMap="personMapping">
select * from person
</select>
</mapper>
PersonMapper.java代码如下:
package xxx.xxx.mapper;
import java.util.List;
import xxx.xxx.pojo.Person;
public interface PersonMapper {
public List<Person> queryAllPersonUseResultMap();
public List<Person> queryAllPerson();
public int queryPersonCount();
public String queryPersonNameById(int id);
}
Person.java(实体类)代码如下:
package xxx.xxx.pojo;
public class Person {
private int pid;
private String pname;
private int age;
private boolean sex;
public Person() {
}
public Person(int pid, String pname, int age, boolean sex) {
this.pid = pid;
this.pname = pname;
this.age = age;
this.sex = sex;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public String getPname() {
return pname;
}
public void setName(String pname) {
this.pname = pname;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public boolean isSex() {
return sex;
}
public void setSex(boolean sex) {
this.sex = sex;
}
@Override
public String toString() {
return "Person [pid=" + pid + ", pname=" + pname + ", age=" + age + ", sex=" + sex + "]";
}
}
测试代码如下:
package xxx.xxx.test;
import java.io.IOException;
import java.io.Reader;
import java.util.List;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import xxx.xxx.mapper.PersonMapper;
import xxx.xxx.pojo.Person;
public class TestMyBatis {
public static void main(String[] args) throws IOException {
// queryAllPersonUseResultMap();
// queryAllPerson();
// queryPersonCount();
queryPersonNameById();
}
public static void queryAllPersonUseResultMap() throws IOException {
// 1.读取MyBatis配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2.实例化SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 3.实例化SqlSession
try (SqlSession session = sqlSessionFactory.openSession()) {
// 4.获取PersonMapper接口
PersonMapper personMapper = session.getMapper(PersonMapper.class);
// 5.执行SQL
List<Person> persons = personMapper.queryAllPersonUseResultMap();
persons.forEach(System.err::println);
}
}
public static void queryAllPerson() throws IOException {
// 1.读取MyBatis配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2.实例化SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 3.实例化SqlSession
try (SqlSession session = sqlSessionFactory.openSession()) {
// 4.获取PersonMapper接口
PersonMapper personMapper = session.getMapper(PersonMapper.class);
// 5.执行SQL
List<Person> persons = personMapper.queryAllPerson();
persons.forEach(System.err::println);
}
}
public static void queryPersonCount() throws IOException {
// 1.读取MyBatis配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2.实例化SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 3.实例化SqlSession
try (SqlSession session = sqlSessionFactory.openSession()) {
// 4.获取PersonMapper接口
PersonMapper personMapper = session.getMapper(PersonMapper.class);
// 5.执行SQL
int count = personMapper.queryPersonCount();
System.err.println(count);
}
}
public static void queryPersonNameById() throws IOException {
// 1.读取MyBatis配置文件
Reader reader = Resources.getResourceAsReader("mybatis-config.xml");
// 2.实例化SqlSessionFactory
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
// 3.实例化SqlSession
try (SqlSession session = sqlSessionFactory.openSession()) {
// 4.获取PersonMapper接口
PersonMapper personMapper = session.getMapper(PersonMapper.class);
// 5.执行SQL
String name = personMapper.queryPersonNameById(1);
System.err.println(name);
}
}
}
总结
到这里,我们就掌握了Mapper.xml文件中parameterType,resultType,resultMap的用法,大家可以动手试试其他的类型转换,欢迎留言交流,下篇见。