代码准备
创建mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<properties>
<property name="driver" value="com.mysql.cj.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://127.0.0.1:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/>
<property name="username" value="root"/>
<property name="password" value="123456"/>
</properties>
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<environments default="default">
<environment id="default">
<transactionManager type="JDBC"/>
<dataSource type="POOLED">
<property name="driver" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${username}"/>
<property name="password" value="${password}"/>
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="mapper/ConstructorMapper.xml" />
</mappers>
</configuration>
创建ConstructorMapper.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="com.ys.mybatis.mapper.ConstructorMapper">
<resultMap id="employeeMap" type="com.ys.mybatis.domian.Employee">
<constructor>
<idArg name="id" column="id"/>
<arg name="name" column="name" />
</constructor>
<result property="age" column="age"/>
<result property="phone" column="phone"/>
</resultMap>
<select id="getEmployeeByIdForSimple" resultType="com.ys.mybatis.domian.Employee">
select * from employee where id = #{id}
</select>
<select id="getEmployeeByIdForResultMap" resultMap="employeeMap">
select * from employee where id = #{id}
</select>
</mapper>
创建实体类Employee
@Slf4j
@Data
public class Employee {
private Integer id;
private String name;
private Integer age;
private String phone;
public Employee() {
log.info("调用了空构造方法");
}
public Employee(Integer id) {
this.id = id;
log.info("调用了一个参数的构造方法");
}
public Employee(Integer id, String name) {
this.id = id;
this.name = name;
log.info("调用了两个参数的构造方法");
}
public Employee(Integer id, String name, Integer age) {
this.id = id;
this.name = name;
this.age = age;
log.info("调用了三个参数的构造方法");
}
public Employee(Integer id, String name, Integer age, String phone) {
this.id = id;
this.name = name;
this.age = age;
this.phone = phone;
log.info("调用了四个参数的构造方法");
}
}
创建ConstructorMapper.java
public interface ConstructorMapper {
Employee getEmployeeByIdForSimple(Integer id);
Employee getEmployeeByIdForResultMap(Integer id);
}
创建测试类ConstructorTest
@Slf4j
public class ConstructorTest {
private SqlSessionFactory sqlSessionFactory;
@BeforeEach
public void before() {
InputStream inputStream = ConfigurationTest.class.getClassLoader().getResourceAsStream("mybatis-config.xml");
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
}
}
1.使用默认构造方法
@Test
public void testDefaultConstructor() {
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取ConstructorMapper对象
ConstructorMapper mapper = sqlSession.getMapper(ConstructorMapper.class);
Employee employee = mapper.getEmployeeByIdForSimple(1);
System.out.println(employee);
}
默认情况下,mybatis通过默认构造方法,实例化对象
2.使用指定构造方法
@Test
public void testResultMap() {
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取ConstructorMapper对象
ConstructorMapper mapper = sqlSession.getMapper(ConstructorMapper.class);
Employee employee = mapper.getEmployeeByIdForResultMap(1);
System.out.println(employee);
}
如果select指定了resultMap,且resultMap存在constructor标签,则mybatis就根据constructor标签指定的构造器进行实例化
PS : 如果测试方法报错,需要在maven文件中添加如下配置:
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.2</version>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
</plugins>
3.没有默认构造方法,ResultMap也不存在constructor标签 (将默认构造方法注释)
相关sql、Mapper接口方法、测试方法、测试结果 如下所示:
<resultMap id="noDefaultEmployeeMap" type="com.ys.mybatis.domian.Employee">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="phone" column="phone"/>
</resultMap>
<select id="getEmployeeByIdForNoDefaultResultMap" resultMap="noDefaultEmployeeMap">
select id,name,age,phone from employee where id = #{id}
</select>
Employee getEmployeeByIdForNoDefaultResultMap(Integer id);
@Test
public void noDefaultConstructorTest() {
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取ConstructorMapper对象
ConstructorMapper mapper = sqlSession.getMapper(ConstructorMapper.class);
Employee employee = mapper.getEmployeeByIdForNoDefaultResultMap(1);
System.out.println(employee);
}
选择参数个数和返回列数相等的构造方法
PS : 注意是查询返回的列数,不是ResultMap指定的映射个数。如果将查询的列数改成三个,则使用三个参数的构造方法
<select id="getEmployeeByIdForNoDefaultResultMap" resultMap="noDefaultEmployeeMap">
select id,name,age from employee where id = #{id}
</select>
4.自定义TypeHandler
4.1 自定义EmployeeTypeHandler
@MappedTypes(value = {Employee.class})
public class EmployeeTypeHandler extends BaseTypeHandler<Employee> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Employee employee, JdbcType jdbcType) throws SQLException {
switch (i) {
case 1:
ps.setInt(i, employee.getId());
break;
case 2:
ps.setString(i, employee.getName());
break;
case 3:
ps.setInt(i, employee.getAge());
break;
case 4:
ps.setString(i, employee.getPhone());
break;
}
}
@Override
public Employee getNullableResult(ResultSet rs, String columnName) throws SQLException {
Employee employee = new Employee();
employee.setId(rs.getInt("id"));
employee.setName(rs.getString("name"));
employee.setAge(rs.getInt("age"));
employee.setPhone(rs.getString("phone"));
return employee;
}
@Override
public Employee getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
Employee employee = new Employee();
employee.setId(rs.getInt(1));
employee.setName(rs.getString(2));
employee.setAge(rs.getInt(3));
employee.setPhone(rs.getString(4));
return employee;
}
@Override
public Employee getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
Employee employee = new Employee();
employee.setId(cs.getInt(1));
employee.setName(cs.getString(2));
employee.setAge(cs.getInt(3));
employee.setPhone(cs.getString(4));
return employee;
}
}
4.2 在mybatis-config.xml文件中配置TypeHandler
<typeHandlers>
<typeHandler handler="com.ys.mybatis.handler.EmployeeTypeHandler"/>
</typeHandlers>
4.3 执行测试方法
@Test
public void testDefaultConstructor() {
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取ConstructorMapper对象
ConstructorMapper mapper = sqlSession.getMapper(ConstructorMapper.class);
Employee employee = mapper.getEmployeeByIdForSimple(1);
System.out.println(employee);
}
自定义TypeHandler可以使用任意构造方法实例化对象,比如将getNullableResult方法修改为如下所示:
@Override
public Object getNullableResult(ResultSet rs, String columnName) throws SQLException {
return new Employee(rs.getInt("id"), rs.getString("name"), rs.getInt("age"), rs.getString("phone"));
}
局部使用typeHandler
假设现在Employee存在一个additional属性,其在数据库中是以Json格式存储的,我们希望可以将这个Json字符串,转成Additional实例对象。Additional结构如下:
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Additional {
private String email;
private String address;
}
创建AdditionalTypeHandler
public class AdditionalTypeHandler extends BaseTypeHandler<Additional> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, Additional additional, JdbcType jdbcType) throws SQLException {
switch (i) {
case 1:
ps.setString(i, additional.getEmail());
break;
case 2:
ps.setString(i, additional.getAddress());
break;
}
}
@Override
public Additional getNullableResult(ResultSet rs, String columnName) throws SQLException {
String val = rs.getString(columnName);
return new Gson().fromJson(val, Additional.class);
}
@Override
public Additional getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String val = rs.getString(columnIndex);
return new Gson().fromJson(val, Additional.class);
}
@Override
public Additional getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String val = cs.getString(columnIndex);
return new Gson().fromJson(val, Additional.class);
}
}
相关sql、Mapper接口方法、测试方法、测试结果 如下所示:
<resultMap id="additionalMap" type="com.ys.mybatis.domian.Employee">
<result property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
<result property="phone" column="phone"/>
<result property="additional" column="json" typeHandler="com.ys.mybatis.handler.AdditionalTypeHandler"/>
</resultMap>
<select id="getEmployeeByIdForPart" resultMap="additionalMap">
select * from employee where id = #{id}
</select>
Employee getEmployeeByIdForPart(Integer id);
@Test
public void testPartTypeHandler() {
// 获取sqlSession
SqlSession sqlSession = sqlSessionFactory.openSession();
// 获取ConstructorMapper对象
ConstructorMapper mapper = sqlSession.getMapper(ConstructorMapper.class);
Employee employee = mapper.getEmployeeByIdForPart(1);
System.out.println(employee);
}
PS : 需要把步骤4.2在mybatis-config.xml文件中配置的EmployeeTypeHandler移除
优先级
- 返回类型存在自定义的TypeHandler
- ResultMap中指定了构造方法
- 默认构造方法
- 无默认构造方法,且ResultMap中未指定了构造方法 (resultType也会被解析成ResultMap)