本例在【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. go语言学习--处理map的无序输出

    最近工作中遇到了这样的一个场景,需要处理一个无限极分类的问题,对于数据结构的定义首先想到了,map,map[int]map[int]struct.通过两层map的定义归类parent_id和id的关系 ...

  2. 关于AsyncSocket

               写篇博客,在我项目中用到了一个很重要的第三方---AsyncSocket,写下我对AsyncSocket使用心得.我的项目中是APP对硬件直接交互,APP对硬件发指令的时候不需要 ...

  3. JQuery miniui使用小记

    1.renderer="onActionRenderer" 如我们需要在一行数据时加上删除操作时,就需要在每行上加上“删除”按钮,以下为实现代码: 列加载时主要的属性

  4. Using a ScrollView - RN4

    使用滚动条. 1. import import {ScrollView} from "react-native"; 2. Using <ScrollView> ... ...

  5. 构建一个Vue项目

    一 我们需要安装vue.js Vue.js官网 当我们已经安装了vue-cli,那么我们需要更新Vue-cli. vue-cli3.0使用及配置 二 安装好了之后: 我们可以直接使用命令:mkdir ...

  6. C# 数字转换成大写

    /// <summary> /// 数字转大写 /// </summary> /// <param name="Num">数字</para ...

  7. mysql 表映射为java bean 手动生成。

    在日常工作中,一般是先建表.后建类.当然也有先UML构建类与类的层级关系,直接生成表.(建模)这里只针对先有表后有类的情况.不采用代码生成器的情况. 例如: 原表结构: ),)) BEGIN ); ) ...

  8. python大法好——面向对象

    python大法好——面向对象 Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.本章节我们将详细介绍Python的面向对象编程. 如果你以前没 ...

  9. Vue.js连接后台数据jsp页面  ̄▽ ̄

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  10. 【ESP8266】、ESP8266通讯使用的AT指令

    一.AT指令介绍 AT(Attention), AT指令一般应用于终端设备和PC应用之间建立连接.通过AT指令来控制. 二.常用AT指令 AT指令主要分为: 基础AT指令,WIFI功能AT指令,TCP ...