Mybatis实例参考04-列名与属性名不一致的解决方法
目录
1 前言
2 spring配置:beans.xml
3 mybatis配置:mybatis-config.xml
4 mapper配置:PersonMapper.xml
5 bean:
6 dao:
7 daoimpl:
8 service:
9 serviceImpl:
10 主测试类main:
11 mybatisUtil:
1 前言
本文供以下文章参考使用:
mybatis要点梳理__evenif的博客-CSDN博客
本文顺序在文章:
Spring基础回顾__evenif的博客-CSDN博客
之后。其中的项目代码,在该文基础上进行了扩充,如果根据本文代码无法正确构建自己的测试环境,请先学习spring相关知识后再来。
2 spring配置:beans.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 自动创建单例 --><bean id="personDao" class="com.evenif.dao.impl.PersonMysqlDaoImpl"/>
<!-- <bean id="aalog" class="com.evenif.log.AnnotationAspectLog"/>--><!-- 自动创建单例服务依赖注入已存在dao --><bean id="personService" class="com.evenif.service.impl.PersonServiceImpl"><property name="personDao" ref="personDao"></property></bean><!-- 配置aop自动扫描使用注解,生产环境中推荐 -->
<!-- <aop:aspectj-autoproxy/>-->
</beans>
3 mybatis配置:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configurationPUBLIC "-//mybatis.org//DTD Config 3.0//EN""http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration><!-- 导入外部数据源配置文件--><properties resource="db.properties"/><typeAliases><!-- 别名方式一:给全类名指定别名,可以在mapper映射文件中简化引用--><!--<typeAlias type="com.evenif.bean.Index" alias="Index"/>--><!-- 别名方式二:给包下所有类名指定别名,默认别名是对应的类名--><package name="com.evenif.bean"/></typeAliases><!-- environments指mybatis可以配置多个环境 default指向默认的环境每个SqlSessionFactory对应一个环境environment --><environments default="development"><environment id="development"><!-- JDBC - 这个配置直接使用JDBC的提交和回滚功能。它依赖于从数据源获得连接来管理事务的生命周期。MANAGER - 这个配置基本上什么都不做。它从不提交或者回滚一个连接的事务。而是让容器(例如:Spring或者J2EE应用服务器)来管理事务的生命周期--><transactionManager type="JDBC"/><!-- 数据源类型:UNPOOLED - 这个类型的数据源实现只是在每次需要的时候简单地打开和关闭连接。POOLED - 这个数据源的实现缓存了JDBC连接对象,用于避免每次创建新的数据库连接时都初始化和连接认证,加快程序响应。并发WEB应用通常通过这种做法来获得快速响应。JNDI - 这个数据源的配置是为了准备与像Spring或应用服务器能够在外部或者内部配置数据--><dataSource type="POOLED"><property name="driver" value="${driver}"/><property name="url" value="${url}"/><property name="username" value="${username}"/><property name="password" value="${password}"/></dataSource></environment></environments><mappers><!-- 配置javaBean对应的mapper文件的位置--><mapper resource="PersonMapper.xml"/></mappers>
</configuration>
4 mapper配置:PersonMapper.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapperPUBLIC "-//mybatis.org//DTD Mapper 3.0//EN""http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<!-- namespace命名空间 防止sql语句的id重名namespace命名:包名+类名+mapper文件名parameterType : sql语句参数类型resultType: 返回结果类型userGeneratedKeys="true" 使用自增主键 -->
<mapper namespace="PersonMapper"><resultMap id="PersonMap" type="com.evenif.bean.Person"><id column="c_id" property="id"/><result column="c_name" property="name"/><result column="c_age" property="age"/></resultMap><!-- 一个小技巧,可以将公共的语句提出来使用<include refid="Base_Column"/>来引用建议使用该方法:不用select *,如果增删数据库字段,使用*自动匹配会报错,如果使用该方法如果数据库新加字段,原项目不受影响(新加字段不生效)--><sql id="Alias_Base_Column">c_id id,c_name name,c_age age</sql><sql id="Base_Column">c_id ,c_name ,c_age</sql><!-- #################### 结果集映射(推荐)#################### --><select id="getById" resultMap="PersonMap">select<include refid="Base_Column"/>from t_person where c_id=#{id}</select><!-- 在mybatis-config.xml已经给com.evenif.bean包下的所有类指定了别名,依然可以使用全类名,不影响--><select id="loadAll" resultMap="PersonMap">select<include refid="Base_Column"/>from t_person</select><!-- #################### 给列名起别名解决列名和属性名不一致 #################### --><select id="getById0" resultType="com.evenif.bean.Person">select<include refid="Alias_Base_Column"/>from t_person where c_id=#{id}</select><!-- 在mybatis-config.xml已经给com.evenif.bean包下的所有类指定了别名,依然可以使用全类名,不影响--><select id="loadAll0" resultType="com.evenif.bean.Person">select<include refid="Alias_Base_Column"/>from t_person</select></mapper>
5 bean:
package com.evenif.bean;public class Person {int id;String name;int age;public Person() {}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public int getAge() {return age;}public void setAge(int age) {this.age = age;}@Overridepublic String toString() {return "Person{" +"id=" + id +", name='" + name + '\'' +", age=" + age +'}';}
}
6 dao:
package com.evenif.dao;
import com.evenif.bean.Person;import java.util.List;public interface PersonDao {/*使用给列名起别名的方式解决属性名与类名不一致*/Person getById(int id);List<Person> loadAll();/*设置结果集解决属性名与类名不一致*/Person getById0(int id);List<Person> loadAll0();}
7 daoimpl:
package com.evenif.dao.impl;import com.evenif.bean.Person;
import com.evenif.dao.PersonDao;
import com.evenif.mybatis.MybatisUtil;
import org.apache.ibatis.session.SqlSession;import java.util.List;public class PersonMysqlDaoImpl implements PersonDao {@Overridepublic Person getById(int id) {try (SqlSession session = MybatisUtil.getSession()) {Person person = session.selectOne("PersonMapper.getById", id);return person;} catch (Exception e) {e.printStackTrace();}return null;}/*** @return*/@Overridepublic List<Person> loadAll() {try (SqlSession session = MybatisUtil.getSession()) {List<Person> list = session.selectList("PersonMapper.loadAll");return list;} catch (Exception e) {e.printStackTrace();}return null;}/*** @param id* @return*/@Overridepublic Person getById0(int id) {try (SqlSession session = MybatisUtil.getSession()) {Person person = session.selectOne("PersonMapper.getById0", id);return person;} catch (Exception e) {e.printStackTrace();}return null;}/*** @return*/@Overridepublic List<Person> loadAll0() {try (SqlSession session = MybatisUtil.getSession()) {List<Person> list = session.selectList("PersonMapper.loadAll0");return list;} catch (Exception e) {e.printStackTrace();}return null;}
}
8 service:
package com.evenif.service;import com.evenif.bean.Person;import java.util.List;public interface PersonService {Person getPerson(int id);List<Person> loadPersonList();Person getPerson0(int id);List<Person> loadPersonList0();
}
9 serviceImpl:
package com.evenif.service.impl;import com.evenif.bean.Person;
import com.evenif.dao.PersonDao;
import com.evenif.service.PersonService;
import java.util.List;public class PersonServiceImpl implements PersonService {private PersonDao personDao;public void setPersonDao(PersonDao personDao) {this.personDao = personDao;}public PersonServiceImpl() {}public PersonServiceImpl(PersonDao personDao) {this.personDao = personDao;}@Overridepublic Person getPerson(int id) {return personDao.getById(id);}@Overridepublic List<Person> loadPersonList() {return personDao.loadAll();}@Overridepublic Person getPerson0(int id) {return personDao.getById0(id);}@Overridepublic List<Person> loadPersonList0() {return personDao.loadAll0();}
}
10 主测试类main:
package com.evenif;import com.evenif.bean.Person;
import com.evenif.service.PersonService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import java.io.IOException;
import java.util.List;public class Main {public static void main(String[] args) throws IOException {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");PersonService service = (PersonService) context.getBean("personService");{System.out.println("使用结果集映射查询一条记录:");Person person = service.getPerson(1);System.out.println(person);}{System.out.println("使用结果集映射查询所有记录:");List<Person> list = service.loadPersonList();System.out.println(list);}{System.out.println("给列名起别名查询一条记录:");Person person = service.getPerson0(1);System.out.println(person);}{System.out.println("给列名起别名查询所有记录:");List<Person> list = service.loadPersonList0();System.out.println(list);}}
}
//执行结果:
//使用结果集映射查询一条记录:
// Person{id=1, name='张三', age=13}
//使用结果集映射查询所有记录:
// [Person{id=1, name='张三', age=13}, Person{id=2, name='李四', age=14}, Person{id=3, name='王五', age=15}]
//给列名起别名查询一条记录:
// Person{id=1, name='张三', age=13}
//给列名起别名查询所有记录:
// [Person{id=1, name='张三', age=13}, Person{id=2, name='李四', age=14}, Person{id=3, name='王五', age=15}]
11 mybatisUtil:
package com.evenif.mybatis;import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;import java.io.IOException;
import java.io.InputStream;public class MybatisUtil {public static SqlSession getSession() throws IOException {String resource = "mybatis-config.xml";InputStream inputStream = Resources.getResourceAsStream(resource);SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);return sqlSessionFactory.openSession();}
}
Mybatis实例参考04-列名与属性名不一致的解决方法相关推荐
- SSM_Mybatis_Day01(快速入门、映射文件概述、核心配置文件概述、相应API、代理开发方式、映射文件深入、数据类型的映射、列名和属性名不一致的时候的处理)
SSM_Mybatis_Day01(快速入门.映射文件概述.核心配置文件概述.相应API.代理开发方式.映射文件深入.数据类型的映射.列名和属性名不一致的时候的处理) 1. Mybatis mybat ...
- mybatis学习(18):列名与属性名不一致的情况(使用ResultMap)
目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import com.geyao.mybatis. ...
- mybatis学习(17):列名与属性名不一致的情况(使用别名)
目录结构 com.geyao.mybatis.mapper BlogMapper类 package com.geyao.mybatis.mapper;import com.geyao.mybatis. ...
- 【MyBatis】sql列名与实体类属性名不同的解决方式
sql列名与Java实体类属性名不同的解决方式 例如: 数据库 CREATE TABLE orders(order_id INT PRIMARY KEY AUTO_INCREMENT,order_no ...
- Mybatis 解决数据库字段名和实体类属性名不一致问题
一.问题描述 1.1 查询 password 字段为 null 现象 数据库字段名: 实体类属性名: @Data @NoArgsConstructor @AllArgsConstructor publ ...
- Mybatis处理字段名和属性名不一致的几种方法
Mybatis处理字段名和属性名不一致的几种方法 1.为查询的字段设置别名,和属性名保持一致2.当字段符合MySQL的要求使用,而属性符合Java的要求使用驼峰-此时可以在mybatis核心配置文件中 ...
- SQL Server服务器名称与默认实例名不一致的修复方法
SQL Server服务器名称与默认实例名不一致的修复方法 分类: 个人累积 SQl SERVER 数据库复制2011-08-10 09:49 10157人阅读 评论(0) 收藏 举报 sql ser ...
- Dapper数据库字段(列)与实体属性名不一致,通过Column特性自动注入映射
一.前言 Dapper是.NET下一个micro的ORM,它和Entity Framework或Nhibnate不同,属于轻量级的,并且是半自动的.Dapper只有一个代码文件,完全开源,你可以放在项 ...
- 桌面计算机右键属性不弹窗,Win7系统右键计算机属性不见了的解决方法
Win7系统右键计算机属性不见了的解决方法 Win7系统右键计算机属性不见了怎么办?正常情况下,"计算机"右键会出现多个选项,若是唯独没有"属性"这项功能,很多 ...
- android ellipsize 多行,android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法...
android TextView多行文本(超过3行)使用ellipsize属性无效问题的解决方法 布局文件中的TextView属性 复制代码 代码如下: android:id="@+id/b ...
最新文章
- php dir类,PHP基于dir类实现目录遍历删除
- ssh调用expect使用以及shell同时传入两个参数调用
- 重庆理工大学计算机考研就业,重庆理工大学专业硕士含金量高吗?好就业吗?
- 利用循环神经网络生成唐诗_11种主要神经网络结构图解
- linux虚拟实验服务,Linux虚拟服务器(LVS)实验文档PDF
- mysql查询优化explain命令详解
- Webpack - 自学笔记
- matlab中的神经网络训练,MATLAB中的神经网络训练
- web api 开发记录
- http报文和协议首部
- 服务器sxs文件通用的吗,win10x sources sxs文件有什么作用
- Laravel 调试利器 Laravel Debugbar 扩展包安装及使用教程
- 清北学堂2018年1月省选强化班模拟考试1
- 安居客头部导航栏的编写
- 【Ubuntu18.04安装搜狗中文输入法】
- linux 百度云 备份软件下载,Linux 安装百度云,自动备份服务器资料
- BPDU Guard, BPDU Filter, Root Guard, Loop Guard UDLD
- 【图文并茂】六十多个 vscode 插件,助你打造最强编辑器
- java中实现的十进制转换成二进制的简单代码
- Linux系统中如何创建NFS共享目录