MyBatis与Spring的传统DAO方式的开发整合

本文介绍如何通过MyBatis-Spring整合实现持久层操作,包括使用SqlSessionTemplate管理和调用MyBatis SQL方法,定义Customer实体类,创建CustomerMapper.xml映射文件,实现CustomerDao接口以及整合测试。

SqlSessionTemplate:是mybatis-spring 的核心类,它负责管理MyBatis的SqlSession,调用MyBatis的SQL方法。当调用SQL方法时,SqlSessionTemplate将会保证使用的SqlSession和当前Spring的事务是相关的。它还管理SqlSession的生命周期,包含必要的关闭、提交和回滚操作。

SqlSessionDaoSupport:是一个抽象支持类,它继承了DaoSupport类,主要是作为DAO的基类来使用。可以通过SqlSessionDaoSupport类的getSqlSession()方法来获取所需的SqlSession.

1.实现持久层

(1)在src目录下,创建一个com.kang.po包,并在包中创建Customer类

package com.kangxg.po;
/*
 *  客户持久化类
 */
public class Customer {
  //
  private Integer id;
  private String  username;
  private String  jobs;
  private String  phone;
  
  public Integer getId()
  {
     return this.id ;
  }
  public void setId(Integer id)
  {
      this.id = id;
  }
    
  public String getUsername()
  {
     return this.username ;
  }
  public void setIUsername(String username)
  {
      this.username = username;
  }
  public String getJobs()
  {
     return this.jobs ;
  }
  public void setJobs(String jobs)
  {
      this.jobs = jobs;
  }
  public String getPhone()
  {
     return this.phone ;
  }
  public void setPhone(String phone)
  {
      this.phone = phone;
  }
  @Override
  public String toString()
  {
      return "Customer [id =" + id +"," +"username =" +username +", jobs =" +jobs +", phone =" +phone +"]";
  }
}


(2)在com.kangxg.po包中创建 CustomerMapper.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">
<!-- namespace 表示命名空间  -->
<mapper namespace="com.kangxg.po.CustomerMapper">

  <select id="findCustomerById" parameterType = "Integer" resultType="Customer">
      select * from t_customer 
      where
      id =#{id}
  </select>
  
</mapper>


(3) 在 MyBatis的配置文件mybatis-config.xml中 配置映射文件 CustomerMapper.xml的位置

 <mapper resource="com/kangxg/po/CustomerMapper.xml"/>


2.实现DAO层

(1) 在src目录下,创建一个com.kangxg.dao包,并在包中创建接口CustomerDao

package com.kangxg.dao;
import com.kangxg.po.Customer;
public interface CustomerDao {
   public Customer findCustomerById(Integer id);
   
}


(2) 在src目录下,创建一个com.kangxg.dao.impl包 并创建CustomerDao接口实现类 CustomerDaoImpl

package com.kangxg.dao.impl;
import org.mybatis.spring.support.SqlSessionDaoSupport;
import com.kangxg.dao.CustomerDao;

import com.kangxg.po.Customer;

public class CustomerDaoImpl extends SqlSessionDaoSupport implements CustomerDao {
     public Customer findCustomerById(Integer id)
     {
        return this.getSqlSession().selectOne("com.kangxg.po"+".CustomerMapper.findCustomerById",id);
     }
}


(3)在Spring的配置文件applicationContext.xml中编写CustomerDaoImpl的配置

    <bean id = "CustomerDao" class ="com.kangxg.dao.impl.CustomerDaoImpl">
        <property name="sqlSessionFactory" ref ="sqlSessionFactory"></property>
    </bean>


3.整合测试

在src目录下,创建一个com.kangxg.test包 并创建测试类DaoTest

package com.kangxg.test;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.kangxg.dao.CustomerDao;
import com.kangxg.po.Customer;


public class DaoTest {

    @Test
    public void findCustomerByIdTest(){
        String xmlPath = "applicationContext.xml";
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath);
        CustomerDao customerDao =(CustomerDao) applicationContext.getBean("CustomerDao");
        Customer  customer = customerDao.findCustomerById(1);
        System.out.println(customer);
    }

}


4.debug 运行程序

DEBUG [main] - ==>  Preparing: select * from t_customer where id =? 
DEBUG [main] - ==> Parameters: 1(Integer)
DEBUG [main] - <==      Total: 1
Customer [id =1,username =kangxf, jobs =java, phone =13111111111]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值