1.原始方法开发Dao

Dao接口

 package cn.sm1234.dao;

 import java.util.List;

 import cn.sm1234.domain.Customer;

 public interface CustomerDao {

     public void saveCustomer(Customer customer);

     public void updateCustomer(Customer customer);

     public void deleteCustomer(Integer id);

     public List<Customer> queryAllCustomer();

     public Customer queryCustomerById(Integer id);

     public List<Customer> queryCustomerByName(String name);
}

Dao实现:

 package cn.sm1234.dao.impl;

 import java.util.List;

 import org.apache.ibatis.session.SqlSession;

 import cn.sm1234.dao.CustomerDao;
import cn.sm1234.domain.Customer;
import cn.sm1234.utils.SessionUtils; public class CustomerDaoImpl implements CustomerDao { @Override
public void saveCustomer(Customer customer) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
sqlSession.insert("insertCustomer", customer);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
}
} @Override
public void updateCustomer(Customer customer) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
sqlSession.update("updateCustomer", customer);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} } @Override
public void deleteCustomer(Integer id) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
sqlSession.delete("deleteCustomer", id);
sqlSession.commit();
} catch (Exception e) {
e.printStackTrace();
sqlSession.rollback();
} finally {
sqlSession.close();
} } @Override
public List<Customer> queryAllCustomer() {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
return sqlSession.selectList("queryAllCustomer");
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return null;
} @Override
public Customer queryCustomerById(Integer id) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
return sqlSession.selectOne("queryCustomerById", id);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return null;
} @Override
public List<Customer> queryCustomerByName(String name) {
SqlSession sqlSession = null;
try {
sqlSession = SessionUtils.getSesion();
return sqlSession.selectList("queryCustomerById", name);
} catch (Exception e) {
e.printStackTrace();
} finally {
sqlSession.close();
}
return null;
} }

测试类:

package cn.sm1234.test;

import java.util.List;

import org.junit.Test;

import cn.sm1234.dao.CustomerDao;
import cn.sm1234.dao.impl.CustomerDaoImpl;
import cn.sm1234.domain.Customer; public class Demo1 { @Test
public void test1(){
Customer c = new Customer();
c.setName("陈六222");
c.setGender("男");
c.setTelephone("13244445555"); CustomerDao dao = new CustomerDaoImpl();
dao.saveCustomer(c);
} @Test
public void test2(){
Customer c = new Customer();
c.setId(1);
c.setName("李四"); CustomerDao dao = new CustomerDaoImpl();
dao.updateCustomer(c);
} @Test
public void test3(){
CustomerDao dao = new CustomerDaoImpl();
dao.deleteCustomer(14);
} @Test
public void test4(){
CustomerDao dao = new CustomerDaoImpl();
List<Customer> list = dao.queryAllCustomer();
for (Customer customer : list) {
System.out.println(customer);
}
} @Test
public void test5(){
CustomerDao dao = new CustomerDaoImpl();
Customer customer = dao.queryCustomerById(1);
System.out.println(customer);
} @Test
public void test6(){
CustomerDao dao = new CustomerDaoImpl();
List<Customer> list = dao.queryCustomerByName("%陈%");
for (Customer customer : list) {
System.out.println(customer);
}
}
}

Customer.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">
<!-- 该文件存放CRUD的sql语句 -->
<mapper namespace="test">
<!-- 添加 -->
<insert id="insertCustomer" parameterType="customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert> <!-- 修改 -->
<!-- parameterType传入对象,包含需要使用的值 -->
<update id="updateCustomer" parameterType="customer">
UPDATE t_customer SET NAME = #{name} WHERE id = #{id}
</update> <!-- 查询所有数据 -->
<!-- 输出映射 resultType -->
<!-- parameterType可以省略,resultType不可以省略 -->
<select id="queryAllCustomer" resultType="customer">
SELECT * FROM t_customer
</select> <!-- 根据id查询 -->
<select id="queryCustomerById" parameterType="_int" resultType="customer">
SELECT * FROM t_customer WHERE id=#{value}
</select> <!-- 根据name模糊查询 -->
<select id="queryCustomerByName" parameterType="string" resultType="customer">
<!-- 方法一 -->
SELECT * FROM t_customer WHERE NAME LIKE #{value}
<!-- 方法二 -->
<!-- SELECT * FROM t_customer WHERE NAME LIKE '%${value}%' -->
</select> <!-- 删除 -->
<delete id="deleteCustomer" parameterType="int">
DELETE FROM t_customer WHERE id=#{value}
</delete> </mapper>

缺点:代码繁琐。

