mabatis--使用mapper代理开发dao
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的更多相关文章
- mybatis 学习笔记(三):mapper 代理开发 dao 层
mybatis 学习笔记(三):mapper 代理开发 dao 层 优势 通过使用mapper 代理,我们可以不需要去编写具体的实现类(使用 getMapper() 方法自动生成),只需编写接口即可, ...
- Mybatis的mapper代理开发dao方法
看完了之前的mybatis原始的dao开发方法是不是觉得有点笨重,甚至说没有发挥mybatis 作为一个框架的优势.总结了一下,原始的dao方法有以下几点不足之处 dao接口实现方法中存在大量的模板方 ...
- mybatis入门基础(二)----原始dao的开发和mapper代理开发
承接上一篇 mybatis入门基础(一) 看过上一篇的朋友,肯定可以看出,里面的MybatisService中存在大量的重复代码,看起来不是很清楚,但第一次那样写,是为了解mybatis的执行步骤,先 ...
- Spring+SpringMVC+MyBatis深入学习及搭建(二)——MyBatis原始Dao开发和mapper代理开发
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6869133.html 前面有写到Spring+SpringMVC+MyBatis深入学习及搭建(一)——My ...
- 【mybatis深度历险系列】深入浅出mybatis中原始dao的开发和mapper代理开发
使用Mybatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper接口开发方法.mybatis在进行dao开发的时候,涉及到三姐妹,分别是SqlSessionFactoryBuilder ...
- MyBatis使用Mapper动态代理开发Dao层
开发规范 Mapper接口开发方法只需要程序员编写Mapper接口(相当于Dao接口),由Mybatis框架根据接口定义创建接口的动态代理对象,代理对象的方法体同原始Dao接口实现类方法. Mappe ...
- 【转】Mybatis学习---MyBatis知识、原始Dao开发和mapper代理开发
[原文]https://www.toutiao.com/i6594610137560777223/ 一.什么是MyBatis MyBatis 是一款优秀的持久层框架,它支持定制化 SQL.存储过程以及 ...
- Mybatis学习总结(二)——Mapper代理开发
一.概要 1.原始DAO开发中存在的问题:(1)DAO实现方法体中存在很多过程性代码.(2)调用SqlSession的方法(select/insert/update)需要指定Statement的id, ...
- 31Mybatis_mybatis和spring整合-mapper代理开发
案例结构图:
随机推荐
- Django框架之登录案例
内容: (1)request.GET和request.POST (2)获取get方法提交和post方法提交的数据 一.登录案例 登录逻辑代码 def login(request): if reques ...
- 安装完 Ubuntu 16.04.1,重启出现[sda] Assuming drive cache: write through的问题
重装了一下ubuntu,安装成功后重启出现了这个问题 刚开始以为是重启慢,就没在意这么多,可是我等了半个小时,(我特么的真闲,其实是忙别的忘了),还不行,咦,然后我就去找了找问题,哈哈哈哈 看图说话, ...
- elasticsearch数据组织结构
elasticsearch数据组织结构 1. mapping 1.1. 简介 mapping:意为映射关系,特别是指组织结构.在此语境中可理解为数据结构,包括表结构,表约束,数据类型等 ...
- fastdfs下载文件自定义文件名称
fdfs 存储节点storage安装nginx,修改nginx配置文件 location /group1/M00/ { root /fdfs/storage/data; if ($arg_attnam ...
- HDU1875 畅通工程再续
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全 ...
- 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ
题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数 ...
- 基础总结篇之八:创建及调用自己的ContentProvider
转自:http://blog.csdn.net/wellsoho/article/details/49494141 若不能坚持到底,即使是朽木也不能折断:只要坚持不停地用刀刻,就算是金属玉石也可以雕出 ...
- JavaScript中关于隐式转换的一些总结
JavaScript运算符中的隐式转换规律:一.递增递减运算符(前置.后置)1.如果包含的是有效数字字符串或者是有效浮点数字符串,则会将字符串转换(Number())为数值,再进行加减操作,返回值的类 ...
- python浅析格式化输出和深浅copy
一,格式化输出 今天主要想记录一下关于格式化输出的例子,然后结合了自己的理解,分析如下: 格式是 :百分号+占位符 主要有三种使用形式:%s (其中s表示string)表示字符串 %d (其中d表 ...
- IIS 配置迁移
使用管理员身份运行cmd 应用程序池: # 导出所有应用程序池 %windir%\system32\inetsrv\appcmd list apppool /config /xml > c:\a ...