Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Type conversion in mybatis

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "the way of type conversion in mybatis". In the operation of actual cases, many people will encounter this dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

1. Scene

This is a common requirement in daily java development, where codes such as 0 or 1 (not limited to numbers) are used to represent a state. For example, use 0 for female and 1 for male. And writing to the database may be an identity that is read from the database and restored to a specific description. And in general, in order to better understand or eliminate magic values, the usual solution is to define an enumeration:

Some enumerations are defined in this way

Public enum GenderType {FEMALE,MALE,UNKNOWN}

So usually a lot of people will enter the library like this (java pseudo code)

If (GenderType.MALE) {/ / write 1} else if (GenderType.FEMALE) {/ / write 0} else {/ / may also be from Thailand, then 2}

When reading, either do the same reverse processing as above or use the database sql syntax case when to write directly to DTO.

CASE gender WHEN 1 THEN 'male' WHEN 0 THEN 'female' ELSE 'unknown' END

This treatment doesn't look very elegant. And a lot of judgment and processing logic, and our business is not very relevant. So we can choose a better way to deal with it.

TypeHandler in 2.Mybatis

If you use Mybatis in the ORM framework. Then this problem can be easily solved through the TypeHandler interface.

2.1 TypeHandler Analysis 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;}

Source code analysis:

The setParameter method writes your own logic through the T type passed in and chooses to call a set method of the PreparedStatement object to write the data to the database. This method is used to write libraries.

GetResult (ResultSet rs, String columnName) reads the library by field name and converts it to T type.

GetResult (ResultSet rs, int columnIndex) reads the library through the field index and converts it to T type.

GetResult (CallableStatement cs, int columnIndex) calls a stored procedure to get the result and convert it to a T type.

2.2 EnumOrdinalTypeHandler

We found that TypeHandler has an implementation class EnumOrdinalTypeHandler. It literally means that types can be handled by enumerating their ordinal numbers.

Override public void setNonNullParameter (PreparedStatement ps, int I, E parameter, JdbcType jdbcType) throws SQLException {ps.setInt (I, parameter.ordinal ());}

Let's not consider the setNull situation for the moment. Through this method, we find that the sequence values of enumerations are indeed stored (the order starts from 0). In the above example, if GenderType.FEMALE is 0, if GenderType.MALE is 1, but when GenderType.UNKNOWN is stored is 2. When fetching, it is also naturally reversed for specific GenderType enumerations.

2.3 EnumTypeHandler

We also found another enumerated type processor. Its set method looks like this:

@ Override public void setNonNullParameter (PreparedStatement ps, int I, E parameter, JdbcType jdbcType) throws SQLException {if (jdbcType = = null) {ps.setString (I, parameter.name ());} else {ps.setObject (I, parameter.name (), jdbcType.TYPE_CODE); / / see r3589}}

Regardless of the jdbcType problem, we find that the value of Enum.name () is written to the database. Take the above example if GenderType.FEMALE is FEMALE, if GenderType.MALE is MALE, but when GenderType.UNKNOWN is stored is UNKNOWN. Reading the library is a reverse operation through Enum.valueOf (Class enumType,String name).

2.4 Custom TypeHandler

What if our enumerated types or we use other ways to handle class conversions? Of course, Mybatis won't help you with such a specific thing. You need to do it yourself. We also take enumerations as an example, and then mimic the above two TypeHandler. Let's take the initial example. I personally prefer to define enumerations this way:

Public enum GenderTypeEnum {/ * * female. * / FEMALE (0, "female"), / * male. * / MALE (1, "male"), / * unknown. * / UNKNOWN (2, "unknown"); private int value; private String description; GenderType (int value, String description) {this.value = value; this.description = description;} public int value () {return this.value;} public String description () {return this.description;}}

You can implement the three hook methods of this abstract class by inheriting BaseTypeHandler:

