1、编写mapper.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"> <!-- 命名空间:其作用是对sql进行分类管理,使用mapper代理开发dao时,namespace需要设置为mapper接口的全类名 -->
<mapper namespace="com.tt.mybatis.mapper.CustomerMapper"> <!--
配置sql语句
通过select执行数据库查询:
id标识映射文件中的sql
parameterType为输入参数的类型;
resultType为输出参数的Java对象类型
-->
<select id="findCustomerById" parameterType="int" resultType="com.tt.po.Customer">
<!-- #{value}表示一个占位符,其中的value可以为任何名称 -->
select * from customers where id=#{id}
</select> <select id="findCustomerByName" parameterType="java.lang.String" resultType="com.tt.po.Customer">
<!-- ${value}表示字符串拼接,{}中的变量只能为value,存在sql注入风险,不建议使用 -->
select * from customers where name like '%${value}%';
</select> <!-- 添加用户 -->
<insert id="insertCustomer" parameterType="com.tt.po.Customer">
<selectKey keyProperty="id" order="AFTER" resultType="java.lang.Integer">
select LAST_INSERT_ID()
</selectKey>
insert into customers(name, email, address) values(#{name}, #{email}, #{address})
</insert> <!-- 删除用户 -->
<delete id="deleteCustomer" parameterType="java.lang.Integer">
delete from customers where id=#{id}
</delete> <!-- 更新用户 -->
<update id="updateCustomer" parameterType="java.util.HashMap">
update customers set name=#{name}, email=#{email},address=#{address} where id=#{id}
</update>
</mapper>

--注意:需要在全局配置文件(SqlMapConfig.xml)中加载该映射文件。

2、编写 mapper 接口,需要遵循一些开发规范,mybatis可以自动生成mybatis接口实现类对象:

--1) 在mapper.xml中的 namespace 等于mapper接口地址(全类名);

--2) mapper.java 接口中的方法和mapper.xml中 statement 的id一致;

--3) mapper.java 接口中的方法的输入参数类型和mapper.xml中的 statement 的 parameterType 指定的类型一致;

--4) mapper.java 接口中的方法的返回值类型和 mapper.xml 中的 statement 的resultType 指定的类型一致;

package com.tt.mybatis.mapper;

import java.util.List;

import com.tt.po.Customer;

public interface CustomerMapper {

    public Customer findCustomerById(int id) throws Exception;
public List<Customer> findCustomerByName(String name) throws Exception;
public void insertCustomer(Customer customer) throws Exception;
public void deleteCustomer(int id) throws Exception;
}

3、测试:

public class CustomerMapperTest {

    private SqlSessionFactory sqlSessionFactory = null;
@Before
public void setUp() throws Exception {
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
} @Test
public void testFindCustomerById() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); Customer customer = customerMapper.findCustomerById(4);
sqlSession.close();
System.out.println(customer);
} @Test
public void testFindCustomerByName() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); List<Customer> customers = customerMapper.findCustomerByName("TT");
sqlSession.close();
System.out.println(customers);
} @Test
public void testInsertCustomer() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); Customer customer = new Customer("TestUser", "test@gmail.com", "China");
customerMapper.insertCustomer(customer);
sqlSession.commit();
sqlSession.close();
System.out.println(customer.getId());
} @Test
public void testDeleteCustomer() throws Exception {
SqlSession sqlSession = sqlSessionFactory.openSession();
CustomerMapper customerMapper = sqlSession.getMapper(CustomerMapper.class); customerMapper.deleteCustomer(9);
sqlSession.commit();
sqlSession.close();
} }

mabatis--使用mapper代理开发dao的更多相关文章

  1. mybatis 学习笔记(三):mapper 代理开发 dao 层

    mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...

  2. Mybatis的mapper代理开发dao方法

    看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...

  3. mybatis入门基础(二)----原始dao的开发和mapper代理开发

    承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...

  4. Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...

  5. 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发

    使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...

  6. MyBatis使用Mapper动态代理开发Dao层

    开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...

  7. 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发

    [原文]https://www.toutiao.com/i6594610137560777223/ 一.什么是MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...

  8. Mybatis学习总结(二)——Mapper代理开发

    一.概要 1.原始DAO开发中存在的问题:(1)DAO实现方法体中存在很多过程性代码.(2)调用SqlSession的方法(select/insert/update)需要指定Statement的id, ...

  9. 31Mybatis_mybatis和spring整合-mapper代理开发

    案例结构图:

随机推荐

  1. LLC半桥谐振变换器调试记录

    1.判断二极管是否击穿 2.判断mos管是否烧坏 直接用声音档,发出响声说明击穿了 3.测试二极管的正负极方法 将万用表调到二极管档 1.信号发生芯片周围的电阻 2.反馈部分的电阻 3.实验准备部分: ...

  2. Bugku - CTF加密篇之滴答~滴

    滴答~滴 答案格式KEY{xxxxxxxxx}

  3. cookie,session,localStorage和sessionStorage

    cookies:存储于浏览器端的数据.可以设置 cookies 的Max-Age或者Expires到期时间,如果不设置时间,则在浏览器关闭窗口的时候会消失. session:存储于服务器端的数据.se ...

  4. ES6-使用模板字符串完成字符串拼接

        var obj = {name:'tom',age:11};     //es5的字符串拼接比较麻烦     var str = '姓名是:'+obj.name+' '+'年龄是:'+obj. ...

  5. 吴裕雄--天生自然Numpy库学习笔记:NumPy 线性代数

    import numpy.matlib import numpy as np a = np.array([[1,2],[3,4]]) b = np.array([[11,12],[13,14]]) p ...

  6. MySQL复制方法

    MySQL的二进制日志,MySQL复制原理,MySQL主从模式搭建,MySQL双主模式搭建,MySQL级联模式搭建,MySQL半同步模式复制 一.二进制日志 1.概念 MySQL的二进制日志(bina ...

  7. linux/centos之配置tomcat

    一:下载tomcat压缩包 在http://archive.apache.org/dist/tomcat/中下载合适版本的tomcat,也可以在官网上下载,只是一般只有最新版本,选择二进制的后缀为ta ...

  8. JAVA GUI窗体及控件

    Swing基本操作: JAVA显示一个带按钮的窗口: import java.awt.*; import javax.swing.*; import javax.swing.border.EmptyB ...

  9. 前端学习 之 CSS(三)

    九:浮动 浮动是css里面布局最多的一个属性,也是很重要的一个属性. float:表示浮动的意思. 属性值: none: 表示不浮动,默认 left: 表示左浮动 right:表示右浮动 例: htm ...

  10. 工具 - PyCharm相关

    Ctrl + Q查看Documentation Ctrl + Alt + L 格式化代码 """""" + enter就可以自动生成DocS ...