Chinaunix首页 | 论坛 | 博客
  • 博客访问: 54799
  • 博文数量: 73
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 425
  • 用 户 组: 普通用户
  • 注册时间: 2015-01-05 15:44
文章分类
文章存档

2015年(73)

我的朋友

分类: Java

2015-01-07 09:34:22

在开发的时候应该遇到这样的情况,数据库中的字段名与属性名不一致的情况,通常数据库中的字段命名时多个单词之间使用下划线连接在一起的,而在类中的属性名则多数是用驼峰标识的命名方式,我见过的大多数都是这样,那么使用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.   
  2. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"  
  3. "" >   
  4. < configuration >   
  5.        
  6.      < typeAliases >   
  7.          < typeAlias   type = "com.tenghu.mybatis.model.Department"   alias = "Department" />   
  8.        
  9.      < environments   default = "development" >   
  10.          < environment   id = "development" >   
  11.              < transactionManager   type = "JDBC" />   
  12.              < dataSource   type = "POOLED" >   
  13.                  < property   name = "driver"   value = "${jdbc.driver}" />   
  14.                  < property   name = "url"   value = "${jdbc.url}" />   
  15.                  < property   name = "username"   value = "${jdbc.username}" />   
  16.                  < property   name = "password"   value = "${jdbc.password}" />   
  17.                
  18.            
  19.        
  20.       
  21.        
  22.      < mappers >   
  23.          < mapper   resource = "com/tenghu/mybatis/model/xml/DepartmentMapper.xml" />   
  24.        
  25.   

映射文件:

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

  1.   
  2. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"  
  3. "" >   
  4. < mapper   namespace = "com.tenghu.mybatis.model.xml.DepartmentMapper" >   
  5.       
  6.        
  7.      < resultMap   type = "Department"   id = "tab_department" >   
  8.          < id   property = "ids"   column = "ids" />   
  9.          < result   property = "deName"   column = "de_name" />   
  10.          < result   property = "pIds"   column = "p_ids" />   
  11.          < result   property = "deChargePerson"   column = "de_charge_person" />   
  12.          < result   property = "createTime"   column = "create_time" />   
  13.        
  14.       
  15.        
  16.      < select   id = "queryAllDepartment"   resultMap = "tab_department" >   
  17.         select * from tab_department  
  18.        
  19.   

工具类:

[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 .util.List;  
  4.   
  5. import org..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.         } ch (Exception e) {  
  35.             e.printStackTrace();  
  36.         }finally{  
  37.             //关闭SqlSession  
  38.             MybatisUtil.closeSession(sqlSession);  
  39.         }  
  40.     }  
  41. }  

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

  • 本文来自:
  • 本文链接:
阅读(253) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~