MyBatis实体类属性与表字段不一致的4种解决方案
Posted manchuria-saika
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了MyBatis实体类属性与表字段不一致的4种解决方案相关的知识,希望对你有一定的参考价值。
pom的依赖配置:
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数据库的建表语句:
1 CREATE TABLE `tb_user` ( 2 `id` int(11) NOT NULL AUTO_INCREMENT, 3 `username` varchar(32) NOT NULL COMMENT ‘用户名‘, 4 `sex` char(1) DEFAULT NULL COMMENT ‘性别‘, 5 `birthday` date DEFAULT NULL COMMENT ‘生日‘, 6 `address` varchar(256) DEFAULT NULL COMMENT ‘地址‘, 7 PRIMARY KEY (`id`) 8 ) ;
Java实体类代码:
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测试方法代码:
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的配置:
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的配置:
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 </mapper>
实体类属性name与表字段username不一致
方案一
采用SQL语句AS起别名的形式,为查询语句的显示列定义别名,让这个别名与实体类的属性名相同以完成自动映射。
SQL语句:
1 SELECT id,username AS name ,sex,birthday,address FROM user
缺点:
1.如果不一致的字段过多会导致语句过长。
2.如果查询语句有多个,每次都要起别名会导致重复代码增多,多次定义显示列增加了工作量。
方案二
修改实体类set方法的方法名对应表字段名。
MyBatis映射的原理就是通过返回的字段名找实体类的setXxx方法进行封装。
Java代码:
1 public void setUsername(String name) { 2 this.name = name; 3 }
方案三
利用<sql>标签提取SQL语句片段统一配置别名,在原查询语句上用<include>标签引入,简化操作。
SQL语句:
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>
方案四
使用<resultMap>标签配置手动映射。
id 是这个封装规则的唯一标识,用于被查询标签中的resultMap所引用。
type 用于指定将查询结果封装到哪个实体类中。
column是表的字段。
property是实体类的属性。
SQL语句:
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>
注解形式:
1 @Result(property ="name",column = "username")
以上是关于MyBatis实体类属性与表字段不一致的4种解决方案的主要内容,如果未能解决你的问题,请参考以下文章
Mybatis——实体类属性名和数据库字段名不同时的解决办法