解决方法:利用动态代理方式Dao接口开发。Dao层只需要接口,不需要重复的Dao层实现。

2.动态代理方式开发Dao层(推荐使用)

好处:无需再去编写Dao层的实现类。

Dao接口:

 package cn.sm1234.dao;

 import java.util.List;

 import cn.sm1234.domain.Customer;

 public interface CustomerDao {

     public void saveCustomer(Customer customer);

     public void updateCustomer(Customer customer);

     public void deleteCustomer(Integer id);

     public List<Customer> queryAllCustomer();

     public Customer queryCustomerById(Integer id);

     public List<Customer> queryCustomerByName(String name);
}

Customer.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">
<!-- 该文件存放CRUD的sql语句 -->
<!--
如果是动态代理方式
1)namespace必须和Dao接口的路径保持一致
2)Dao接口里面的方法和sql语句的id保持一致
3)Dao接口的方法的参数和返回值类型 和 映射文件的parameterType和resultType要对应
-->
<mapper namespace="cn.sm1234.dao.CustomerDao">
<!-- 添加 -->
<insert id="saveCustomer" parameterType="customer">
INSERT INTO t_customer(NAME,gender,telephone) VALUES(#{name},#{gender},#{telephone})
</insert> <!-- 修改 -->
<!-- parameterType传入对象,包含需要使用的值 -->
<update id="updateCustomer" parameterType="customer">
UPDATE t_customer SET NAME = #{name} WHERE id = #{id}
</update> <!-- 查询所有数据 -->
<!-- 输出映射 resultType -->
<!-- parameterType可以省略,resultType不可以省略 -->
<select id="queryAllCustomer" resultType="customer">
SELECT * FROM t_customer
</select> <!-- 根据id查询 -->
<select id="queryCustomerById" parameterType="_int" resultType="customer">
SELECT * FROM t_customer WHERE id=#{value}
</select> <!-- 根据name模糊查询 -->
<select id="queryCustomerByName" parameterType="string" resultType="customer">
<!-- 方法一 -->
SELECT * FROM t_customer WHERE NAME LIKE #{value}
<!-- 方法二 -->
<!-- SELECT * FROM t_customer WHERE NAME LIKE '%${value}%' -->
</select> <!-- 删除 -->
<delete id="deleteCustomer" parameterType="int">
DELETE FROM t_customer WHERE id=#{value}
</delete> </mapper>

测试代码:

 package cn.sm1234.test;

 import java.util.List;

 import org.apache.ibatis.session.SqlSession;
import org.junit.Test; import cn.sm1234.dao.CustomerDao;
import cn.sm1234.domain.Customer;
import cn.sm1234.utils.SessionUtils; public class Demo2 { @Test
public void test1(){
Customer c = new Customer();
c.setName("陈六333");
c.setGender("男");
c.setTelephone("13244445555"); SqlSession sqlSession = SessionUtils.getSession();
//getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.saveCustomer(c);
sqlSession.commit();
sqlSession.close();
} @Test
public void test2(){
Customer c = new Customer();
c.setId(1);
c.setName("李四222"); SqlSession sqlSession = SessionUtils.getSession();
//getMapper(): 返回指定接口的动态代理的实现类对象
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.updateCustomer(c);
sqlSession.commit();
sqlSession.close();
} @Test
public void test3(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
dao.deleteCustomer(19);
sqlSession.commit();
sqlSession.close();
} @Test
public void test4(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
List<Customer> list = dao.queryAllCustomer();
for (Customer customer : list) {
System.out.println(customer);
}
} @Test
public void test5(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
Customer customer = dao.queryCustomerById(1);
System.out.println(customer);
} @Test
public void test6(){
SqlSession sqlSession = SessionUtils.getSession();
CustomerDao dao = sqlSession.getMapper(CustomerDao.class);
List<Customer> list = dao.queryCustomerByName("%陈%");
for (Customer customer : list) {
System.out.println(customer);
}
}
}

要点总结:

如果是动态代理方式
1)namespace必须和Dao接口的路径保持一致
2)Dao接口里面的方法和sql语句的id保持一致
3) Dao接口的方法的参数和返回值类型 和 映射文件的parameterType和resultType要对应

