In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to map Java enumerations in MyBatis database fields". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let the editor take you to learn how MyBatis database fields map Java enumerations.
Sometimes we need to map the fields of the database to the enumeration types of Java. For example, we have an enumeration of auto parts types.
Public enum ProductType implements Localisable {TYPE1 ("accessories"), TYPE2 ("car products"); private String value; private ProductType (String value) {this.value = value;} @ Override public String getValue () {return this.value;}}
This enumeration type implements an interface
Public interface Localisable {String getValue ();}
An entity class with a fitting category that contains the enumerated field (here only some field properties are included)
/ * AllArgsConstructor@NoArgsConstructorpublic class ProviderProductLevel {@ Getter @ Setter private Long id; @ Getter @ Setter private String code; @ Setter private String name; @ Getter @ Setter private Integer sort; @ Setter private Integer level; @ Getter @ Setter private ProductType productType; @ Getter @ Setter private String pictureUrl;}
The table structure in the database is as follows
The dao method is as follows (find a fitting category through id and instantiate it)
@ Mapperpublic interface LevelDao {ProviderProductLevel findLevel1 (Long id);}
The mapper mapping file is as follows
Select id,code,name,sort,product_type from product_level id=# {id}
We can see that there is a mapping processor typeHandler= "com.cloud.productprovider.untils.DbEnumTypeHandler" here.
The code for the mapping processor is as follows
@ AllArgsConstructorpublic class DbEnumTypeHandler extends BaseTypeHandler {private Class type; @ Override public void setNonNullParameter (PreparedStatement ps, int I, Localisable parameter, JdbcType jdbcType) throws SQLException {ps.setString (iParameter.getValue ());} @ Override public Localisable getNullableResult (ResultSet rs, String columnName) throws SQLException {String value = rs.getString (columnName); if (rs.wasNull ()) {return null;} else {return convert (value) } @ Override public Localisable getNullableResult (ResultSet rs, int columnIndex) throws SQLException {String value = rs.getString (columnIndex); if (rs.wasNull ()) {return null;} else {return convert (value);} @ Override public Localisable getNullableResult (CallableStatement cs, int columnIndex) throws SQLException {String value = cs.getString (columnIndex); if (cs.wasNull ()) {return null } else {return convert (value);}} private Localisable convert (String value) {Localisable [] dbEnums = type.getEnumConstants (); for (Localisable dbEnum: dbEnums) {if (dbEnum.getValue (). Equals (value)) {return dbEnum;}} return null;}}
The Json string of the result object returned by the test is as follows
{"code": "0000001", "id": 1, "name": "Oil products", "productType": "TYPE1", "sort": 1}
At this point, I believe you have a deeper understanding of "how MyBatis database fields map Java enumeration". 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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.