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. 通过命令创建Django项目

    本人是使用window10操作系统进行讲解Django框架,Linux系统和windows版本几乎一致,可以自行学习就可以解决. 首先在系统上创建了虚拟环境,如果不会创建,可以根据这篇文章学习:htt ...

  2. opencv python:ROI 与 泛洪填充

    提取ROI区域,处理然后放回去: 泛洪填充 测试代码:显示一张图像,鼠标点击之后,会从该点开始进行填充,显示填充后的结果图像 注:二值图像的填充需要使用选项:cv2.FLOODFILL_MASK_ON ...

  3. 【转】使用普通用户执行docker

    原文:https://www.cnblogs.com/klvchen/p/9098745.html CentOS 版本 7.4,Docker 版本 docker-1.13 及以下 ll /var/ru ...

  4. Python学习第二十六课——PyMySql(python 链接数据库)

    Python 链接数据库: 需要先安装pymysql 包 可以设置中安装,也可以pip install pymysql 安装 加载驱动: import pymysql # 需要先安装pymysql 包 ...

  5. HDU 1372 Knight Moves(bfs)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 这是一道很典型的bfs,跟马走日字一个道理,然后用dir数组确定骑士可以走的几个方向, ...

  6. web优化(一 前端)

    当我们在浏览器地址栏中输入一个URL的时候,网页开始请求,我们在页面上看到的内容就是许多个HTTP请求从服务器返回的数据展示,这些展示的快慢很大程度依赖前端的优化,怎样做好前端的优化,我这里总结了几点 ...

  7. VB.NET中Sub和Function的区别

    function是函数,sub是子程序,都可以传递参数,但函数有返回值,子程序没有 function 可以用自身名字返回一个值,sub 需定义别的变量,用传址方式传回值. Sub 过程与Functio ...

  8. ADO.Net实体数据模型添加DB-First/Code First报错

    Authentication method 'caching_sha2_password' not supported by any of the available plugins. 解决办法: 1 ...

  9. char、pchar、string互相转换

    1.string转换成pchar 可以使用pchar进行强制类型转换,也可以使用StrPCopy函数 var s:string; p,p1:PChar; begin s:='Hello Delphi' ...

  10. 七 Struts2访问Servlet的API方式二:原生方式

    Struts2访问Servlet的API方式二:原生方式 和解耦合的方式不同,原生方式既可以拿到域对象,也可以调用域对象中的方法 前端jsp: <%@ page language="j ...