spring与mybatis四种整合方法

一、采用org.mybatis.spring.mapper.MapperScannerConfigurer

整体结构如下图:

spring与mybatis四种整合方法_第1张图片

1、配置文件

1>applicationContext01.xml



    
    
    
    
    
        
    
   
    
      
        
        
        
        
    
 
    
    
        
        
        
        
        
        
    
 
    
    
        
        
        
        
    
   
    
    

2>jdbc.properties外置文件

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/demo?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root

3>mybatis-config.xml


  
 

    
       
    

4>studentMapper.xml



 

   
    
        insert intot_app_student(id,name,sex,age) values(#{id},#{name},#{sex},#{age})
      
    
        select * from t_app_student where id =#{id}
      

5>pom.xml


  4.0.0
 
  hys.web.project
  hys_demo_ssm
  0.0.1-SNAPSHOT
  jar
  hys_demo_ssm
  http://maven.apache.org
 
  
    UTF-8
  
 
  
    
      junit
      junit
      4.10
      test
    
   
    
    
        commons-dbcp
        commons-dbcp
        1.4
    
   
       
       mysql
       mysql-connector-java
       5.0.8
    
   
    
    
        org.mybatis
        mybatis
        3.4.1
    
   
    
    
        org.mybatis
        mybatis-spring
        1.3.0
    
   
    
    
        org.springframework
        spring-web
        4.2.0.RELEASE
    
   
    
        org.springframework
        spring-orm
        4.2.0.RELEASE
    
   
    
        org.springframework
        spring-expression
        4.2.0.RELEASE
    
   
  

建立表: 

CREATE TABLE t_app_student
( id VARCHAR(32) NOT NULL PRIMARY KEY,
  name VARCHAR(15),
  sex VARCHAR(2),
  age INT
)

2、创建actionservicedaoentity

1>实体类Student

packagecom.hys.app.student.entity;
 
public class Student {
   
    private Stringid;
    private Stringname;
    private Stringsex;
    private int age;
   
    publicStudent(){
      
    }  
    publicStudent(Stringid, Stringname, Stringsex,intage) {
       this.id =id;
       this.name =name;
       this.sex =sex;
       this.age =age;
    }
    public StringgetId() {
       returnid;
    }
    public void setId(String id) {
       this.id =id;
    }
    public StringgetName() {
       returnname;
    }
    public void setName(String name) {
       this.name =name;
    }
    public StringgetSex() {
       returnsex;
    }
    public void setSex(String sex) {
       this.sex =sex;
    }
    public int getAge() {
       returnage;
    }
    public void setAge(intage) {
       this.age =age;
    }
    @Override
    public StringtoString() {
       return"Student [id=" +id +", name=" +name +", sex=" +sex
              + ", age=" + age + "]";
    }
   
}

2>dao接口

packagecom.hys.app.student.dao;
importcom.hys.app.student.entity.Student;
 
public interface StudentDao {
   public void save(Student student);
   public Student getStudent(Stringid);
}

3>service服务

packagecom.hys.app.student.service;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.hys.app.student.dao.StudentDao;
importcom.hys.app.student.entity.Student;
 
@Service
publicclass StudentService {
   
    @Autowired
    private StudentDao studentDao;
   
    public void save(Student student) {
       studentDao.save(student);
    }
   
    public Student getStudent(String id) {
       Student student =studentDao.getStudent(id);
       return student;
    }
   
}

4>action测试类

packagecom.hys.app.student.action;
importorg.junit.Test;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.context.ApplicationContext;
importorg.springframework.context.support.ClassPathXmlApplicationContext;
importorg.springframework.stereotype.Controller;
importcom.hys.app.student.entity.Student;
importcom.hys.app.student.service.StudentService;
 
@Controller
publicclass StudentAction {
   
    @Autowired
    private StudentService studentService;
 
    @Test
    public void test1(){
       //获取上下文对象
       ApplicationContext context =newClassPathXmlApplicationContext("applicationContext01.xml");
       StudentAction studentAction =(StudentAction)context.getBean("studentAction");
      
//     Student student = newStudent("002","小明","男",20);
//     studentAction.studentService.save(student);
       Student std = studentAction.studentService.getStudent("001");
       System.out.println(std);
    }
}

注:重复的文件在下面的例子中不在贴出来,在测试类中注意替换applicationContext01.xml文件名

二、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数

整体结构如下图:

spring与mybatis四种整合方法_第2张图片

1、配置文件

applicationContext04.xml



    
    
    
    
    
       
    
   
    
      
       
       
       
        
    
 
    
    
       
    
 
     
   
    
    
       
       
       
       
    
   
    
    

2、创建actionservicedaoentity

1>dao接口

packagecom.hys.app.student.dao;
importorg.apache.ibatis.annotations.Insert;
importorg.apache.ibatis.annotations.Select;
importcom.hys.app.student.entity.Student;
 
publicinterface StudentDao {
    @Insert(" insert intot_app_student(id,name,sex,age) values(#{id},#{name},#{sex},#{age})")
    public void save(Student student);
    @Select("select * from t_app_studentwhere id = #{id}")
    public Student getStudent(String id);
}

2>service服务

packagecom.hys.app.student.service;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.hys.app.student.dao.StudentDao;
importcom.hys.app.student.entity.Student;
 
@Service
publicclass StudentService {
   
    @Autowired
    private StudentDao studentDao;
   
    public void save(Student student) {
       studentDao.save(student);
    }
   
    public Student getStudent(String id) {
       Student student =studentDao.getStudent(id);
       return student;
    }
   
}

 三、采用抽象类org.mybatis.spring.support.SqlSessionDaoSupport提供SqlSession

整体结构如下图:

spring与mybatis四种整合方法_第3张图片

1、配置文件

applicationContext03.xml



    
    
    
    
    
        
    
   
    
      
       
       
       
        
    
 
    
      
      
    
   
    
    
        
    
   

studentMapper.xml



 

    
        insert intot_app_student(id,name,sex,age) values(#{id},#{name},#{sex},#{age})
     
    
        select * from t_app_student where id =#{id}
       

2、创建actionservicedaoentity

1>dao接口

packagecom.hys.app.student.dao;
importcom.hys.app.student.entity.Student;
 
public interface StudentDao {
    public void save(Student student);
    public StudentgetStudent(Stringid);
}

2>dao接口实现类

packagecom.hys.app.student.dao;
importjavax.annotation.Resource;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.mybatis.spring.support.SqlSessionDaoSupport;
importorg.springframework.stereotype.Service;
importcom.hys.app.student.entity.Student;
 
@Service
publicclass StudentDaoImp extends SqlSessionDaoSupport implements StudentDao{
   
     /**
      * 我们发现这个类中没有把SqlSessionTemplate作为一个属性,因为我们继承了SqlSessionDaoSupport
      *SqlSessionDaoSupport他会提供sqlsession
     */
 
    @Override
    public void save(Student student) {
       // TODO Auto-generated method stub
       this.getSqlSession().insert("com.hys.app.student.dao.StudentDao.save",student);
    }
 
    @Override
    public Student getStudent(String id) {   
       // TODO Auto-generated method stub
       returnthis.getSqlSession().selectOne("com.hys.app.student.dao.StudentDao.getStudent",id);
    }
 
    /**
     * 使用SqlSessionDaoSupport必须注意,此处源码1.1.1中有自动注入,1.2中取消了自动注入,需要手工注入,侵入性强
     * 也可在spring-mybatis.xml中如下配置,但是这种有多少个dao就要配置到多少个,多个dao就很麻烦。
     * 
     * 
     * 
     */
    @Resource
    public voidsetSqlSessionTemplate(SqlSessionTemplate sqlSessionTemplate){
       super.setSqlSessionTemplate(sqlSessionTemplate);
    }
   
}

3>service服务

packagecom.hys.app.student.service;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.hys.app.student.dao.StudentDaoImp;
importcom.hys.app.student.entity.Student;
 
@Service
publicclass StudentService {
   
    @Autowired
    private StudentDaoImp studentDaoImp;
   
    public void save(Student student) {
       studentDaoImp.save(student);      
    }
   
    public Student getStudent(String id) {
       Student student =studentDaoImp.getStudent(id);
       return student;
    }
 
}

四、采用接口org.apache.ibatis.session.SqlSession的实现类org.mybatis.spring.SqlSessionTemplate

整体结构如下图:

spring与mybatis四种整合方法_第4张图片

1、配置文件

applicationContext02.xml



    
    
    
    
    
        
    
   
    
      
       
       
       
        
    
 
    
      
      
    
   
    
    
        
    
     

studentMapper.xml



 

    
        insert intot_app_student(id,name,sex,age) values(#{id},#{name},#{sex},#{age})
    
    
        select * from t_app_student where id =#{id}
       
 

2、创建actionservicedaoentity

1>dao接口

package com.hys.app.student.dao;
import com.hys.app.student.entity.Student;
 
public interface StudentDao {
    public void save(Student student);
    public Student getStudent(Stringid);
}

2>service服务

packagecom.hys.app.student.service;
importorg.mybatis.spring.SqlSessionTemplate;
importorg.springframework.beans.factory.annotation.Autowired;
importorg.springframework.stereotype.Service;
importcom.hys.app.student.entity.Student;
 
@Service
publicclass StudentService {
   
    /**
     *sqlSessionTemplate模板提供了sqlsession
     */
   
    @Autowired
    private SqlSessionTemplatesqlSessionTemplate;
   
    public void save(Student student) {
       sqlSessionTemplate.insert("com.hys.app.student.dao.StudentDao.save",student);      
    }
   
    public Student getStudent(String id) {
       Student student =sqlSessionTemplate.selectOne("com.hys.app.student.dao.StudentDao.getStudent",id);
       return student;
    }
 
}

你可能感兴趣的:(mybatis)