本例在【Mybatis】MyBatis配置文件的使用(二)基础上继续学习对表执行CRUD操作

使用MyBatis对表执行CRUD操作

  1、定义sql映射xml文件(EmployeeMapper.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">
<!--
namespace:名称空间
id:唯一标识
resultType:返回值类型
#{id}:从传过来的参数中取出id值
-->
<mapper namespace="com.hd.test.mapper.EmployeeMapper">
<select id="getEmployeeById"
resultType="com.hd.test.pojo.Employee">
select id, last_name lastName, gender, email from employee where id =
#{id}
</select> <!-- public Long insertEmployee(Employee employee); -->
<!-- parameterType 可写可不写 -->
<insert id="insertEmployee" parameterType="com.hd.test.pojo.Employee" >
insert into employee(last_name, email, gender) values(#{lastName}, #{email}, #{gender})
</insert> <!-- public boolean updateEmployee(Employee employee); -->
<update id="updateEmployee">
update employee
set last_name = #{lastName}, email = #{email}, gender = #{gender}
where
id = #{id}
</update> <!-- public Integer deleteEmployeeById(Integer id); -->
<delete id="deleteEmployeeById">
delete from employee where id = #{id}
</delete> </mapper>

  2、在mybatis-config.xml文件中注册这个映射文件EmployeeMapper.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> <environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test_mybatis?allowPublicKeyRetrieval=true" />
<property name="username" value="admin" />
<property name="password" value="123456" />
</dataSource>
</environment>
</environments> <mappers>
<!-- 添加sql射文件到Mybatis的全局配置文件中 -->
<mapper resource="mapper/EmployeeMapper.xml" /> </mappers> </configuration>

  3、编写一个EmployeeMapper接口

 package com.hd.test.mapper;

 import com.hd.test.pojo.Employee;

 public interface EmployeeMapper {

     public Employee getEmployeeById(Integer id);

     // 新增
public Long insertEmployee(Employee employee); // 修改
public boolean updateEmployee(Employee employee); // 删除
public Integer deleteEmployeeById(Integer id); }

  4、编写mybatis单元测试类(TestMybatis.java)

 package com.hd.test.mybatis;

 import java.io.IOException;
import java.io.InputStream; import javax.sound.midi.Soundbank; 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 org.junit.Test; import com.hd.test.mapper.EmployeeMapper;
import com.hd.test.pojo.Employee; import sun.print.resources.serviceui; /**
* 1、SqlSession代表和数据库的一次会话,用完必须关闭
2、SqlSession和connection一样都是非线程安装的,每次使用都应该去获取新对象
3、mapper接口没有实现累,但是mybatis会为这个接口生成一个代理对象
(将接口和xml进行绑定)
EmployeeMapper empMapper = sqlSession.getMapper(EmployeeMapper.class)
4、连个重要的配置文件:
mybatis的全局配置文件:包含数据库连接信息,事物管理等系统环境信息
sql映射文件:保存了每一个sql语句的映射信息:
将sql抽取出来。
*
*/
public class TestMybatis { /**
* 测试增删改
* 1、mybatis允许增删改直接定义一下类型返回值
* Ingteger、Long、Boolean
* 2、需要手动提交数据
* SqlSession session = sqlSessionFactory.openSession(); ==> 手动提交
* SqlSession session = sqlSessionFactory.openSession(true); ==> 自动提交
* @throws IOException
*/ // 插入
@Test
public void test1() throws IOException { // 获取SqlSessionFactory
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
// 获取的sqlsession不会自动提交数据
SqlSession session = sqlSessionFactory.openSession(); try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); // 1、插入数据
Employee employee = new Employee("小红", "1", "xiaohong@163.com");
Long returnValue = mapper.insertEmployee(employee);
System.out.println("插入返回值:" + returnValue); // 手动提交数据
session.commit(); } finally {
session.close();
}
} // 修改
@Test
public void test2() throws IOException { // 获取SqlSessionFactory
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取的sqlsession 自动提交数据
SqlSession session = sqlSessionFactory.openSession(true);
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); // 2、更新数据
Employee employee = new Employee(1, "小红", "0", "xiaohong@163.com");
boolean returnValue = mapper.updateEmployee(employee);
System.out.println("更新返回值:" + returnValue); } finally {
session.close();
}
} // 删除
@Test
public void test3() throws IOException { // 获取SqlSessionFactory
InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 获取的sqlsession 自动提交数据
SqlSession session = sqlSessionFactory.openSession(true);
try {
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class); // 3、删除数据
Integer returnValue = mapper.deleteEmployeeById(8);
System.out.println("删除返回值:" + returnValue); } finally {
session.close();
}
} /**
* 查询
* @throws IOException
*/
@Test
public void test() throws IOException {
// 1、根据mybatis全局配置文件,获取SqlSessionFactory
String resource = "mybatis-config.xml";
// 使用MyBatis提供的Resources类加载mybatis的配置文件,获取输入流
InputStream inputStream = Resources.getResourceAsStream(resource);
// 构建sqlSession的工厂
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); // 2、从SqlSession工厂中,获取sqlsession,用来执行sql
SqlSession session = sqlSessionFactory.openSession();
try {
// 查询selectOne
// @param statement Unique identifier matching the statement to use. 一个唯一标识
// @param parameter A parameter object to pass to the statement. 参数
EmployeeMapper mapper = session.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmployeeById(1);
// 输出信息
System.out.println("查询返回值:" + employee);
} finally {
// 关闭session
session.close();
}
} }

  5、运行单元测试类,结果如下:
    

    

