(三)使用XML配置SQL映射器
SqlSessionFactoryUtil.java
package com.javaxk.util; import java.io.IOException;
import java.io.InputStream; import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder; public class SqlSessionFactoryUtil { private static SqlSessionFactory sqlSessionFactory; public static SqlSessionFactory getSqlSessinFactory() { if (sqlSessionFactory == null) { String resource = "mybatis-config.xml";
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);
} return sqlSessionFactory;
} public static SqlSession openSession() {
return getSqlSessinFactory().openSession();
} }
Student.java
package com.javaxk.model;
public class Student {
private int id;
private String name;
private int age;
public Student() {
super();
}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}
}
mybatis-config.xml
<?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>
<!-- <properties resource="jdbc.properties"/> --> <properties>
<property name="jdbc.driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="jdbc.url" value="jdbc:mysql://localhost:3306/db_mybatis?characterEncoding=utf-8"/>
<property name="jdbc.username" value="root"/>
<property name="jdbc.password" value="root"/>
</properties> <!--
<typeAliases>
<typeAlias alias="Student" type="com.javaxk.model.Student"/>
</typeAliases>
--> <typeAliases>
<package name="com.javaxk.model"/>
</typeAliases> <environments default="development"> <environment id="development">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</dataSource>
</environment> <environment id="test">
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property name="driver" value="${jdbc.driverClassName}" />
<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/javaxk/mappers/StudentMapper.xml" />
-->
<package name="com.javaxk.mappers"/>
</mappers>
</configuration>
StudentMapper.java
package com.javaxk.mappers;
import java.util.List;
import com.javaxk.model.Student;
public interface StudentMapper {
public int add(Student student);
public int update(Student student);
public int delete(Integer id);
public Student findById(Integer id);
public List<Student> find();
}
StudentMapper.xml
<?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.javaxk.mappers.StudentMapper"> <resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert> <update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update> <delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete> <select id="findById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select> <select id="find" resultMap="StudentResult">
select * from t_student
</select> </mapper>

