解决数据库字段与实体类属性名不一致问题

细心地小伙伴们会注意到,查出来的数据有的字段可能是null,比如查询单个用户返回的数据为:

  1. {
  2. "id": 19,
  3. "username": "test",
  4. "password": "abc",
  5. "nickname": "t",
  6. "phone": "18300000001",
  7. "status": 1,
  8. "createTime": null,
  9. "icon": "",
  10. "gender": 2,
  11. "birthday": "2020-03-24T00:00:00.000+0000"
  12. }

这个地方,数据库中的字段是create_time,而我们定义的实体属性为createTime,所以查出的数据为null。

针对这个问题,有好几种解决方案。

方案一:使SQL语句使用别名

  1. <select id="list" resultType="com.example.test.model.User">
  2. SELECT
  3. u.id,
  4. u.username,
  5. u.password,
  6. u.nickname,
  7. u.phone,
  8. u.status,
  9. u.icon,
  10. u.create_time as createTime,
  11. u.gender,
  12. u.birthday
  13. from user u
  14. </select>

方案二:使用resultMap映射字段名

<resultMap id="BaseResultMap" type="com.example.test.model.User">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="password" jdbcType="VARCHAR" property="password" />
    <result column="nickname" jdbcType="VARCHAR" property="nickname" />
    <result column="phone" jdbcType="VARCHAR" property="phone" />
    <result column="status" jdbcType="INTEGER" property="status" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="icon" jdbcType="VARCHAR" property="icon" />
    <result column="gender" jdbcType="INTEGER" property="gender" />
    <result column="birthday" jdbcType="DATE" property="birthday" />
</resultMap>
<select id="list" resultMap="BaseResultMap">
    SELECT * from user u
</select>

方案三:配置驼峰规则

配置:

mybatis:
  mapper-locations:
    - classpath:mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true # 开启下划线转驼峰

开启后,返回的结果应该为如下形式:

{
    "id": 19,
    "username": "test",
    "password": "abc",
    "nickname": "t",
    "phone": "18300000001",
    "status": 1,
    "createTime": "2020-03-24T11:52:34.000+0000",
    "icon": "",
    "gender": 2,
    "birthday": "2020-03-24T00:00:00.000+0000"
}

如果有多条数据,只能查出第一条数据

如下mapper,只能查出第一条数据:

<resultMap id="BaseResultMap" type="com.example.test.model.User">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    ...
    <collection property="orders" javaType="List" ofType="com.example.test.model.Order" autoMapping="true">
        <id column="id" property="id"/>
    </collection>
</resultMap>

<select id="query" resultMap="BaseResultMap">
    SELECT * from user u
    LEFT JOIN `order` o on u.id = o.user_id
    where u.id=#{id}
</select>

原因:主表和关联表的id字段名相同造成的

解决方案:为主表或者关联表的id取个别名

详细代码参看:
语雀内容

参考:https://blog.csdn.net/weixin_34189116/article/details/90277936

Property ‘configuration’ and ‘configLocation’ can not specified with together

错误详情:

Caused by: java.lang.IllegalStateException: Property 'configuration' and 'configLocation' can not specified with together

错误原因:
在配置文件中同时指定了 config-locationconfiguration

mybatis:
  type-aliases-package: com.example.mybatisxml.entity
  config-location: classpath:mybatis/mybatis-config.xml
  mapper-locations: classpath:mybatis/mapper/*.xml
  configuration:
    map-underscore-to-camel-case: true

解决方案:
删除配置文件中的 configuration 字段,将其添加到 config-location 所在的MyBatis配置文件中:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <typeAlias alias="Integer" type="java.lang.Integer"/>
        <typeAlias alias="Long" type="java.lang.Long"/>
        <typeAlias alias="HashMap" type="java.util.HashMap"/>
        <typeAlias alias="LinkedHashMap" type="java.util.LinkedHashMap"/>
        <typeAlias alias="ArrayList" type="java.util.ArrayList"/>
        <typeAlias alias="LinkedList" type="java.util.LinkedList"/>
    </typeAliases>
</configuration>

Could not autowire. No beans of ‘xxx’ type found

@Autowired 中报红,虽然不影响运行,但影响视觉:
Snipaste_2021-01-06_21-37-37.webp
错误详情:

Could not autowire. No beans of 'User1Mapper' type found

解决方案:
在Mapper中添加 @Repository 注解即可:
Snipaste_2021-01-06_21-39-40.webp