MyBatis_Study_003(字段名与属性名称不一致,resultMap)
Posted carryLess
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis学习总结——解决字段名与实体类属性名不相同的冲突