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. 【layui】提交表单

    1 <script type="text/javascript"> layui.use(['form', 'layer', 'jquery'], function () ...

  2. cocoapods diff: /../Podfile.lock: No such file or directory 解决方案

    在运行之前的使用 CocoaPods 工程时,有时会报错:diff: /../Podfile.lock: No such file or directory diff: /Manifest.lock: ...

  3. 高级T-SQL进阶系列 (一)【中篇】:使用 CROSS JOIN 介绍高级T-SQL

    [译注:此文为翻译,由于本人水平所限,疏漏在所难免,欢迎探讨指正] 原文连接:传送门. 当一个CROSS JOIN 表现得如同一个INNER JOIN 在上一章节我提到当你使用一个CROSS JOIN ...

  4. Linux 命令中 more、less、head、tail 命令的用法

    more 命令 more 命令,功能类似 cat ,cat 命令是将整个文件的内容从上到下显示在屏幕上. more 命令会一页一页的显示,方便使用者逐页阅读,而最基本的指令就是按空白键(space)往 ...

  5. cross_val_score 交叉验证与 K折交叉验证,嗯都是抄来的,自己作个参考

    因为sklearn cross_val_score 交叉验证,这个函数没有洗牌功能,添加K 折交叉验证,可以用来选择模型,也可以用来选择特征 sklearn.model_selection.cross ...

  6. 关于java自学的内容以及感受(7.21)

    直接切入正题说一下自学到的内容: 定义合法标识符的规则: 可以由英文字母,数字,_,$组成. 不能数字开头和包含空格. 不可以使用关键字和保留字,但是可以包含关键字和保留字. byte short i ...

  7. elk安装2.0版本,自己总结,比较细致 es单机的安装

    用root用户会报错,版本6之前还是可以用root用户启动的,针对es的保护,数据的一些问题,6之后就不允许了. 启动成功 下面验证一下  可以直接用浏览器访问,接受http访问 配置,因为没有暴露端 ...

  8. Cookie信息保存到本地(MozillaCookieJar)

    from urllib import request from http.cookiejar import MozillaCookieJar cookiejar = MozillaCookieJar( ...

  9. 六 Spring属性注入的四种方式:set方法、构造方法、P名称空间、SPEL表达式

    Spring的属性注入: 构造方法的属性注入 set方法的属性注入

  10. win7系统中开启wifi热点

    1.进入cmd下 2.输入命令创建一个热点,名称为testwifi,密码为12345678 netsh wlan 3.进入网络和共享中心->更改适配器设置,看到多出一个“无线网络连接2”,选中本 ...