实体类
package com.ghgcn.mybatis.entity;
public class PhoneNumber {
private String countryCode;
private String stateCode;
private String number;
public PhoneNumber() {
}
public PhoneNumber(String countryCode, String stateCode, String number) {
super();
this.countryCode = countryCode;
this.stateCode = stateCode;
this.number = number;
}
public PhoneNumber(String string) {
if (string != null) {
String[] parts = string.split("-");
if (parts.length > 0)
this.countryCode = parts[0];
if (parts.length > 1)
this.stateCode = parts[1];
if (parts.length > 2)
this.number = parts[2];
}
}
public String getAsString() {
return countryCode + "-" + stateCode + "-" + number;
}
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getStateCode() {
return stateCode;
}
public void setStateCode(String stateCode) {
this.stateCode = stateCode;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@Override
public String toString() {
return "PhoneNumber [countryCode=" + countryCode + ", stateCode=" + stateCode + ", number=" + number + "]";
}
}
package com.ghgcn.mybatis.entity;
import java.io.Serializable;
import java.util.Date;
public class Student implements Serializable {
private static final long serialVersionUID = -7301987836735803448L;
private Integer studId;
private String name;
private String email;
private Date dob;
private PhoneNumber phone;
public Integer getStudId() {
return studId;
}
public void setStudId(Integer studId) {
this.studId = studId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public PhoneNumber getPhone() {
return phone;
}
public void setPhone(PhoneNumber phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Student [studId=" + studId + ", name=" + name + ", email=" + email + ", dob=" + dob + ", phone=" + phone
+ "]";
}
}
typehandler
package com.ghgcn.mybatis.typehandlers;
import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;
import com.ghgcn.mybatis.entity.PhoneNumber;
public class PhoneTypeHandler extends BaseTypeHandler<PhoneNumber> {
@Override
public void setNonNullParameter(PreparedStatement ps, int i, PhoneNumber parameter, JdbcType jdbcType)
throws SQLException {
ps.setString(i, parameter.getAsString());
}
@Override
public PhoneNumber getNullableResult(ResultSet rs, String columnName) throws SQLException {
return new PhoneNumber(rs.getString(columnName));
}
@Override
public PhoneNumber getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
return new PhoneNumber(rs.getString(columnIndex));
}
@Override
public PhoneNumber getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
return new PhoneNumber(cs.getString(columnIndex));
}
}
config.xml中
<typeHandlers>
<typeHandler
handler="com.ghgcn.mybatis.typehandlers.PhoneTypeHandler" />
</typeHandlers>
- 完配置
<?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 resource="jdbc.properties" />
<settings>
<!--执行器,默认为SimpleExcutor -->
<setting name="defaultExecutorType" value="SIMPLE" />
<!-- 开启延时加载 -->
<setting name="lazyLoadingEnabled" value="true" />
<!-- 关闭立即加载 -->
<setting name="aggressiveLazyLoading" value="false" />
<!--开启二级缓存 -->
<setting name="cacheEnabled" value="true" />
<!--超时时间 -->
<setting name="defaultStatementTimeout" value="25" />
<!--一级缓存 -->
<setting name="localCacheScope" value="SESSION" />
<!--日志前缀 -->
<setting name="logPrefix" value="liunan: " />
<!--MyBatis 所用日志的具体实现 SLF4J | LOG4J | LOG4J2 | JDK_LOGGING | COMMONS_LOGGING
| STDOUT_LOGGING | NO_LOGGING -->
<setting name="logImpl" value="SLF4J" />
<setting name="jdbcTypeForNull" value="OTHER" />
</settings>
<!--别名包 -->
<typeAliases>
<package name="com.ghgcn.mybatis.entity" />
</typeAliases>
<typeHandlers>
<typeHandler
handler="com.ghgcn.mybatis.typehandlers.PhoneTypeHandler" />
</typeHandlers>
<environments default="development">
<environment id="development">
<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/StudentMapper.xml" />
</mappers>
</configuration>
mapper.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.ghgcn.mybatis.mapper.StudentMapper">
<resultMap type="com.ghgcn.mybatis.entity.Student"
id="BaseResultMap">
<id column="stud_id" property="studId" />
<result column="name" property="name" />
<result column="email" property="email" />
<result column="dob" property="dob" />
<result column="phone" property="phone" />
<!-- <result typeHandler="com.ghgcn.mybatis.typehandlers.PhoneTypeHandler"
column="phone" property="phone" javaType="com.ghgcn.mybatis.entity.PhoneNumber"
jdbcType="VARCHAR" /> -->
</resultMap>
<sql id="baseSql">
stud_id,name,email,dob,phone
</sql>
<select id="findAllStudents" resultMap="BaseResultMap">
SELECT
<include refid="baseSql" />
FROM student
</select>
<select id="findStudentById" resultMap="BaseResultMap">
SELECT
<include refid="baseSql" />
FROM
student
WHERE stud_id = #{stuId}
</select>
<insert id="insertStudent" parameterType="Student">
INSERT INTO
STUDENT (NAME,EMAIL,DOB,PHONE)
VALUES(#{name},#{email},#{dob},#{phone})
</insert>
<update id="updateStudent" parameterType="Student">
UPDATE STUDENT
SET NAME=#{name},
EMAIL=#{email},
DOB=#{dob},
PHONE=#{phone}
WHERE stud_id = #{studId}
</update>
</mapper>
测试
SqlSession sqlSession = MyBatisSqlSessionFactory.getSqlSessionFactory().openSession();
StudentMapper studentMapper = sqlSession.getMapper(StudentMapper.class);
Student student = studentMapper.findStudentById(7);
logger.info(" findStudentById {} ", student);
2018-04-19 17:44:34.400 [main] INFO com.ghgcn.mybatis.test.StudentTest2 - findStudentById Student [studId=7, name=phone1, email=phone1@qq.com, dob=Thu Apr 19 00:00:00 CST 2018, phone=PhoneNumber [countryCode=0086, stateCode=0755, number=13410110407]]
网友评论