设为首页 加入收藏

TOP

Mybatis解决属性名与字段名不一致
2019-05-12 14:15:01 】 浏览:142
Tags:Mybatis 解决 属性 段名不 一致

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

数据表:

[html]view plaincopy

  1. CREATETABLEtab_department(
  2. idsINTPRIMARYKEYAUTO_INCREMENT,
  3. de_nameVARCHAR(50)COMMENT'部门名称',
  4. p_idsINTCOMMENT'上级部门id',
  5. de_charge_personVARCHAR(50)COMMENT'部门负责人',
  6. create_timeLONGCOMMENT'创建时间'
  7. )COMMENT'部门表'

实体类:

[html]view plaincopy

  1. packagecom.tenghu.mybatis.model;
  2. importjava.io.Serializable;
  3. /**
  4. *部门表
  5. *@authorArvin_Li
  6. *
  7. */
  8. publicclassDepartmentimplementsSerializable{
  9. privatestaticfinallong serialVersionUID = 6998332095922284289L ;
  10. privateintids;//部门编号
  11. privateStringdeName;//部门名称
  12. privateintpIds;//上级部门id
  13. privateStringdeChargePerson;//部门负责人
  14. privatelongcreateTime;//创建时间
  15. //省略get和set方法
  16. }

mybatis主配置文件:

[html]view plaincopy

  1. < xml version = "1.0" encoding = "UTF-8" >
  2. <!DOCTYPEconfiguration
  3. PUBLIC"-//mybatis.org//DTDConfig3.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. < mappers >
  23. < mapper resource = "com/tenghu/mybatis/model/xml/DepartmentMapper.xml" />
  24. </ mappers >
  25. </ configuration >

映射文件:

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

  1. < xml version = "1.0" encoding = "UTF-8" >
  2. <!DOCTYPEmapper
  3. PUBLIC"-//mybatis.org//DTDMapper3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd" >
  5. < mapper namespace = "com.tenghu.mybatis.model.xml.DepartmentMapper" >
  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. </ resultMap >
  14. <!--查询所有部门-->
  15. < select id = "queryAllDepartment" resultMap = "tab_department" >
  16. select*fromtab_department
  17. </ select >
  18. </ mapper >

工具类:

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

  1. packagecom.tenghu.mybatis.util;
  2. importjava.io.IOException;
  3. importjava.io.InputStream;
  4. importjava.util.Properties;
  5. importorg.apache.ibatis.io.Resources;
  6. importorg.apache.ibatis.session.SqlSession;
  7. importorg.apache.ibatis.session.SqlSessionFactory;
  8. importorg.apache.ibatis.session.SqlSessionFactoryBuilder;
  9. /**
  10. *Mybatis工具类
  11. *@authorArvin_Li
  12. *@version1.0
  13. *
  14. */
  15. publicclassMybatisUtil{
  16. privateMybatisUtil(){}
  17. //声明SqlSession工厂
  18. privatestaticSqlSessionFactorysqlSessionFactory;
  19. //使用静态代码块获取SqlSession工厂
  20. static{
  21. try{
  22. //获取mybatis主配置文件流
  23. InputStream inputStream = Resources .getResourceAsStream("mybatis-config.xml");
  24. //创建属性文件对象
  25. Properties properties = new Properties();
  26. //加载属性配置文件
  27. properties.load(Resources.getResourceAsStream("jdbc.properties"));
  28. //创建SqlSessionFactory对象
  29. sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream,properties);
  30. }catch(IOExceptione){
  31. e.printStackTrace();
  32. }
  33. }
  34. /**
  35. *开启SqlSession对象,不自动提交
  36. *@return
  37. */
  38. publicstaticSqlSessionopenSession(){
  39. returnsqlSessionFactory.openSession();
  40. }
  41. /**
  42. *开启自动提交的SqlSession
  43. *@return
  44. */
  45. publicstaticSqlSessionopenAutoCommitSession(){
  46. returnsqlSessionFactory.openSession(true);
  47. }
  48. /**
  49. *关闭SqlSession
  50. *@paramsqlSession
  51. */
  52. publicstaticvoidcloseSession(SqlSessionsqlSession){
  53. if(null!=sqlSession){
  54. sqlSession.close();
  55. }
  56. sqlSession = null ;
  57. }
  58. }

测试类:

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

  1. packagecom.tenghu.mybatis.test;
  2. importjava.util.List;
  3. importorg.apache.ibatis.session.SqlSession;
  4. importorg.junit.Test;
  5. importcom.tenghu.mybatis.model.Department;
  6. importcom.tenghu.mybatis.util.MybatisUtil;
  7. /**
  8. *部门测试类
  9. *@authorArvin_Li
  10. *
  11. */
  12. publicclassDepartmentTest{
  13. //命名空间
  14. privateString namespace = "com.tenghu.mybatis.model.xml.DepartmentMapper." ;
  15. /**
  16. *查询出所有部门
  17. */
  18. @Test
  19. publicvoidtestQueryAllDepartment(){
  20. //获取SqlSession
  21. SqlSession sqlSession = MybatisUtil .openSession();
  22. try{
  23. //查询
  24. List < Department > departList = sqlSession .selectList(namespace+"queryAllDepartment");
  25. //输出部门信息
  26. for(Departmentdepartment:departList){
  27. System.out.println(department.getIds()+"\t"+department.getDeName()+"\t"+department.getpIds()+"\t"+department.getDeChargePerson());
  28. }
  29. }catch(Exceptione){
  30. e.printStackTrace();
  31. }finally{
  32. //关闭SqlSession
  33. MybatisUtil.closeSession(sqlSession);
  34. }
  35. }
  36. }

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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Flume 自定义source   -- S.. 下一篇【Flume】文件收集框架Flume

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目