Mybatis进阶学习笔记——动态代理方式开发Dao接口、Dao层(推荐第二种)的更多相关文章

  1. Mybatis进阶学习笔记——动态sql

    1.if标签 <select id="queryByNameAndTelephone" parameterType="Customer" resultTy ...

  2. Mybatis框架基础入门(三)--Mapper动态代理方式开发

    使用MyBatis开发Dao,通常有两个方法,即原始Dao开发方法和Mapper动态代理开发方法. 原始Dao开发方法需要程序员编写Dao接口和Dao实现类,此方式开发Dao,存在以下问题: Dao方 ...

  3. Spring与MyBatis整合上_Mapper动态代理方式

         将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring来管理..所以该整合,只需将SQLSessionFactory的对象生成器S ...

  4. JS定义函数的2种方式以及区别简述(为什么推荐第二种方式)

     无意中看到了阮一峰大神多年前的一篇博客: 12种不宜使用的Javascript语法    看到第9条的时候受到了启发,感觉之前没怎么理解清楚的一些问题好像突然就清晰了,如下图 可能光这样看,有些小伙 ...

  5. Java学习笔记——动态代理

    所谓动态,也就是说这个东西是可变的,或者说不是一生下来就有的.提到动态就不得不说静态,静态代理,个人觉得是指一个代理在程序中是事先写好的,不能变的,就像上一篇"Java学习笔记——RMI&q ...

  6. Java学习笔记--动态代理

    动态代理 1.JDK动态代理 JDK1.3之后,Java提供了动态代理的技术,允许开发者在运行期创建接口的代理实例.JDK的动态代理主要涉及到java.lang.reflect包中的两个类:Proxy ...

  7. Mybatis进阶学习笔记——输入映射

    1.输入映射 输入映射支持的类型: 1) 基本的类型,int,String,double 等(*)2) JavaBean 类型(*)3) 包装JavaBean 类型(对象里面包含另一个对象) 1.1基 ...

  8. Mybatis进阶学习笔记——关系查询——一对多查询

    一个客户拥有多个订单 <resultMap type="User" id="UserOrderResultMap"> <id column=& ...

  9. Mybatis进阶学习笔记——关系查询——一对一查询

    用户和订单的需求 通过查询订单,查询用户,就是一对一查询 (1)自定义JavaBean(常用,推荐使用) <select id="queryOrderUser" result ...

随机推荐

  1. 分布式 NewSQL 对比

    1.TiDB: 说明: PingCAP 公司基于 Google Spanner / F1 论文实现的开源分布式 NewSQL 数据库. 开源分布式 NewSQL 关系型数据库 TiDB 是新一代开源分 ...

  2. MT【217】韦达定理应用

    若2018次方程$x^{2018}-4036x^{2017}+a_{2016}x^{2016}+\cdots+a_1x+a_0=0$ 有2018个正实数, 则对于所有可能的方程$\sum\limits ...

  3. 04 Zabbix4.0系统配置触发器trigger

    点击返回:自学Zabbix之路 点击返回:自学Zabbix4.0之路 点击返回:自学zabbix集锦 04 Zabbix4.0系统配置触发器trigger 请点击查看Zabbix3.0.8版本trig ...

  4. 自学Linux Shell12.3-case命令

    点击返回 自学Linux命令行与Shell脚本之路 12.3-case命令 有了case命令,就不需要写出所有elif语句来不停的检查同一个变量的值了.case命令会采用列表格式来检查单个变量的多个值 ...

  5. [luogu1962]斐波那契数列

    来提供两个正确的做法: 斐波那契数列双倍项的做法(附加证明) 矩阵快速幂 一.双倍项做法 在偶然之中,在百度中翻到了有关于斐波那契数列的词条(传送门),那么我们可以发现一个这个规律$ \frac{F_ ...

  6. SharePoint 2013 APP 开发示例 (一)List 读写

    在这个示例里,我们将创建一个页面测试 SharePoint APP的权限.这个页面有二个按钮,一个从documents里读数据,一个往documents里写数据: 1. 打开Visual Studio ...

  7. ubuntu 14.04下使用fcitx时将caps lock映射为ctrl

    在~/.xprofile中加入 setxkbmap -option caps:ctrl_modifier 要弄成全局的就在 /etc/X11/Xsession.d/ 里面找个文件塞进去. archli ...

  8. IPython Notebook 运行python Spark程序

    1.安装pip 因为centos7.0自带的python系统是2.7.5,并没有安装pip,需要先安装pip $ wget https://bootstrap.pypa.io/get-pip.py $ ...

  9. python备份网站,并删除指定日期文件

    #!/usr/bin/python# Filename: backup_ver1.pyimport osimport timeimport datetime# 1. The files and dir ...

  10. X-UA-compatible浅谈

    最近了解到svg,原来它出现之前好几年,微软已经推出了vml,但是那时候却被人吐槽无数,看来过早的创新也是失败的原因之一呢~ 为什么谈到这个话题呢?因为IE史上有一个特别奇怪的浏览器IE8,它及不兼容 ...