MyBatis_Study_003(字段名与属性名称不一致,resultMap)
源码:https://github.com/carryLess/mbtsstd-003
1.主配置文件
<?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">
<!-- (以上)文件头在解压的文件夹中mybatis-3.4.4.pdf文件中搜索mybatis-3-config.dtd即可得到 -->
<configuration> <!-- 指定属性配置文件 -->
<properties resource="jdbc.properties" />
<!--
配置类的别名,我建议使用package这种写法
这样写会将该包中所有类的简单类名配置为别名,简单方便
,还有别的写法,自行google
-->
<typeAliases>
<package name="model" />
</typeAliases>
<!-- 配置MyBatis运行环境 -->
<environments default="development">
<environment id="development">
<!-- 使用JDBC事务管理 -->
<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="dao/mapper.xml"/>
<!--
实际开发中可能有多个映射文件,而其中sql标签的id相同时候,执行过程就会报错
我们可以根据mapper映射文件中的namespace属性来区分,调用时候用如下方式
namespace.id
-->
<!--
<mapper resource="dao/mapper2.xml"/>
-->
</mappers> </configuration>
2.mapper映射文件
<?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">
<!-- 文件头在解压的文件夹中mybatis-3.4.4.pdf文件中搜索mybatis-3-mapper.dtd即可得到 -->
<mapper namespace="model.SStudent">
<!-- parameterType属性,框架会根据SQLSession中传递的参数检测到,所以我们一般不用指定 -->
<insert id="insertStudentByList">
insert into SStudent(sname,sage,score) values
<!-- 这里面的collection必须写成list -->
<foreach collection="list" separator="," item="stu">
(#{stu.name},#{stu.age},#{stu.score})
</foreach>
</insert> <!-- 使用别名 -->
<select id="selectById1" resultType="SStudent">
select sid id,sname name,sage age,score from sstudent where sid = #{xxx}
</select> <!-- 使用resultMap -->
<select id="selectById2" resultMap="sStudentMapper">
select sid,sname,sage,score from sstudent where sid = #{xxx}
</select> <!--
type:要映射的实体类
id:resultMap标签的id,用于select标签中resultMap属性
-->
<resultMap id="sStudentMapper" type="SStudent">
<id column="sid" property="id" />
<result column="sname" property="name" />
<result column="sage" property="age" />
</resultMap> </mapper>
3.实体类
package model; /**
* Created by carryLess on 2017/11/29.
*/
public class SStudent {
private Integer id;
private String name;
private Integer age;
private double score; public SStudent() {
} public SStudent(String sname, Integer sage, double score) {
this.name = sname;
this.age = sage;
this.score = score;
} @Override
public String toString() {
return "SStudent{" +
"id=" + id +
", sname='" + name + '\'' +
", sage=" + age +
", score=" + score +
'}';
} public Integer getId() {
return id;
} public void setId(Integer id) {
this.id = id;
} public String getSname() {
return name;
} public void setSname(String sname) {
this.name = sname;
} public Integer getSage() {
return age;
} public void setSage(Integer sage) {
this.age = sage;
} public double getScore() {
return score;
} public void setScore(double score) {
this.score = score;
}
}
4.dao接口与实现类
package dao; import model.SStudent; import java.util.List;
import java.util.Map; /**
* Created by carryLess on 2017/11/29.
*/
public interface IStudentDao { /**
* 插入集合
* @param studentList
*/
void insertStudentByList(List<SStudent> studentList); /**
* 根据id查询1
* @param id
* @return
*/
SStudent selectById1(int id); /**
* 根据id查询2
* @param id
* @return
*/
SStudent selectById2(int id);
}
package dao; import model.SStudent;
import org.apache.ibatis.session.SqlSession;
import utils.MyBatisUtils; import java.util.ArrayList;
import java.util.List;
import java.util.Map; /**
* Created by carryLess on 2017/11/29.
*/
public class StudentDaoImpl implements IStudentDao {
private SqlSession sqlSession; @Override
public void insertStudentByList(List<SStudent> studentList) {
try {
sqlSession = MyBatisUtils.getSqlSession();
sqlSession.insert("insertStudentByList", studentList);
sqlSession.commit();
}finally {
//关闭sqlSession
if(sqlSession != null){
sqlSession.close();
}
}
} @Override
public SStudent selectById1(int id) {
SStudent sStudent;
try {
sqlSession = MyBatisUtils.getSqlSession();
sStudent = sqlSession.selectOne("selectById1", id);
} finally {
if(sqlSession != null){
sqlSession.close();
}
}
return sStudent;
} @Override
public SStudent selectById2(int id) {
SStudent sStudent;
try {
sqlSession = MyBatisUtils.getSqlSession();
sStudent = sqlSession.selectOne("selectById2", id);
} finally {
if(sqlSession != null){
sqlSession.close();
}
}
return sStudent;
}
}
5.工具类
package utils; 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; /**
* Created by carryLess on 2017/11/30.
*/
public class MyBatisUtils { /*
* SqlSession 由SqlSessionFactory对象创建,
* 而SqlSessionFactory对象为重量级对象
* 并且是线程安全的,所以我们将其设为单例
* */
private static SqlSessionFactory factory; /**
* 私有化构造方法,避免该工具类在外部被实例化
*/
private MyBatisUtils(){} /**
* 获取 SqlSession
* @return
*/
public static SqlSession getSqlSession(){
try {
if(factory == null){
//读取配置文件
InputStream inputStream = Resources.getResourceAsStream("mybatis.xml");
//创建工厂类
factory = new SqlSessionFactoryBuilder().build(inputStream);
}
} catch (IOException e) {
e.printStackTrace();
return null;
}
/*
* factory.openSession(true); 创建一个有自动提交功能的SqlSession
* factory.openSession(false); 创建一个没有自动提交功能的SqlSession,需要手动提交
* factory.openSession(); 同factory.openSession(false);
*/
return factory.openSession();
}
}
6.测试类
package test; import dao.IStudentDao;
import dao.StudentDaoImpl;
import model.SStudent;
import org.junit.Before;
import org.junit.Test; import java.util.ArrayList;
import java.util.List; /**
* Created by carryLess on 2017/11/29.
*/
public class MyTest { private IStudentDao dao; @Before
public void initDao(){
dao = new StudentDaoImpl();
} @Test
public void testInsertList(){
List<SStudent> sStudentList = new ArrayList<SStudent>();
for(int i = 11;i<20;i++){
SStudent sStudent = new SStudent();
sStudent.setSname("zhangs-"+i);
sStudent.setSage(25+i);
sStudent.setScore(90);
sStudentList.add(sStudent);
}
dao.insertStudentByList(sStudentList);
} @Test
public void testSelectById(){
SStudent sStudent = dao.selectById2(18);
System.out.println(sStudent);
} }
MyBatis_Study_003(字段名与属性名称不一致,resultMap)的更多相关文章
- json字符串转java对象,json中字段名称与对象属性名称不一致
json字符串转java对象,json字段名称与对象属性名称不一致可以在对象属性上添加注解@SerializedName解决
- 在oracle中操作表及字段注释,查询一个表的所有字段名以及属性和约束
1.查询表注释 SELECT * FROM USER_TAB_COMMENTS; 三列:TABLE_NAME,TABLE_TYPE,COMMENTS 2.查询字段注释 SELECT * FROM US ...
- hibernate字段名和属性
字段名和属性名相同 Annotation:默认为@Basic 注意:如果在成员属性没有加入任何注解,则默认在前面加入了@Basic Xml中不用写column 字段名和属性名不同 Annotation ...
- 字段名与属性名不一致问题 通过resultMap解决
- 懒汉处理dapper字段名与属性名的映射方式
你还以为走路是世上最简单的事情呢?只不过是把一只脚放到另一只脚前面.但我一直很惊讶这些原本是本能的事情实际上做起来有多困难.而吃,吃也是一样的,有些人吃起东西来可困难了.说话也是,还有爱.这些东西都可 ...
- MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突(转载)
本文转载自:http://www.cnblogs.com/jpf-java/p/6013307.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这 ...
- MyBatis入门学习教程-解决字段名与实体类属性名不相同的冲突
在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...
- MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突
在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况下的如何解决字段名与实体类属性名不相同的冲突. 一.准备演示需要使用的表和数据 CREATE TAB ...
- MyBatis——解决字段名与实体类属性名不相同的冲突
原文:http://www.cnblogs.com/xdp-gacl/p/4264425.html 在平时的开发中,我们表中的字段名和表对应实体类的属性名称不一定都是完全相同的,下面来演示一下这种情况 ...
随机推荐
- [one day one question] 有没有免费接收短信验证用于注册的软件或者平台?
问题描述: 想要批量注册撸羊毛,有手机短信验证码验证,这怎么破? 解决方案: 免费的肯定没有的,不过"一条短信收费一毛钱"倒是有一个,本人是亲自试用过,该平台收不到短信验证码不收费 ...
- 常用php操作redis命令整理(二)哈希类型
HSET将哈希表key中的域field的值设为value;如果field是哈希表中的一个新建域,并且值设置成功,返回1;如果哈希表中域field已经存在且旧值已被新值覆盖,返回0. <?php ...
- JAVA面试题整理(7)-Redis
Redis面试题汇总 1.Redis用过哪些类型数据,以及Redis底层怎么实现 分析:是不是觉得这个问题很基础,其实我也这么觉得.然而根据面试经验发现,至少百分八十的人答不上这个问题.建议,在项目中 ...
- Openldap基于digest-md5方式的SASL认证配置
1. openldap编译 如果需要openldap支持SASL认证,需要在编译时加上–enable-spasswd选项安装完cyrus-sasl,openssl(可选),BDB包后执行: 1 2 $ ...
- MVC 返回对象换成json
错误界面: 这个就是返回对象没有转换成json 就是要再返回的头部添加application/json 代码: using System; using System.Collections.Gener ...
- 【查看内存】Linux查看内存使用情况(一)
用 'top -i' 看看有多少进程处于 Running 状态,可能系统存在内存或 I/O 瓶颈,用 free 看看系统内存使用情况,swap 是否被占用很多,用 iostat 看看 I/O 负载情况 ...
- ImportError: cannot import name 'izip & TypeError: 'float' object cannot be interpreted as an integer
ImportError: cannot import name 'izip' 参考:https://codereview.stackexchange.com/questions/26271/impor ...
- 判断一个数是否是4的n次方
def is_Power_of_four(n): while n and not (n & 0b11): n >>= ) print(is_Power_of_four()) pri ...
- [html5]HTML5中<section>和<article>的区别
一.section元素 从字面理解就是区块.部分的意思,相对于article元素更加广泛,每个区块都可以使用,比如页面里的导航菜单.文章正文.文章的评论等. 1.section元素用于对网站或应用程序 ...
- [翻译]PyMongo官方文档
PyMongo官方文档翻译 周煦辰 2016-06-30 这是本人翻译的PyMongo官方文档.现在网上分(抄)享(袭)的PyMongo博客文章很多,一方面这些文章本就是抄袭的,谈不上什么格式美观,另 ...