The date in the JavaBean of the MyBatis type converter is stored as a varchar type

Source: Internet
Author: User

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"/&gt
    ; <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>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.