【Mybatis】MyBatis对表执行CRUD操作(三)的更多相关文章

  1. 使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: <?xml version="1.0&quo ...

  2. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作(转载)

    本文转载自:http://www.cnblogs.com/jpf-java/p/6013540.html 上一篇博文MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybati ...

  3. MyBatis入门学习教程-使用MyBatis对表执行CRUD操作

    上一篇MyBatis学习总结(一)--MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对use ...

  4. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作--基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...

  5. MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

  6. MyBatis学习总结_02_使用MyBatis对表执行CRUD操作

    一.使用MyBatis对表执行CRUD操作——基于XML的实现 1.定义sql映射xml文件 userMapper.xml文件的内容如下: 1 <?xml version="1.0&q ...

  7. 【转】MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    [转]MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据, ...

  8. MyBatis学习笔记(二)——使用MyBatis对表执行CRUD操作

    转自孤傲苍狼的博客:http://www.cnblogs.com/xdp-gacl/p/4262895.html 上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用My ...

  9. 二:MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作

    上一篇博文MyBatis学习总结(一)——MyBatis快速入门中我们讲了如何使用Mybatis查询users表中的数据,算是对MyBatis有一个初步的入门了,今天讲解一下如何使用MyBatis对u ...

随机推荐

  1. 猴子选大王的c#实现

    原文地址:猴子选大王的c#实现作者:余文 今天被问到了猴子选大王的意思,题目大意就是说有n只猴子围坐成一个圈,按顺时针方向从1到n编号.然后从1号猴子开始沿顺时针方向从1开始报数,报到m的猴子出局,再 ...

  2. websocket简单理解

    实现及原理 Websocket是一种在单个TCP连接上进行全双工通讯的协议. WebSocket 首先发起一个 HTTP 请求,在请求头加上 `Upgrade` 字段,该字段用于改变 HTTP 协议版 ...

  3. 5、Zookeeper命令操作

    一.Zookeeper的四字命令 Zookeeper支持某些特定的四字命令字母与其的交互.他们大多数是查询命令,用来获取Zookeeper服务的当前状态及相关信息.用户在客户端可以通过telnet或n ...

  4. shell循环(两个日期比较,改变某个特定日期来改变当前比较值)

    需求:从当前时间前6个月开始执行某个语句,直到执行到当前日期的前一天.  shell脚本如下: #!/bin/bash yesterday=`date -d -1day +%Y%m%d` sixmon ...

  5. centos 支持安装libsodium

    yum install epel-release -y yum install libsodium -y 然后没了.

  6. [Oracle,2018-02-07] Oracle 报错:“ORA-02292:违反完整约束条件(XXX.FKXXX)

    报错的原因很清楚,就是你要删除的记录是另外某条记录的外键,解决办法: 1.删除子记录,在删除本记录: 2.暂时禁用此外键(适合在测试后清除所有记录的情况,记得最后要恢复此外键) 解决办法有了,现在最主 ...

  7. oracle入坑日记<一> 安装

    学习日记系列(前辈/大神勿喷) 一.下载 下载地址:http://www.oracle.com/technetwork/cn/database/enterprise-edition/downloads ...

  8. laravel中消息通知功能

    以laravel5.5为例子,这个功能laravel自带的有: 1.生成表文件的migration文件,再migrate一下在数据库里生成表.命令为:php artisan notifications ...

  9. 为datagrid、treegrid增加右键表头菜单,用于显示或隐藏列,注意:冻结列不在此菜单中

    var createGridHeaderContextMenu = function(e, field) { e.preventDefault(); var grid = $(this);/* gri ...

  10. html css col-md-offset

    有的时候,我们不想让两个相邻的列挨在一起,这时候利用栅格系统的列偏移(offset)功能来实现,而不必再定义margin值.使用.col-md-offset-*形式的样式就可以将列偏移到右侧.例如,. ...