Mybatis解决属性名与字段名不一致
在开发的时候应该遇到这样的情况,数据库中的字段名与属性名不一致的情况,通常数据库中的字段命名时多个单词之间使用下划线连接在一起的,而在类中的属性名则多数是用驼峰标识的命名方式,我见过的大多数都是这样,那么使用mybatis该如果解决这一的问题呢?如下:数据表:
view plaincopy
[*]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 '部门表'
实体类:
view plaincopy
[*]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主配置文件:
view plaincopy
[*]<?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>
映射文件:
view plaincopyhttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
[*]<?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>
工具类:
view plaincopyhttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
[*]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;
[*] }
[*]}
测试类:
view plaincopyhttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/CODE_ico.pnghttp://onexin.iyunv.com/source/plugin/onexin_bigdata/https://code.csdn.net/assets/ico_fork.svg
[*]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);
[*] }
[*] }
[*]}
这样就可以处理字段名与属性名不一致的情况了。
其他精彩文章文章
jQuery教程(9)-DOM树操作之复制元素
android学习笔记(35)android AlertDialog创建列表对话框
android shareSDK sso登录新浪和微信
mysql 索引类型详解-B-Tree索引
BroadcastReceiver 使用AlertDialog后 app奔溃了
更多关于android开发文章
页:
[1]