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. BZOJ3456 城市规划(多项式求逆)

    设f[i]为连通图的数量,g[i]为不连通图的数量,显然有f[i]=2i*(i-1)/2-g[i],g[i]通过枚举1所在连通块大小转移,有g[i]=Σf[j]*C(i-1,j-1)·2(i-j)*( ...

  2. BZOJ5010 FJOI2017矩阵填数(容斥原理)

    如果只考虑某个子矩阵的话,其最大值为v的方案数显然是vsize-(v-1)size.问题在于处理子矩阵间的交叉情况. 如果两个交叉的子矩阵所要求的最大值不同,可以直接把交叉部分划给所要求的最大值较小的 ...

  3. BZOJ4012 HNOI2015开店(树链剖分+主席树)

    考虑这样一个问题:一棵树初始全是白点,有两种操作:把一个点染黑:询问某点到所有黑点的距离之和. 注意到树上两点x和y的距离为depth[x]+depth[y]-depth[lca(x,y)]*2.要求 ...

  4. MT【28】内心外衣下的等腰三角形个数

    解答:30 评:这道题倒不是传统的与内心相关的向量题,传统的与内心或者内切圆有关的两个结论是aIA+bIB+cIC=0以及所谓的"人品公式"S=rp.这里主要是得到此三角形为以AC ...

  5. android 面试准备基础题

    1.    请描述下Activity的生命周期. 必调用的三个方法:onCreate() --> onStart() --> onResume(),用AAA表示 )父Activity启动子 ...

  6. 【转】Linux 移动或重命名文件/目录-mv 的10个实用例子

    熟悉了 复制命令,下一个相关的命令就是 mv 命令.当你想要将文件从一个位置移动到另一个地方并且不想复制它,那么mv 命令是完成这个任务的首选. 初识 mv 命令 mv 命令是一个与cp类似的命令,但 ...

  7. 使用fiddler模拟http请求

    概述  与httpwath相比,fiddler能模拟http请求.能断点调试.http分析统计吸引了我,使用之后感觉这个工具非常不错,这篇文章只单介绍一下fiddler工作原理,简单介绍一下它的重要功 ...

  8. P1382 楼房 set用法小结

    这个sb题目,剧毒... STL大法好 首先,我准备用经典的线段树优化扫描线来做.之前的矩形周长把我困了数天导致我胸有成竹. 然后,敲代码半小时,调试半个月......这个,sb,怎么改都是0分+2个 ...

  9. 简单认识python(一)

    最近本宝宝被一部小说迷的神魂颠倒的,在网络上四处找免费的小说资源,一直哭唧唧的等待着每天更新的一章.实在是太可怜了,本宝宝决定自己学python,自己抓包小说. 既然知道目的地了,那就和本宝宝一起打怪 ...

  10. 读取数据库配置信息的两种方式(以后开发项目用java链接数据库)-------java基础知识

    第一步:先建立jdbc.properties user=root password url/yanlong driver=com.mysql.jdbc.Driver 第一种方式:直接文件读取 pack ...