4 kinds of solutions MyBatis entity class field attribute table inconsistent

pom configuration dependencies:

 1 <dependencies>
 2     <dependency>
 3         <groupId>org.mybatis</groupId>
 4         <artifactId>mybatis</artifactId>
 5         <version>3.4.5</version>
 6     </dependency>
 7     <dependency>
 8         <groupId>junit</groupId>
 9         <artifactId>junit</artifactId>
10         <version>4.12</version>
11     </dependency>
12     <dependency>
13         <groupId>mysql</groupId>
14         <artifactId>mysql-connector-java</artifactId>
15         <version>8.0.17</version>
16     </dependency>
</17 dependencies>

 

MySQL database built table statement:

. 1  the CREATE  TABLE `tb_user` (
 2    ` id` int ( . 11 ) the NOT  NULL the AUTO_INCREMENT,
 . 3    `username` VARCHAR ( 32 ) the NOT  NULL the COMMENT ' username ' ,
 . 4    ` sex` char ( . 1 ) the DEFAULT  NULL the COMMENT ' Sex ' ,
 . 5    `birthday` DATE the DEFAULT  NULL the COMMENT ' birthday ' ,
 6   `address` varchar(256) DEFAULT NULL COMMENT '地址',
7   PRIMARY KEY (`id`)
8 ) ;

 

Java entity class code:

 1 public class User {
 2 
 3     private int id;
 4     private String name;
 5     private String sex;
 6     private Date birthday;
 7     private String address;
 8 
 9     @Override
10     public String toString() {
11         return "User{" +
12                 "id=" + id +
13                 ", name='" + name + '\'' +
14                 ", sex='" + sex + '\'' +
15                 ", birthday=" + birthday +
16                 ", address='" + address + '\'' +
17                 '}';
18     }
19 
20     public int getId() {
21         return id;
22     }
23 
24     public void setId(int id) {
25         this.id = id;
26     }
27 
28     public String getName() {
29         return name;
30     }
31 
32     public void setName(String name) {
33         this.name = name;
34     }
35 
36     public String getSex() {
37         return sex;
38     }
39 
40     public void setSex(String sex) {
41         this.sex = sex;
42     }
43 
44     public Date getBirthday() {
45         return birthday;
46     }
47 
48     public void setBirthday(Date birthday) {
49         this.birthday = birthday;
50     }
51 
52     public String getAddress() {
53         return address;
54     }
55 
56     public void setAddress(String address) {
57         this.address = address;
58     }
59 }

 

MyBatis test method code:

 1     @Test
 2     public void test01() {
 3 
 4         InputStream inputStream = null;
 5         try {
 6             inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
 7         } catch (IOException e) {
 8             e.printStackTrace();
 9         }
10 
11         SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
12 
13         SqlSessionFactory factory = builder.build(inputStream);
14 
15         SqlSession sqlSession = factory.openSession();
16 
17         List<User> list = sqlSession.selectList("test.queryList");
18 
19         list.forEach(System.out::println);
20 
21         sqlSession.close();
22 
23     }

 

SqlMapConfig configuration:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <!DOCTYPE configuration
 3         PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-config.dtd">
 5 <configuration>
 6     <environments default="development">
 7         <environment id="development">
 8             <transactionManager type="JDBC" />
 9             <dataSource type="POOLED">
10                 <property name="driver" value="com.mysql.cj.jdbc.Driver"/>
11                 <property name="url" value="jdbc:mysql://127.0.0.1:3306/mybatis01?serverTimezone=UTC" />
12                 <property name="username" value="root" />
13                 <property name="password" value="root" />
14             </dataSource>
15         </environment>
16     </environments>
17 
18     <mappers>
19         <mapper resource="UserMapper.xml" />
20     </mappers>
21 </configuration>    

 

UserMapper configuration:

 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <!DOCTYPE mapper
 3         PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
 4         "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 5 <mapper namespace="test">
 6 
 7    <select id="queryList" resultType="com.saika.mybatis.pojo.User">
 8       SELECT * from user
 9    </select>
10 
11 </ >Folders

 

Entity class property name table columns username inconsistency

 

Option One

AS SQL statements using aliases from the form, display columns for the query definition of aliases, so that the attribute names and aliases of the same entity class to complete the automatic mapping.

SQL statement:

1 SELECT id,username AS name ,sex,birthday,address FROM user

 

Disadvantages:

1. If the field too much can cause an inconsistent statement is too long.

2. If there are multiple query, every time aliases will lead to an increase in code duplication, multiple definitions display column increases the workload.

 

Option II

The method of modifying entity class method names set correspondence table field name.

MyBatis mapping principle is encapsulated to find a method of setXxx entity class names returned by the field.

Java code:

1     public void setUsername(String name) {
2         this.name = name;
3     }

 

third solution

Using <sql> tag segment extracting SQL statements alias unified configuration, with <include> tag incorporated in the original query, simplify operation.

SQL statement:

1    <sql id="user">
2       id,username AS name,sex,birthday,address
3    </sql>
4 
5    <select id="queryList" resultType="com.saika.mybatis.pojo.User">
6       SELECT <include refid="user"></include> FROM user
7    </select>

 

Option IV

Use <resultMap> tag configuration manual mapping.

id is a unique identifier of this rule package, intended to be referenced in the query tag resultMap.

type package is used to specify the query results to which entity classes.

column table columns.

property is a property of the entity classes.

SQL statement:

1    <resultMap id="userResultMap" type="com.saika.mybatis.pojo.User">
2       <result column="username" property="name"/>
3    </resultMap>
4 
5    <select id="queryList" resultMap="userResultMap">
6       SELECT id,username,sex,birthday,address FROM user
7    </select>

 Notes form:

1 @Result(property ="name",column = "username")

 

 

 

Guess you like

Origin www.cnblogs.com/manchuria-saika/p/11978096.html