解决实体类属性与表字段名不一致的问题

如果实体类属性与表字段名不一致时,会出现对象为null,如何处理?

有三种解决方法:

准备工作:

项目工程如下:

解决实体类属性与表字段名不一致的问题_第1张图片

1.引用依赖


    
      junit
      junit
      4.12
      test
    

    
    
      javax.servlet
      javax.servlet-api
      4.0.0
      provided
    

    
    
      mysql
      mysql-connector-java
      5.1.38
    

    
    
      org.mybatis
      mybatis
      3.4.6
    

2.创建数据库表

#创建地域表
CREATE TABLE CH_REGION (
  ID BIGINT(10) NOT NULL AUTO_INCREMENT,	#ID号
  PARENT_ID BIGINT(10) DEFAULT NULL,			#父级ID号
  REGION_ID BIGINT(10) DEFAULT NULL,			#地域编号
  REGION_PARENT_ID BIGINT(10) DEFAULT NULL,	#地域父级编号
  REGION_NAME VARCHAR(100) DEFAULT NULL,		#地域名称
  REGION_TYPE INT(11) DEFAULT NULL COMMENT '1省 2市 3区',	#地域类型
  PRIMARY KEY (ID)
) ENGINE=INNODB AUTO_INCREMENT=11186 DEFAULT CHARSET=utf8 COMMENT='地区信息表';

3.创建实体类

package com.liuYongQi.MyBatisTest2.pojo;

import java.io.Serializable;

/**
 * @ClassName: Region
 * @Description: TODO 地域表实体类
 * @Author: Administrator
 * @CreateDate: 2018/10/8 10:05
 * @UpdateUser: Administrator
 * @UpdateDate: 2018/10/8 10:05
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */

public class Region implements Serializable {
    private Integer id;                 //ID号
    private Integer parentId;           //父级ID号
    private Integer regionId;           //地域编号
    private Integer regionParentId;     //地域父级编号
    private String regionName;          //地域名称
    private Integer regionType;         //地域类型

    public Region(Integer id, Integer parentId, Integer regionId, Integer regionParentId, String regionName, Integer regionType) {
        this.id = id;
        this.parentId = parentId;
        this.regionId = regionId;
        this.regionParentId = regionParentId;
        this.regionName = regionName;
        this.regionType = regionType;
    }

    public Region(Integer parentId, Integer regionId, Integer regionParentId, String regionName, Integer regionType) {
        this.parentId = parentId;
        this.regionId = regionId;
        this.regionParentId = regionParentId;
        this.regionName = regionName;
        this.regionType = regionType;
    }

    public Region() {
    }

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Integer getParentId() {
        return parentId;
    }

    public void setParentId(Integer parentId) {
        this.parentId = parentId;
    }

    public Integer getRegionId() {
        return regionId;
    }

    public void setRegionId(Integer regionId) {
        this.regionId = regionId;
    }

    public Integer getRegionParentId() {
        return regionParentId;
    }

    public void setRegionParentId(Integer regionParentId) {
        this.regionParentId = regionParentId;
    }

    public String getRegionName() {
        return regionName;
    }

    public void setRegionName(String regionName) {
        this.regionName = regionName;
    }

    public Integer getRegionType() {
        return regionType;
    }

    public void setRegionType(Integer regionType) {
        this.regionType = regionType;
    }

    @Override
    public String toString() {
        return "Region{" +
                "id=" + id +
                ", parentId=" + parentId +
                ", regionId=" + regionId +
                ", regionParentId=" + regionParentId +
                ", regionName='" + regionName + '\'' +
                ", regionType=" + regionType +
                '}';
    }
}

 4.创建地域表实体类接口

package com.liuYongQi.MyBatisTest2.dao;

import com.liuYongQi.MyBatisTest2.pojo.Region;

/**
 * @ClassName: IRegionDao
 * @Description: TODO 地域表实体类接口
 * @Author: Administrator
 * @CreateDate: 2018/10/8 10:10
 * @UpdateUser: Administrator
 * @UpdateDate: 2018/10/8 10:10
 * @UpdateRemark: 修改内容
 * @Version: 1.0
 */
public interface IRegionDao {
    /**
     * @Author Administrator
     * @Description //TODO 根据地域名称查询地域对象信息
     * @Date 10:16 2018/10/8
     * @Param [regionName]
     * @return com.liuYongQi.MyBatisTest2.pojo.Region
     * @exception
     */
    public Region selectRegionByRegionName(String regionName);
}

5.创建MyBatisConfig.xml




    
        
        

        
        
        
    

    
    
        
        
            
            

            
            
                
                
                
                
            
        
    

    
    
        
    

6.创建映射文件






    
    


7.创建测试类

package com.liuYongQi.MyBatisTest2.test;

import com.liuYongQi.MyBatisTest2.dao.IRegionDao;
import com.liuYongQi.MyBatisTest2.pojo.Region;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class TestCRUD {
    //获得SqlSession对象,通过SqlSession操作CRUD
    private SqlSession sqlSession;
    private IRegionDao iregionDao;
    @Before
    public void before(){
        //读取配置文件,获取SQLSessionFactory
        try {
            //通过Resources类加载核心配置文件,得到文件的输入流
            InputStream inputStream = Resources.getResourceAsStream("MyBatisConfig.xml");
            //创建会话工厂,编译配置文件流,sqlSessionFactory
            SqlSessionFactory sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
            //通过sqlSessionFactory得到sqlSession象
            sqlSession = sqlSessionFactory.openSession();
            //通过sqlSession获得映射实例类的父接口
            iregionDao = sqlSession.getMapper(IRegionDao.class);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @After
    public void after(){
        sqlSession.commit();
    }

    @Test
    public void selectRegionByRegionName(){
        Region region = iregionDao.selectRegionByRegionName("湖南省");
        System.out.println(region);
    }
    

















}

结果为:

解决实体类属性与表字段名不一致的问题_第2张图片

解决方案如下:

方法一:在SQL语句中为列名取别名(与属性名一致)



方法二:resultMap指定映射关系,MyBatis也能自动完成映射




    
    
    
    
    
    

解决实体类属性与表字段名不一致的问题_第3张图片

方法三:设置全局属性(推荐)

通常情况下,Java中的实体类属性采用骆峰命名,而数据库中表的字段则用下划线区分字母。在这种情况下MyBatis提供了一个全局属性mapUnderscoreToCamelCase来解决两者名字不一致的问题。

在mybatisConfig.xml中配置:


    

三种方案结果都为:

今天的测试就到这里了,谢谢大家的支持!

如果大家想浏览我的下一篇文章,请留言

版权声明:此文章属于原创,不准随意转载:https://blog.csdn.net/LYQ2332826438

 

 

 

你可能感兴趣的:(java,代码示例,框架,案例,MyBatis,问题)