First step: Write type converter
Package com.wkk.utils;
Import java.sql.CallableStatement;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import Java.sql.Types;
Import java.text.ParseException;
Import Java.text.SimpleDateFormat;
Import Java.util.Date;
Import Org.apache.ibatis.type.JdbcType;
Import Org.apache.ibatis.type.TypeHandler; public class Mydatetypehander implements typehandler<date>{SimpleDateFormat simpledateformat=new SimpleDateFor
Mat ("Yyyy-mm-dd");
* * FROM database to JavaBean */public Date GetResult (ResultSet rs, String columnName) throws SQLException {
First get the value in the database String value=rs.getstring (columnName);
if (value==null) {return null;
}else{Date Date=null;
try {date = Simpledateformat.parse (value);
catch (ParseException e) {e.printstacktrace ();
} return date; }} public DaTe GetResult (ResultSet rs, int columnindex) throws SQLException {//First get the value in the database String value=rs.ge
Tstring (columnindex);
if (value==null) {return null;
}else{Date Date=null;
try {date = Simpledateformat.parse (value);
catch (ParseException e) {e.printstacktrace ();
} return date;
} public Date GetResult (callablestatement cs, int columnindex) throws SQLException {//Get the value in the database first
String value=cs.getstring (columnindex);
if (value==null) {return null;
}else{Date Date=null;
try {date = Simpledateformat.parse (value);
catch (ParseException e) {e.printstacktrace ();
} return date;
}
}
/* *javabean to database *i current parameter * Date Current parameter Java Object * Jdbctype the database type of the current parameter * date--string/PU
Blic void Setparameter (preparedstatement ps, int i, date date, Jdbctype Jdbctype) throws SQLException {
if (date==null) {//If it is empty, store it in the database as a null ps.setnull (I, Types.varchar);
}else{String Str=simpledateformat.format (date);
Ps.setstring (I,STR);
}
}
}
Step Two: Configure in the MyBatis configuration document
Note the order in which MyBatis's official configuration documents are to be followed:
Properties,setting,typealiases,typehandles,objectfactory,plugins,environments,mappers
Configuration content is as follows:
<typeHandlers>
<typehandler handler= "Com.wkk.utils.MyDateTypeHander" javatype= "Java.util.Date"
Jdbctype= "VARCHAR"/>
</typeHandlers>
Step Three:
When writing SQL statements in Mapper.xml, be aware of quoting him:
BeginTime and Endtime
<resultmap type= "recruitment" id= "Recruitmentoperatemap" > <id property= "Recruitmentid" Recruitmentid "/> <result property=" Personnumber "column=" Personnumber "/> <result property=" LinkMan "Co lumn= "Linkman"/> <result property= "Phone" column= "phone"/> <result property= "email" column= "email"/>
; <result property= "Postname" column= "Postname"/> <result property= "begintime" column= "BeginTime" Java.util.Date "jdbctype=" VARCHAR "/> <result property=" Endtime "column=" Endtime "javatype=" Java.util.Date " Jdbctype= "VARCHAR"/> <result property= "salary" column= "salary"/> "<result property=" Recruitmentcode "Co lumn= "Recruitmentcode"/> <association property= "Company" javatype= "Com.wkk.entity.Company" > <id PR Operty= "CompanyID" column= "CompanyID"/> <result property= "companyName" column= "CompanyName"/> ; result property= "companyaddress" column= "CompanyAddress"/> <result property= "Companyscale" column= "Companyscale"/> <result "property="
Companyintroduce "column=" Companyintroduce "/> <result property=" Companycode "column=" CompanyCode "/> <result property= "Companynature" column= "Companynature"/> </association> </resultMap>