(三)使用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 ...
 
随机推荐
- python入门:1-100所有数的和
			
#!/usr/bin/env python # -*- coding:utf-8 -*- #1-100所有数的和 """ 给x赋值为1,y赋值为0,while循环真,循环 ...
 - go语言操作mongodb
			
Install the MongoDB Go Driver The MongoDB Go Driver is made up of several packages. If you are just ...
 - svn断开链接后,重新share提交代码报错
			
前言:svn怎样断开链接并清除干净请查看此地址-->android studio中断开SVN连接,并彻底清理项目中的.svn文件 1.每次把项目重新关联到新的svn地址上,我都抓狂一样的烦躁,因 ...
 - shell 中的操作符
			
1.算术操作符 2.关系操作符 3.布尔操作符 4.字符串操作符 5.文件相关操作符 算术操作符 bash shell 没有提供任何机制来执行简单的算术运算,不过我们可以借助于一些其他程序,如 exp ...
 - 【Asp.net入门05】第一个ASP.NET 应用程序-测试Asp.net程序
			
测试示例应用程序 本部分内容: ASP.NET应用程序测试方法 web窗体访问过程 Visual Studio工具栏上有一个下拉列表,其中列出了工作站上已安装的浏览器的名称(单击浏览器名称右侧的向下箭 ...
 - Docker图形界面管理之Shipyard
			
一.介绍 Shipyard基于Docker API实现的容器图形管理系统,支持container.images.engine.cluster等功能,可满足我们基本的容器部署需求. 可堆栈的Docker ...
 - Docker入门与应用系列(四)网络管理
			
一.Docker的五种网络模式 在使用docker run创建docker容器时,可以用--net选项指定容器的网络模式,Docker有以下5种网络模式: 1. bridge模式 使用docker r ...
 - 谈谈Flash图表中数据的采集
			
一般来说flash中的数据是不能被现有技术很容易采集到的,但是也不能谈flash色变,要具体问题具体分析,有些flash是可以通过一些分析发现背后的数据.然后采集就变得很容易了. 具体案例:搜房房价走 ...
 - C#  定时执行方法: System.Timers.Timer用法示例
			
System.Timers.Timer t = new System.Timers.Timer(5000); //设置时间间隔为5秒 private void Form1_Load(ob ...
 - 【leetcode 简单】 第九十二题 第N个数字
			
在无限的整数序列 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, ...中找到第 n 个数字. 注意: n 是正数且在32为整形范围内 ( n < 231). 示例 1: ...