本例在【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. problem:浏览器如何区分html超文本和普通文本

    运营同学问:后端返回的一串元素标签,我想在网页中显示的时候,将标签中的内容渲染出来,不希望直接显示标签. 回答:bootstrap加模版组织的网页,模版渲染的数据只能渲染字符串,不能转化富文本. 运营 ...

  2. Linux(CentOS-7) 下载 解压 安装 redis 操作的一些基本命令

    使用xshell 连接到虚拟机,并且创建 一个redis目录:创建文件命令:mkdir 文件名ls:查看当前文件里面的所有文件 使用xftp 将下载的linux版本 reids上传动新建的redis目 ...

  3. iterator简单描述

    Item 26. Prefer iterator to const iterator, reverse_iterator, and const_reverse_iterator. 上面一段话,是< ...

  4. java多线程中并发集合和同步集合有哪些?区别是什么?

    java多线程中并发集合和同步集合有哪些? hashmap 是非同步的,故在多线程中是线程不安全的,不过也可以使用 同步类来进行包装: 包装类Collections.synchronizedMap() ...

  5. Android ScrollView嵌套Recyclerview滑动卡顿,松手即停问题解决;

    假如你的布局类似这样的: <ScrollView android:layout_width="match_parent" android:layout_height=&quo ...

  6. node和npm的安装和镜像源的修改

    在node官网下载https://nodejs.org/en/ 直接下载msi的文件,需要配置环境变量 我的电脑-->属性-->高级系统配置-->环境变量-->用户变量,在用户 ...

  7. [leetcode]335. Self Crossing

    You are given an array x of n positive numbers. You start at point (,) and moves x[] metres to the n ...

  8. leetcode1030

    class Solution(object): def __init__(self): self.List = list() def bfs(self,R,C,S,V): T = list() whi ...

  9. nginx 301跳转

    server { server_name xxxx.com; return 301 $scheme://www.xxxx.com$request_uri; }

  10. 解决PHP使用POST提交数据不完整,数据不全的问题

    在后台form中,通过ajax请求返回了一个有很多input的form表单,提交数据后,要格式化数组时发现提交过来的数据不完整. PHP从5.3.9开始 php.ini 增加一个变量 max_inpu ...