Mybatis解决属性名与字段名不一致

在开发的时候应该遇到这样的情况,数据库中的字段名与属性名不一致的情况,通常数据库中的字段命名时多个单词之间使用下划线连接在一起的,而在类中的属性名则多数是用驼峰标识的命名方式,我见过的大多数都是这样,那么使用mybatis该如果解决这一的问题呢?如下:

数据表:

[html] view plain copy

  1. CREATE TABLE tab_department(  
  2.     ids INT PRIMARY KEY AUTO_INCREMENT,  
  3.     de_name VARCHAR(50) COMMENT '部门名称',  
  4.     p_ids INT COMMENT '上级部门id',  
  5.     de_charge_person VARCHAR(50) COMMENT '部门负责人',  
  6.     create_time LONG COMMENT '创建时间'  
  7. ) COMMENT '部门表'  

实体类:

[html] view plain copy

  1. package com.tenghu.mybatis.model;  
  2.   
  3. import java.io.Serializable;  
  4.   
  5. /**  
  6.  * 部门表  
  7.  * @author Arvin_Li  
  8.  *  
  9.  */  
  10. public class Department implements Serializable{  
  11.     private static final long  serialVersionUID  =  6998332095922284289L ;  
  12.       
  13.     private int ids;//部门编号  
  14.     private String deName;//部门名称  
  15.     private int pIds;//上级部门id  
  16.     private String deChargePerson;//部门负责人  
  17.     private long createTime;//创建时间  
  18.       
  19.     //省略get和set方法  
  20. }  

mybatis主配置文件:

[html] view plain copy

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE configuration  
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd" >   
  5. < configuration >   
  6.      <!-- 配置实体类别名 -->   
  7.      < typeAliases >   
  8.          < typeAlias   type = "com.tenghu.mybatis.model.Department"   alias = "Department" />   
  9.      </ typeAliases >   
  10.      < environments   default = "development" >   
  11.          < environment   id = "development" >   
  12.              < transactionManager   type = "JDBC" />   
  13.              < dataSource   type = "POOLED" >   
  14.                  < property   name = "driver"   value = "${jdbc.driver}" />   
  15.                  < property   name = "url"   value = "${jdbc.url}" />   
  16.                  < property   name = "username"   value = "${jdbc.username}" />   
  17.                  < property   name = "password"   value = "${jdbc.password}" />   
  18.              </ dataSource >   
  19.          </ environment >   
  20.      </ environments >   
  21.       
  22.      <!-- 配置映射文件 -->   
  23.      < mappers >   
  24.          < mapper   resource = "com/tenghu/mybatis/model/xml/DepartmentMapper.xml" />   
  25.      </ mappers >   
  26. </ configuration >   

映射文件:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. <? xml   version = "1.0"   encoding = "UTF-8" ?>   
  2. <!DOCTYPE mapper  
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >   
  5. < mapper   namespace = "com.tenghu.mybatis.model.xml.DepartmentMapper" >   
  6.       
  7.      <!-- 配置映射字段 -->   
  8.      < resultMap   type = "Department"   id = "tab_department" >   
  9.          < id   property = "ids"   column = "ids" />   
  10.          < result   property = "deName"   column = "de_name" />   
  11.          < result   property = "pIds"   column = "p_ids" />   
  12.          < result   property = "deChargePerson"   column = "de_charge_person" />   
  13.          < result   property = "createTime"   column = "create_time" />   
  14.      </ resultMap >   
  15.       
  16.      <!-- 查询所有部门 -->   
  17.      < select   id = "queryAllDepartment"   resultMap = "tab_department" >   
  18.         select * from tab_department  
  19.      </ select >   
  20. </ mapper >   

