In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use mybatis custom date type converter". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use mybatis custom date type converter.
Preface
Use typeHandlers in mybatis to implement a converter for custom date types.
Focus on 2.2 handler
1. Resources
1.1 sqlMapConfig.xml
TypeHandlers: custom date type converter:
(1) convert the date type to the long integer type of long and store it in the database.
(2) change the bigint in the database (which essentially corresponds to the long type of java) to the date type. Environments: load database mapper: location of the mapping file
1.2 log4j.properties
Print log
# direct log messages to stdout # # log4j.appender.stdout=org.apache.log4j.ConsoleAppenderlog4j.appender.stdout.Target=System.outlog4j.appender.stdout.layout=org.apache.log4j.PatternLayoutlog4j.appender.stdout.layout.ConversionPattern=%d {ABSOLUTE}% 5p% c {1}:% L -% m%n### direct messages to file mylog.log # log4j.appender.file=org.apache.log4j.FileAppenderlog4j.appender.file.File=c:/mylog.loglog4j.appender.file.layout=org.apache.log4j.PatternLayoutlog4j. Appender.file.layout.ConversionPattern=%d {ABSOLUTE}% 5p% c {1}:% L -% m%n### set log levels-for more verbose logging change 'info' to' debug' # log4j.rootLogger=debug Stdout1.3 userMapper.xml
1.findByid: inserting elements through id query
2.save: when xml mapper mapper
UseGeneratedKeys: the value is true and specify the attributes:
KeyProperty: the primary key property of the corresponding Java object
KeyColumn: the corresponding database record primary key field
Select * from sys_user where id=# {id} insert into sys_user (id,username,email,password,phoneNum,birthday) values (null,# {username}, null,# {password}, null,# {birthday}) II. Java structure
2.1 dao
UserMapper
1.findByid: query User using id values
2.save: inserting User into the database
Package com.mytest.dao;import com.mytest.pojo.User;import java.util.List;public interface UserMapper {User findByid (int id); public void save (User user);} 2.2 handler
DateTypeHandler
SetNonNullParameter: convert the java type to the type required by the database
GetNullableResult: convert types in the database to java types
Package com.mytest.handler;import org.apache.ibatis.type.BaseTypeHandler;import org.apache.ibatis.type.JdbcType;import java.sql.CallableStatement;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Date;public class DateTypeHandler extends BaseTypeHandler {/ / convert the java type to the type required by the database public void setNonNullParameter (PreparedStatement preparedStatement, int I, Date date, JdbcType jdbcType) throws SQLException {long time = date.getTime () PreparedStatement.setLong (iForce time);} / / convert the type in the database to java type / / the field name to be converted by the String parameter / / the result set queried by ResultSet public Date getNullableResult (ResultSet resultSet, String s) throws SQLException {/ / get the data needed in the result set (long) convert to Date type return long aLong = resultSet.getLong (s) Date date = new Date (aLong); return date; public Date getNullableResult (ResultSet resultSet, int I) throws SQLException {long aLong = resultSet.getLong (I); public Date getNullableResult (CallableStatement callableStatement, int I) throws SQLException {long aLong = callableStatement.getLong (I);} 2.3 pojopackage com.mytest.pojo;import java.util.Date;public class User {private Integer id; private String username; private String password; private Date birthday Public Date getBirthday () {return birthday;} public void setBirthday (Date birthday) {this.birthday = birthday @ Override public String toString () {return "User {" + "id=" + id + ", username='" + username +'\'+ ", password='" + password +'\'+ ", birthday=" + birthday +'}'; public Integer getId () {return id Public void setId (Integer id) {this.id = id; public String getUsername () {return username; public void setUsername (String username) {this.username = username; public String getPassword () {return password; public void setPassword (String password) {this.password = password;} 2.4 service
Service is not implemented and is only used for testing in this demo.
SqlSession.commit () is used for transaction commit of mybatis. Transactions in mybatis are not committed by default, so transactions need to be committed in addition, deletion and modification. There are generally two ways to commit transactions in mybatis:
1 sqlSession.commit ()
2 automatic submission can be achieved by using openSession (true) without calling the commit () method.
Package com.mytest.service;import com.mytest.dao.UserMapper;import com.mytest.pojo.User;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 java.io.IOException;import java.io.InputStream;import java.util.Date;import java.util.List Public class ServiceTest {public static void main (String [] args) throws IOException {InputStream inputStream = Resources.getResourceAsStream ("sqlMapConfig.xml"); SqlSessionFactory sqlSessionFactory = (SqlSessionFactory) new SqlSessionFactoryBuilder () .build (inputStream); / / automatic transaction commit SqlSession sqlSession = sqlSessionFactory.openSession () if openSession is true; UserMapper mapper = sqlSession.getMapper (UserMapper.class); User user = new User () / / insert id value / / user.setId (5); user.setUsername ("wangwu"); user.setPassword ("123"); user.setBirthday (new Date ()); / / return attribute value mapper.save (user); Integer userId = user.getId (); sqlSession.commit (); User findUser = mapper.findByid (userId) The structure and Test of System.out.println (findUser);}} three data tables
Data table structure
test
At this point, I believe you have a deeper understanding of "how to use mybatis custom date type converter". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.