Mybaits解决实体类字段与数据库字段不一致问题
public class Employee {
private Integer id;
private String lastName;
private String email;
private String gender;
//setter与getter省略
}
而数据库中的 last_name字段是这样的与实体不一致
@Test
public void test05() throws IOException{
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try{
EmployeeMapperPlus mapper = openSession.getMapper(EmployeeMapperPlus.class);
Employee empById = mapper.getEmpById(1);
System.out.println(empById);
/*Employee empAndDept = mapper.getEmpAndDept(1);
System.out.println(empAndDept);
System.out.println(empAndDept.getDept());*/
// Employee employee = mapper.getEmpByIdStep(3);
// System.out.println(employee);
//System.out.println(employee.getDept());
//System.out.println(employee.getDept());
}finally{
openSession.close();
}
}
在xml里这样配置的
<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById" resultType="com.atguigu.mybatis.bean.Employee">
select * from tbl_employee where id=#{id}
</select>
因为属性和字段不一致对应查询结果如图

导致了lastName没有附上值
如何解决呢,可以在mybaits-confg.xml配置驼峰命名就能解决这一个问题,但是很局限性
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
<setting name="jdbcTypeForNull" value="NULL"/>
<!--显示的指定每个我们需要更改的配置的值,即使他是默认的。防止版本更新带来的问题 -->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
接下来讲解用resultMap解决这一个问题
<resultMap type="com.atguigu.mybatis.bean.Employee" id="MySimpleEmp">
<!--指定主键列的封装规则
id定义主键会底层有优化;
column:指定哪一列
property:指定对应的javaBean属性
-->
<id column="id" property="id"/>
<!-- 定义普通列封装规则 -->
<result column="last_name" property="lastName"/>
<!-- 其他不指定的列会自动封装:我们只要写resultMap就把全部的映射规则都写上。 -->
<result column="email" property="email"/>
<result column="gender" property="gender"/>
</resultMap>
<!-- public Employee getEmpById(Integer id); -->
<select id="getEmpById" resultMap="MySimpleEmp">
select * from tbl_employee where id=#{id}
</select>
执行结果如下:

Mybaits解决实体类字段与数据库字段不一致问题的更多相关文章
- Mybatis实体类属性与数据库字段不一致解决办法
例如:实体类 String userName 数据库:name 解决办法一: 通过给字段加别名,别名写成实体类属性一 eg:select name userName from student ...
- 阶段3 1.Mybatis_05.使用Mybatis完成CRUD_9 Mybatis中的返回值深入-解决实体类属性和数据库列名不对应的两种方式
sql语句里面起别名的方式 测试查询的方法 数据字段 都有值了. 配置查询接口列表和实体类属性名对应关系 id可以随便起名 主键的对应 再次测试,并没有封装成功 这是应为定义的对应关系并没有使用. 当 ...
- mybatis注解开发实体类属性和数据库字段不对应问题
/** * 查询所有用户 * @return */ @Select("select * from user") @Results(id="userMap",va ...
- Mybatis中resultMap的作用-解决实体类属性名和数据库字段不一致
解决实体类属性名和数据库字段不一致
- java实体类和json串字段名称不一致或者与map中字段名称不一致使用注解转化
package yuanCheng; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List ...
- EntityFrameworkCore 根据实体类自动创建数据库
1.首先新建 Asp.Net Core WebApi 项目 2.添加一下引用 : 2.1 Pomelo.EntityFrameworkCore.MySql(我用的Mysql 根据自己情况引用就行) ...
- com.google.gson的SerializedName解决实体类与关键字的重名
使用google的gson包,解决实体类中字段与java关键字的重名: // 比如 当实体类中有switch关键字时,解决冲突如下 @SerializedName("switch" ...
- hibernate映射实体类查询时数据库空字段赋值给实体类报错的问题
因为一直报实体类空异常,去网上查了资料只查到了并没有查到数据库空值时不给实体类赋值的属性 异常 org.hibernate.InvalidMappingException: Could not par ...
- 使用springboot mybatis 查询时实体类中的驼峰字段值为null
看到返回结果以后主要分析了一下情况: 实体类的get.set方法确实 mapper.xml文件中的resultMap.resultType等原因导致 数据库中数据存在问题 经过检查与验证发现以上都不存 ...
随机推荐
- springboot热部署设置
springboot提供了热部署,所谓热部署就是当你修改了文件代码,不用重新去启动服务器,而你只要重新build一下当前项目就可以重新编译了.而这就是热部署. 其实springboot热部署就是通过一 ...
- Bashed -- hack the box
Introduction Target: 10.10.10.68 (OS: Linux) Kali linux: 10.10.16.44 Information Enumeration Firstly ...
- 关于一个function abc() 内 return一个值, 或者多个值写法
1.想return一个值,选第一种写法 function abc(){ a = '我是adad' return a } console.log(abc) // ==> 这个是错的,不要这样写,经 ...
- 2019.10.17 CCSP自闭打铜
很幸运被珠姐第二次抓过来参加CCSP(第二次看xmk夺冠),不过今年的题目还是那么让人心肌梗塞呢(听题解的时候基本都能听懂也算是进步了8). 比赛前一天晚上躺在宾馆里,做梦梦见自己在打比赛,局面应该是 ...
- Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)
E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...
- Nginx 502 Bad Gateway 的错误的解决方案
我用的是nginx反向代理Apache,直接用Apache不会有任何问题,加上nginx就会有部分ajax请求502的错误,下面是我收集到的解决方案. 一.fastcgi缓冲区设置过小 出现错误,首先 ...
- mysql:You can't specify target table 'sessions' for update in FROM clause
更新数据时,在where条件子句里面如果想使用子查询按条件更新部分数据,需要将查询的结果设为临时表.可以参考: https://blog.csdn.net/poetssociety/article/d ...
- SpringBoot 1.x 之入门
1 SpringBoot简介 SpringBoot简化Spring应用开发,约定大于配置,去繁从简,just run就能创建一个独立的,产品级别的应用. 背景: J2EE笨重的开发.繁多的配置.低下的 ...
- websocket和通信
最近默默的在学websocket,推荐的当然是阮一峰的博客了,其中也学到了不少 可以去看看咯 http://www.ruanyifeng.com/blog/2017/05/websocket.htm ...
- MySQL——复制(Replication)
1.复制概述 1.1.复制解决的问题数据复制技术有以下一些特点:(1) 数据分布(2) 负载平衡(load balancing)(3) 备份(4) 高可用性(high avai ...