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代理开发
案例结构图:
随机推荐
- 详解mysql中的Using与On的用法
多用才可以体会各个关键字的用法啊... 原文来自[http://bbs.php100.com/read-htm-tid-148469.html] 在用Join进行多表联合查询时,我们通常使用On来建立 ...
- opencv python:ROI 与 泛洪填充
提取ROI区域,处理然后放回去: 泛洪填充 测试代码:显示一张图像,鼠标点击之后,会从该点开始进行填充,显示填充后的结果图像 注:二值图像的填充需要使用选项:cv2.FLOODFILL_MASK_ON ...
- OpenCV介绍
OpenCV 是什么 OpenCV是计算机视觉开源库,主要算法涉及图像处理和机器学习相关方法. 是 Intel 公司贡献出来的,俄罗斯工程师贡献大部分 C/C++ 代码 在多数图像处理相关的应用程序中 ...
- java集合知识点
若不重写equals方法,则调用的是object对象的equals方法,相当于==比较,比较的是对象的内存地址 |------Collection接口:单列集合,用来存储一个一个对象 |------L ...
- java 8 list的stream操作 list中的对象中的某一个成员取出转为该成员的list,以及对象过滤,筛选某个属性后的成员
取成员属性list List<String> configList = codeEntityList.stream().map(t -> t.getName()).distinct( ...
- vue+element ui table组件封装,使用render渲染
后台管理经常会用到表格,一开始封装了一个常用的功能性表格,点击这里: 后来由于需求增加,在表格中还会用到switch,select,input等多种组件,每次都要在html中增加<el-tabl ...
- 树莓派4B踩坑指南 - (9)安装Git和Docker
安装Git sudo apt-get install wget git-core 安装Docker curl -sSL https://get.docker.com | sh # 树莓派专属脚本福利, ...
- 如何为开发项目编写规范的README文件
前言 了解一个项目,首先都是通过其Readme文件了解信息.如果你以为Readme文件都是随便写写的那你就错了.github,oschina git gitcafe的代码托管平台上的项目的Readme ...
- spring boot 动态注入bean
方法一 SpringContextUtil public class SpringContextUtil { private static ApplicationContext application ...
- Mybatis之foreach用法----List、Array、Map三种类型遍历
在mybatis的xml文件中构建动态sql语句时,经常会用到标签遍历查询条件.特此记录下不同情况下书写方式!-------仅供大家参考------ 1. foreach元素的属性 collectio ...