在开发的时候应该遇到这样的情况,数据库中的字段名与属性名不一致的情况,通常数据库中的字段命名时多个单词之间使用下划线连接在一起的,而在类中的属性名则多数是用驼峰标识的命名方式,我见过的大多数都是这样,那么使用mybatis该如果解决这一的问题呢?如下:
数据表:
- CREATE TABLE tab_department(
- ids INT PRIMARY KEY AUTO_INCREMENT,
- de_name VARCHAR(50) COMMENT '部门名称',
- p_ids INT COMMENT '上级部门id',
- de_charge_person VARCHAR(50) COMMENT '部门负责人',
- create_time LONG COMMENT '创建时间'
- ) COMMENT '部门表'
实体类:
- package com.tenghu.mybatis.model;
- import java.io.Serializable;
- /**
- * 部门表
- * @author Arvin_Li
- *
- */
- public class Department implements Serializable{
- private static final long serialVersionUID = 6998332095922284289L;
- private int ids;//部门编号
- private String deName;//部门名称
- private int pIds;//上级部门id
- private String deChargePerson;//部门负责人
- private long createTime;//创建时间
- //省略get和set方法
- }
mybatis主配置文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE configuration
- PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-config.dtd">
- <configuration>
- <!-- 配置实体类别名 -->
- <typeAliases>
- <typeAlias type="com.tenghu.mybatis.model.Department" alias="Department"/>
- </typeAliases>
- <environments default="development">
- <environment id="development">
- <transactionManager type="JDBC"/>
- <dataSource type="POOLED">
- <property name="driver" value="${jdbc.driver}"/>
- <property name="url" value="${jdbc.url}"/>
- <property name="username" value="${jdbc.username}"/>
- <property name="password" value="${jdbc.password}"/>
- </dataSource>
- </environment>
- </environments>
- <!-- 配置映射文件 -->
- <mappers>
- <mapper resource="com/tenghu/mybatis/model/xml/DepartmentMapper.xml"/>
- </mappers>
- </configuration>
映射文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE mapper
- PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
- "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
- <mapper namespace="com.tenghu.mybatis.model.xml.DepartmentMapper">
- <!-- 配置映射字段 -->
- <resultMap type="Department" id="tab_department">
- <id property="ids" column="ids"/>
- <result property="deName" column="de_name"/>
- <result property="pIds" column="p_ids"/>
- <result property="deChargePerson" column="de_charge_person"/>
- <result property="createTime" column="create_time"/>
- </resultMap>
- <!-- 查询所有部门 -->
- <select id="queryAllDepartment" resultMap="tab_department">
- select * from tab_department
- </select>
- </mapper>
工具类:
- package com.tenghu.mybatis.util;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Properties;
- import org.apache.ibatis.io.Resources;
- import org.apache.ibatis.session.SqlSession;
- import org.apache.ibatis.session.SqlSessionFactory;
- import org.apache.ibatis.session.SqlSessionFactoryBuilder;
- /**
- * Mybatis工具类
- * @author Arvin_Li
- * @version 1.0
- *
- */
- public class MybatisUtil {
- private MybatisUtil(){}
- //声明SqlSession工厂
- private static SqlSessionFactory sqlSessionFactory;
- //使用静态代码块获取SqlSession工厂
- static{
- try {
- //获取mybatis主配置文件流
- InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");
- //创建属性文件对象
- Properties properties=new Properties();
- //加载属性配置文件
- properties.load(Resources.getResourceAsStream("jdbc.properties"));
- //创建SqlSessionFactory对象
- sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream, properties);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- /**
- * 开启SqlSession对象,不自动提交
- * @return
- */
- public static SqlSession openSession(){
- return sqlSessionFactory.openSession();
- }
- /**
- * 开启自动提交的SqlSession
- * @return
- */
- public static SqlSession openAutoCommitSession(){
- return sqlSessionFactory.openSession(true);
- }
- /**
- * 关闭SqlSession
- * @param sqlSession
- */
- public static void closeSession(SqlSession sqlSession){
- if(null!=sqlSession){
- sqlSession.close();
- }
- sqlSession=null;
- }
- }
测试类:
- package com.tenghu.mybatis.test;
- import java.util.List;
- import org.apache.ibatis.session.SqlSession;
- import org.junit.Test;
- import com.tenghu.mybatis.model.Department;
- import com.tenghu.mybatis.util.MybatisUtil;
- /**
- * 部门测试类
- * @author Arvin_Li
- *
- */
- public class DepartmentTest {
- //命名空间
- private String namespace="com.tenghu.mybatis.model.xml.DepartmentMapper.";
- /**
- * 查询出所有部门
- */
- @Test
- public void testQueryAllDepartment(){
- //获取SqlSession
- SqlSession sqlSession=MybatisUtil.openSession();
- try {
- //查询
- List<Department> departList=sqlSession.selectList(namespace+"queryAllDepartment");
- //输出部门信息
- for (Department department : departList) {
- System.out.println(department.getIds()+"\t"+department.getDeName()+"\t"+department.getpIds()+"\t"+department.getDeChargePerson());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }finally{
- //关闭SqlSession
- MybatisUtil.closeSession(sqlSession);
- }
- }
- }
这样就可以处理字段名与属性名不一致的情况了。
转自:http://aijuans.iteye.com/blog/2173090