@ MappedTypes ({GenderTypeEnum.class}) @ MappedJdbcTypes ({JdbcType.INTEGER}) public class GenderTypeEnumTypeHandler extends BaseTypeHandler {@ Override public void setNonNullParameter (PreparedStatement ps, int I, GenderTypeEnum parameter, JdbcType jdbcType) throws SQLException {if (jdbcType = = null) {ps.setInt (I, parameter.value ());} else {/ / see r3589 ps.setObject (I, parameter.value (), jdbcType.TYPE_CODE) } @ Override public GenderTypeEnum getNullableResult (ResultSet rs, String columnName) throws SQLException {return getGenderType (rs.getInt (columnName));} @ Override public GenderTypeEnum getNullableResult (ResultSet rs, int columnIndex) throws SQLException {return getGenderType (rs.getInt (columnIndex));} @ Override public GenderTypeEnum getNullableResult (CallableStatement cs, int columnIndex) throws SQLException {return getGenderType (cs.getInt (columnIndex)) } private GenderTypeEnum getGenderType (int value) {Class genderTypeClass = GenderTypeEnum.class; return Arrays.stream (genderTypeClass.getEnumConstants ()) .filter (genderType-> genderType.value () = = value) .findFirst () .orElse (GenderTypeEnum.UNKNOWN);}}

Now that the TypeHandler implementation is written, how do you make it work? Let's keep going.

Core points of 2.5 TypeHandler

The function of TypeHandler is the conversion between javaType and jdbcType. So when declaring a TypeHandler, be sure to be clear about the two types that the TypeHandler handles. This is a principle that must be made clear. MyBatis does not determine which JDBC type to use by snooping database meta-information, so you must specify which type of field to use in the parameter and result mapping so that it can be bound to the correct type handler. MyBatis is not aware of the data type until the statement is executed. The binding type conversion relationship is performed through @ MappedJdbcTypes and @ MappedTypes in the above example, or it can be specified by jdbcType or javaType in the typeHandler element of xml. If specified at the same time, xml has a higher priority. Note that it is possible that you will overwrite the built-in TypeHandler. So be sure to understand some of the default processors provided by Mybatis when customizing. Avoid the impact on other businesses. So one of the most important principles for using custom TypeHandler is to declare JavaType and JdbcType. Although the above is a bit raw, it is very important to use TypeHandler well. Next, let's talk about the specific configuration.

2.6 Registration-free TypeHandler

We will only talk about the configuration in xml:

A declaration in a rultMap element is generally used for querying. Be sure to pay attention to some of the principles in 2.5.

Then it is used in insert and update statements. They are all the same, and here is only one insertion example.

Insert into student (student_name, gender, age) values (# {studentName}, # {genderType,javaType=cn.felord.mybatis.enums.GenderTypeEnum,jdbcType=INTEGER,typeHandler=cn.felord.mybatis.type.GenderTypeEnumTypeHandler}, # {age})

Aliases can be used if aliases are registered. The advantage of the above is that there is no need to register in TypeHandlerRegistry.

2.7Register TypeHandler

Register the TypeHandler in the configuration, and then the Mybatis automatically matches based on the two types. So here we still want to emphasize the core points of 2.5.

If you are a xml configuration, you need to register declaratively in the tag in the Configuration configuration file.

JavaConfig mode, first, you can get the Configuration object through the SqlSessionFactory object to register the typeHandler. If you use mybatis-spring components, you can configure the centralized package path of typeHandler in the setTypeHandlersPackage method of SqlSessionFactoryBean, then the framework will automatically scan and register them. The corresponding configuration property in springboot is mybatis.typeHandlersPackage.

If you sign up for TypeHandler. In Mapper.xml, you only need to declare jdbcType and javaType, and you don't need to declare a specific typeHandler. Mybatis will automatically map to the specific registered TypeHandler through jdbcType and javaType. Like the following example

Insert into student (student_name, gender, age) values (# {studentName}, # {genderType,javaType=cn.felord.mybatis.enums.GenderTypeEnum,jdbcType=INTEGER}, # {age}) 3. Summary

Today we learned how to convert types by using type handlers in mybatis development, how to handle enumerations, and how to customize the processor and use it. I believe it will be of great help to you in the process of java development. The related code is in my code cloud repository: https://gitee.com/felord/mybatis-test.git

That's all for the content of "Type conversion in mybatis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report