1.问题背景

最近在使用SpringBoot项目连接数据库使用mybatis进行实体查询时遇到的这个问题,实体类创建如下:

 

最后请求到url地址上发现实体类并没有封装到POJO上:

 

 这里汇总一下这个问题的解决方法,大致有三种。

 

1.1.查询的sql语句中使用别名进行查询

 

1.2.使用resultMap映射实体

<resultMap id="deptMap" type="dept">
    <!-- property: 实体类属性名.
         column: 库中表的列名
        javaType: 数据类型.
    -->
    <id property="deptno" column="deptno" javaType="long"></id>
    <result property="dname" column="dname"></result>
    <result property="dbSource" column="db_source"></result>
</resultMap>

<select id="queryById" parameterType="long" resultMap="deptMap">
        select * from springcloud_db01.dept where deptno = #{deptno}
</select>

 

1.3.开启SpringBoot-Mybatis驼峰命令配置

在application.yml中开启驼峰命名识别,注意不能和mybatis-config.xml一起使用。这里有个坑(解决参考博文:博文链接),可以直接使用下面的配置避坑:

mybatis:
  #config-location: classpath:mybatis/mybatis-config.xml
  type-aliases-package: com.fengye.springcloud.pojo
  mapper-locations: classpath:mybatis/mapper/*Mapper.xml
  #注意config-location: classpath:mybatis/mybatis-config.xml不能与
  #驼峰一起使用
#开启实体类与数据库映射驼峰命令识别 configuration: map-underscore-to-camel-case: true

 

最终问题解决:

 

以上三种方式可以根据实际开发场景进行选择,比如我这种情况就可以直接选择第三种方式,比较简单。

 

问题解决参考博客链接如下:

SpringBoot Mybatis 的驼峰命名 开启驼峰命名的方法

MyBatis - 实体类的属性名和数据库列名不一致时的两种解决办法!

解决:java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified

 

posted on 2021-03-31 15:11  人无名,则可专心练剑  阅读(692)  评论(0编辑  收藏  举报