MyBatis中当实体类中的属性名和表中的字段名不一样,怎么办

方法1: 在mybatis核心配置文件中指定,springboot加载mybatis核心配置文件

springboot项目的一个特点就是0配置,本来就省掉了mybatis的核心配置文件,现在又加回去算什么事,总之这种方式可行但没人这样用

具体操作:

①创建mybatis核心配置文件,放在resources下,设置setting标签,开启驼峰命名

comjavasm.spring bOOt.m a p per  AdminMapper.xml  config

2  3  4  5  6  9  application.yml x springboot  x ÅMybatisConfig.java  configuration  PUBLIC --//mybatis. org//DTD Config 3. O//EN"  'http://mybatis- dtd¯ >  mybatis-config.xml  (settings)  (setting name=  settings)  gura 10K'  mapUnderscoreToCame1Case"

②在springboot的yml配置文件中配置mybatis核心配置文件

1

2

mybatis:

    config-location: classpath:config/mybatis-config.xml

方法2: 在springboot的配置文件中指定(常用)

mybatis都被整合到springboot项目中了,自然属性都被springboot自动配置了,现在的情况就类似于我们要去修改自动配置好的属性

我们只需要在springboot的配置文件中设置一下就行了

mybati s :  mapperlocations: classpath: / mapper/*. xml  confi gurati on :  map—underscore—to—camel—case: true

1

2

3

mybatis:

  configuration:

    map-underscore-to-camel-case: true

方法3: 写一个配置类 自定义注册器

除了修改属性,也可以直接写一个配置类,在类中重写方法,让springboot配置mybatis时运行我们自定义的方法(自定义注册器)而不去运行默认方法

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

@Configuration

public class MybatisConfig {

@Bean

public ConfigurationCustomizer configurationCustomizer(){

    return new ConfigurationCustomizer() {

        @Override

        public void customize(org.apache.ibatis.session.Configuration configuration) {

            configuration.setLazyLoadingEnabled(true);

            configuration.setMapUnderscoreToCamelCase(true);

            configuration.setLogImpl(Log4jImpl.class);

                }

            };

        }

}

方法4:起别名。

数据库表的字段的别名就起为实体类中对应属性名。

select emp_id as empId,emp_name as empName,emp_sal as salary from t_emp

方法5:进行ResultMap映射。

一次性定义,重复使用,避免重复起别名。

<resultMap id="employeeMap" type="com.atguigu.mybatis.entity.Employee">

    <!-- 使用id标签设置主键列和主键属性之间的对应关系 -->

    <!-- column属性用于指定字段名;property属性用于指定Java实体类属性名 -->

    <id column="emp_id" property="empId"/>   

    <!-- 使用result标签设置普通字段和Java实体类属性之间的关系 -->

    <result column="emp_name" property="empName"/>

    <result column="emp_salary" property="empSalary"/>

</resultMap>

<select id="selectEmployeeByRM" resultMap="employeeMap">

    select emp_id,emp_name,emp_salary from t_emp where emp_id=#{empId}

</select>

Mybatis-plus方法

1.实体类添加注解

主要注释到实体类上的注解:

@TableName(value = …)
当数据库名与实体类名不一致或不符合驼峰命名时,需要在此注解指定表名

主键自增的设定:

@TableId(type = IdType.AUTO)
指定实体类的属性为对应的主键

@TableField注解

1、 主要用来解决实体类的字段名与数据库中的字段名不匹配的问题(数据库user_addr,字段useraddr未驼峰,mp默认开启驼峰)
2. 实体类中的属性字段在表中不存在的问题

1

2

3

4

// 用来解决数据库中的字段和实体类的字段不匹配问题

@TableField(value = "age")

// 用来解决实体类中有的属性但是数据表中没有的字段

@TableField(exist = false// 默认为true

2.在springboot的配置文件中指定(常用)

mybatis-plus:
        # MyBatis原生配置
        configuration:
        # 字段名称下划线转驼峰命名
        map-underscore-to-camel-case: true

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

穗余

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值