In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the role of TypeHandler in Mybatis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
Mybatis-3.4.6.release.
TypeHandler is an important component in mybatis, which is used to set parameters for statement or to take values from Resultset.
List-1
Public interface TypeHandler {void setParameter (PreparedStatement ps, int I, T parameter, JdbcType jdbcType) throws SQLException; T getResult (ResultSet rs, String columnName) throws SQLException; T getResult (ResultSet rs, int columnIndex) throws SQLException; T getResult (CallableStatement cs, int columnIndex) throws SQLException;}
As shown in List-1, the setParameter method is used in ParameterHandler, and the other three getResult methods are used in ResultSetHandler, why there are three getResult methods because values are obtained from ResultSet, which can be obtained through subscripts or column names, and stored procedures are handled differently.
Figure 1
The class inheritance diagram of BaseTypeHandler is shown in figure 1. First, let's take a look at TypeReferernce. Its main function is to get generics on the class, which is implemented in the constructor of TypeReferernce. As shown in List-2 below, the result of getRawType is BigDecimal.
List-2
Public class BigDecimalTypeHandler extends BaseTypeHandler
Template pattern is used in BaseTypeHandler. The specific implementation is implemented by subclasses, such as List-3:
List-3
Public abstract class BaseTypeHandler extends TypeReference implements TypeHandler {@ Override public void setParameter (PreparedStatement ps, int I, T parameter, JdbcType jdbcType) throws SQLException {... If parameter is null, call the setNull method ps.setNull (I, jdbcType.TYPE_CODE) of PreparedStatement directly. SetNonNullParameter (ps, I, parameter, jdbcType);.} @ Override public T getResult (ResultSet rs, String columnName) throws SQLException {... Result = getNullableResult (rs, columnName);.} @ Override public T getResult (ResultSet rs, int columnIndex) throws SQLException {... Result = getNullableResult (rs, columnIndex);.} @ Override public T getResult (CallableStatement cs, int columnIndex) throws SQLException {... Result = getNullableResult (cs, columnIndex);...} public abstract void setNonNullParameter (PreparedStatement ps, int I, T parameter, JdbcType jdbcType) throws SQLException; public abstract T getNullableResult (ResultSet rs, String columnName) throws SQLException; public abstract T getNullableResult (ResultSet rs, int columnIndex) throws SQLException; public abstract T getNullableResult (CallableStatement cs, int columnIndex) throws SQLException;}
Let's look at an example, BooleanTypeHandler:
List-4
Public class BooleanTypeHandler extends BaseTypeHandler {@ Override public void setNonNullParameter (PreparedStatement ps, int I, Boolean parameter, JdbcType jdbcType) throws SQLException {ps.setBoolean (I, parameter);} @ Override public Boolean getNullableResult (ResultSet rs, String columnName) throws SQLException {return rs.getBoolean (columnName); @ Override public Boolean getNullableResult (ResultSet rs, int columnIndex) throws SQLException {return rs.getBoolean (columnIndex) } @ Override public Boolean getNullableResult (CallableStatement cs, int columnIndex) throws SQLException {return cs.getBoolean (columnIndex);}} is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
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.