所有的主测试类都在JUtil的测试方法前后中调用
private static Logger logger = Logger.getLogger(StudentTest2.class);
private SqlSession sqlSession = null;
private StudentMapper studentMapper = null; /**
* 测试方法前调用
* @throws Exception
*/
@Before
public void setUp() throws Exception {
sqlSession = SqlSessionFactoryUtil.openSession();
studentMapper = sqlSession.getMapper(StudentMapper.class);
} /**
* 测试方法后调用
* @throws Exception
*/
@After
public void tearDown() throws Exception {
sqlSession.close();
}
第一节:insert映射语句
添加映射配置文件
<insert id="add" parameterType="Student" >
insert into t_student values(null,#{name},#{age})
</insert>
@Test
public void testAdd() {
logger.info("添加学生");
Student student = new Student("小宁" ,88);
studentMapper.add(student);
sqlSession.commit(); }
第二节:update映射语句
<update id="update" parameterType="Student">
update t_student set name=#{name},age=#{age} where id=#{id}
</update>
@Test
public void testUpdate() {
logger.info("更新学生");
Student student = new Student(20,"小宁22",99);
studentMapper.update(student);
sqlSession.commit();
}
第三节:delete映射语句
<delete id="delete" parameterType="Integer">
delete from t_student where id=#{id}
</delete>
@Test
public void testDelete() {
logger.info("删除学生");
studentMapper.delete(20);
sqlSession.commit();
}
第四节:select映射语句
(1)通过ID查找学生
<select id="findById" parameterType="Integer" resultType="Student">
select * from t_student where id=#{id}
</select>
@Test
public void testFindById() {
logger.info("通过ID查找学生");
Student student=studentMapper.findById(1);
System.out.println(student);
}
(2)查找所有学生
<resultMap type="Student" id="StudentResult">
<id property="id" column="id"/>
<result property="name" column="name"/>
<result property="age" column="age"/>
</resultMap> <select id="find" resultMap="StudentResult">
select * from t_student
</select>
@Test
public void testFind() {
logger.info("查找所有学生");
List<Student> studentlist = studentMapper.find();
for (Student s : studentlist) {
System.out.println(s);
}
}
(三)使用XML配置SQL映射器的更多相关文章
- Java Persistence with MyBatis 3(中文版) 第三章 使用XML配置SQL映射器
关系型数据库和SQL是经受时间考验和验证的数据存储机制.和其他的ORM 框架如Hibernate不同,MyBatis鼓励开发者可以直接使用数据库,而不是将其对开发者隐藏,因为这样可以充分发挥数据库服务 ...
- Mybatis基于XML配置SQL映射器(二)
Mybatis之XML注解 之前已经讲到通过 mybatis-generator 生成mapper映射接口和相关的映射配置文件: 下面我们将详细的讲解具体内容 首先我们新建映射接口文档 sysUse ...
- Mybatis基于XML配置SQL映射器(一)
Durid和Mybatis开发环境搭建 SpringBoot搭建基于Spring+SpringMvc+Mybatis的REST服务(http://www.cnblogs.com/nbfujx/p/76 ...
- Mybatis基于XML配置SQL映射器(三)
Mybatis之动态SQL mybatis 的动态sql语句是基于OGNL表达式的.可以方便的在 sql 语句中实现某些逻辑. 总体说来mybatis 动态SQL 语句主要有以下几类: if choo ...
- MyBatis学习笔记3--使用XML配置SQL映射器
<resultMap type="Student" id="StudentResult"> <id property="id&quo ...
- MyBatis 3 使用注解配置SQL映射器
l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l 动态SQL @Sel ...
- 使用注解配置SQL映射器
在上一章,我们看到了我们是怎样在映射器Mapper XML配置文件中配置映射语句的.MyBatis也支持使用注解来配置映射语句.当我们使用基于注解的映射器接口时,我们不再需要在XML配置文件中配置了. ...
- MyBatis 3(中文版) 第四章 使用注解配置SQL映射器
本章将涵盖以下话题: l 在映射器Mapper接口上使用注解 l 映射语句 @Insert,@Update,@Delete,@SeelctStatements l 结果映射 一对一映射 一对多映射 l ...
- Mybatis基于接口注解配置SQL映射器(一)
上文已经讲解了基于XML配置的SQL映射器,在XML配置的基础上MyBatis提供了简单的Java注解,使得我们可以不配置XML格式的Mapper文件,也能方便的编写简单的数据库操作代码. Mybat ...
随机推荐
- laravel Collection mapToDictionary 例子
源码 示例 <?php require __DIR__ . '/bootstrap/app.php'; $arr = [ [ 'name' => 'John', 'age' => 2 ...
- Gogs安装配置(快速搭建版)转载
gogs官网 oschina gogs介绍 一句话描述: 一款极易搭建的自助 Git 服务. 环境 centos7:golang+mysqldb+git 安装配置环境 yum install mysq ...
- Mongo副本集搭建
解压mongodb-linux-x86_64-rhel70-3.2.0.tgz 将解压后的bin路径添加到系统环境变量,保证mongo.mongod等命令可用 创建副本集目录mongo/27017.2 ...
- Java基础-面向对象第二特征之继承(Inheritance)
Java基础-面向对象第二特征之继承(Inheritance) 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.继承的概述 在现实生活中,继承一般指的是子女继承父辈的财产.在程序 ...
- java基础-迭代器(Iterator)与增强for循环
java基础-迭代器(Iterator)与增强for循环 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Iterator迭代器概述 Java中提供了很多个集合,它们在存储元素时 ...
- vs配置SP++3.0
最近在研究信号处理的C语言算法,突然发现一个西安交大的师兄之前已经写出来了一个完整的库,同样是研究生三年,差别怎么这样大呢. 先从用轮子开始吧. 一.SP++3.0安装及测试 官网下载地址: http ...
- JS开启浏览器全屏模式,需要手动触发
<html > <meta charset="UTF-8"> <body> <button onclick="launchFul ...
- 什么是EOF -- 转
转载地址:http://www.ruanyifeng.com/blog/2011/11/eof.html 我学习C语言的时候,遇到的一个问题就是EOF. 它是end of file的缩写,表示&quo ...
- HDU1505 City Game 悬线法
题意: 给出一个像这样的矩阵 R F F F F F F F F F F F R R R F F F F F F F F F F F F F F F 求F组成的最大子矩阵(面积最大) 有多组数 ...
- JVM性能调优监控工具详解
现实企业级Java开发中,有时候我们会碰到下面这些问题: OutOfMemoryError,内存不足 内存泄露 线程死锁 锁争用(Lock Contention) Java进程消耗CPU过高 .... ...