1、接口定义

package com.mybatis.dao;

import com.mybatis.bean.Employee;

public interface EmployeeMapper {
public Employee getEmpById(Integer id); public void addEmp(Employee employee); public void updateEmp(Employee employee); public void deleteEmpById(Integer id);
}

2、mapper编写

<?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.mybatis.dao.EmployeeMapper">
<!--public Employee getEmpById(Integer id);-->
<select id="getEmpById" resultType="com.mybatis.bean.Employee">
select * from tbl_employee where id = #{id}
</select> <!--public void addEmp(Employee employee);-->
<!-- parameterType:参数类型,可以省略-->
<insert id="addEmp" parameterType="com.mybatis.bean.Employee">
insert into tbl_employee(last_name, gender, email)
values (#{lastName},#{gender},#{email})
</insert> <!--public void updateEmp(Employee employee);-->
<update id="updateEmp">
update tbl_employee set last_name=#{lastName},email=#{email},gender=#{gender} where id=#{id}
</update> <!--public void deleteEmpById(Integer id);-->
<delete id="deleteEmpById">
delete from tbl_employee where id=#{id}
</delete>
</mapper>

3、测试代码

package com.mybatis.demo;

import java.io.IOException;
import java.io.InputStream; import com.mybatis.bean.Employee;
import com.mybatis.dao.EmployeeMapper;
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; public class MyTest {
public SqlSessionFactory getSqlSessionFactory() throws IOException {
String resource = "mybatis-config.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
return new SqlSessionFactoryBuilder().build(inputStream);
} /**
* 测试增删改
* 1、mybatis允许增删改直接定义以下类型返回值
* Integer、Long、Boolean、void
* 2、需要手动提交数据
* sqlSessionFactory.openSession();===》手动提交
* sqlSessionFactory.openSession(true);===》自动提交
*/ @Test
public void testSelect() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
Employee employee = mapper.getEmpById(1);
System.out.println(employee);
} finally {
openSession.close();
}
} @Test
public void testInsert() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//此处获取到的SqlSession不会自动提交数据
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
mapper.addEmp(new Employee("jetty", "jetty@126.com", 1));
//手动提交数据
openSession.commit();
} finally {
openSession.close();
} } @Test
public void testUpdate() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
SqlSession openSession = sqlSessionFactory.openSession();
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
mapper.updateEmp(new Employee(2, "jerry", "jerry@qq.com", 1));
openSession.commit();
} finally {
openSession.close();
} } @Test
public void testDelete() throws IOException {
SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
//自动提交
SqlSession openSession = sqlSessionFactory.openSession(true);
try {
EmployeeMapper mapper = openSession.getMapper(EmployeeMapper.class);
mapper.deleteEmpById(2);
} finally {
openSession.close();
} }
}

最新文章

  1. deepin 15.3 安装配置nginx
  2. mysql 查询数据库表结构
  3. 利用grunt-contrib-connect和grunt-connect-proxy搭建前后端分离的开发环境
  4. BFC以及文档流
  5. python3爬虫 url管理器
  6. 新旧各版本的MySQL可以从这里下载
  7. iOS ARC下dealloc过程及.cxx_destruct的探究
  8. 使用PHP抓取网站ico图标
  9. 使用httpclient抓取时,netstat 发现很多time_wait连接
  10. 为什么不要在viewDidLoad方法中设置开始监听键盘通知
  11. GTX 750TI 使用 ffmpeg 时无法用 GPU HEVC(h.265) 进行加速
  12. 在Ubuntu15.10中,使用wxPython的webview和JS进行交互
  13. 3.sparkSQL整合Hive
  14. 自定义django-admin命令
  15. Cognos中新建SQLserver数据源的步骤
  16. map reduce相关程序
  17. 在eclipse中使用Maven3(笔记二)
  18. 海思hi3518 opencv测试
  19. 省市联动js代码
  20. hihocoder 1178 : 计数

热门文章

  1. 实现斗地主纸牌游戏---洗牌 发牌 看底牌的具体功能------Map集合存储方法 遍历的应用
  2. C#之WinForm界面分辨率问题
  3. C#利用phantomJS抓取AjAX动态页面
  4. Kotlin 数据类型(数值类型)
  5. ubuntu - 14.04,必须会的技能-安装PPA源中的程序,更大范围使用deb格式安装文件!!
  6. UIPasteboard
  7. loj #2254. 「SNOI2017」一个简单的询问
  8. Oracle基本函数即字段拆分
  9. ios对new Date() 的兼容问题
  10. Julia体验 语言特性 元编程,宏