`
aijuans
  • 浏览: 1547769 次
社区版块
存档分类
最新评论

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

阅读更多

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

数据表:

 

[html] view plaincopy
 
  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 plaincopy
 
  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 plaincopy
 
  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 plaincopy在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 plaincopy在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 plaincopy在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. }  


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

 

其他精彩文章文章

jQuery教程(9)-DOM树操作之复制元素

android学习笔记(35)android AlertDialog创建列表对话框[2]

android shareSDK sso登录新浪和微信

mysql 索引类型详解-B-Tree索引

BroadcastReceiver 使用AlertDialog后 app奔溃了

 

更多关于android开发文章

3
9
分享到:
评论
2 楼 baggio_hk 2015-01-06  
这种也能推荐上来?
1 楼 tanliansheng 2015-01-06  
好搞笑   

相关推荐

Global site tag (gtag.js) - Google Analytics