工具类:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. package com.tenghu.mybatis.util;  
  2.   
  3. import java.io.IOException;  
  4. import java.io.InputStream;  
  5. import java.util.Properties;  
  6.   
  7. import org.apache.ibatis.io.Resources;  
  8. import org.apache.ibatis.session.SqlSession;  
  9. import org.apache.ibatis.session.SqlSessionFactory;  
  10. import org.apache.ibatis.session.SqlSessionFactoryBuilder;  
  11.   
  12. /**  
  13.  * Mybatis工具类  
  14.  * @author Arvin_Li  
  15.  * @version 1.0  
  16.  *  
  17.  */  
  18. public class MybatisUtil {  
  19.     private MybatisUtil(){}  
  20.       
  21.     //声明SqlSession工厂  
  22.     private static SqlSessionFactory sqlSessionFactory;  
  23.       
  24.     //使用静态代码块获取SqlSession工厂  
  25.     static{  
  26.         try {  
  27.             //获取mybatis主配置文件流  
  28.             InputStream  inputStream = Resources .getResourceAsStream("mybatis-config.xml");  
  29.             //创建属性文件对象  
  30.             Properties  properties = new  Properties();  
  31.             //加载属性配置文件  
  32.             properties.load(Resources.getResourceAsStream("jdbc.properties"));  
  33.             //创建SqlSessionFactory对象  
  34.              sqlSessionFactory = new  SqlSessionFactoryBuilder().build(inputStream, properties);  
  35.         } catch (IOException e) {  
  36.             e.printStackTrace();  
  37.         }  
  38.     }  
  39.       
  40.     /**  
  41.      * 开启SqlSession对象,不自动提交  
  42.      * @return  
  43.      */  
  44.     public static SqlSession openSession(){  
  45.         return sqlSessionFactory.openSession();  
  46.     }  
  47.       
  48.     /**  
  49.      * 开启自动提交的SqlSession  
  50.      * @return  
  51.      */  
  52.     public static SqlSession openAutoCommitSession(){  
  53.         return sqlSessionFactory.openSession(true);  
  54.     }  
  55.       
  56.     /**  
  57.      * 关闭SqlSession  
  58.      * @param sqlSession  
  59.      */  
  60.     public static void closeSession(SqlSession sqlSession){  
  61.         if(null!=sqlSession){  
  62.             sqlSession.close();  
  63.         }  
  64.          sqlSession = null ;  
  65.     }  
  66. }  

测试类:

[html] view plain copy 在CODE上查看代码片 派生到我的代码片

  1. package com.tenghu.mybatis.test;  
  2.   
  3. import java.util.List;  
  4.   
  5. import org.apache.ibatis.session.SqlSession;  
  6. import org.junit.Test;  
  7.   
  8. import com.tenghu.mybatis.model.Department;  
  9. import com.tenghu.mybatis.util.MybatisUtil;  
  10.   
  11. /**  
  12.  * 部门测试类  
  13.  * @author Arvin_Li  
  14.  *  
  15.  */  
  16. public class DepartmentTest {  
  17.     //命名空间  
  18.     private String  namespace = "com.tenghu.mybatis.model.xml.DepartmentMapper." ;  
  19.       
  20.     /**  
  21.      * 查询出所有部门  
  22.      */  
  23.     @Test  
  24.     public void testQueryAllDepartment(){  
  25.         //获取SqlSession  
  26.         SqlSession  sqlSession = MybatisUtil .openSession();  
  27.         try {  
  28.             //查询  
  29.             List < Department >   departList = sqlSession .selectList(namespace+"queryAllDepartment");  
  30.             //输出部门信息  
  31.             for (Department department : departList) {  
  32.                 System.out.println(department.getIds()+"\t"+department.getDeName()+"\t"+department.getpIds()+"\t"+department.getDeChargePerson());  
  33.             }  
  34.         } catch (Exception e) {  
  35.             e.printStackTrace();  
  36.         }finally{  
  37.             //关闭SqlSession  
  38.             MybatisUtil.closeSession(sqlSession);  
  39.         }  
  40.     }  
  41. }  

这样就可以处理字段名与属性名不一致的情况了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值