zoukankan      html  css  js  c++  java
  • Spring与MyBatis整合上_Mapper动态代理方式

         将MyBatis与Spring进行整合,主要解决的问题就是将SqlSessionFactory对象交由Spring来管理。。所以该整合,只需将SQLSessionFactory的对象生成器SQLSessionFactoryBean注册到Spring容器中,再将其注入给Dao的实现类即可完成整合。

    可以通过2种方式来实现Spring与MyBatis的整合:

    • Mapper动态代理
    • 支持扫描的Mapper动态代理

    一、环境搭建

    二、定义映射文件

     1 import java.util.List;
     2 
     3 import com.jmu.beans.Student;
     4 
     5 //Dao增删改查
     6 public interface IStudentDao {
     7     void insertStudent(Student student);
     8     void deleteById(int id);
     9     void updateStudent(Student student);
    10 
    11     List<String> selectAllStudentsNames();
    12     String selectStudentNameById(int id);
    13 
    14     List<Student> selectAllStudents();
    15     Student selectStudentById(int id);
    16 }
    IStudentDao
     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE mapper
     3 PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
     5 <mapper namespace="com.jmu.dao.IStudentDao">
     6     <insert id="insertStudent">
     7         insert into student(name,age) values(#{name},#{age})
     8     </insert>
     9 
    10     <delete id="deleteById">
    11         delete from student where id=#{XXX}
    12     </delete>
    13 
    14     <update id="updateStudent">
    15         update student set name=#{name},age=#{age} where id=#{id}
    16     </update>
    17 
    18     <select id="selectAllStudents" resultType="student">
    19         select id,name,age from student
    20     </select>
    21 
    22     <select id="selectStudentById" resultType="student">
    23         select id,name,age from student where id=#{XXX}
    24     </select>
    25 </mapper>
    mapper.xml

    三、定义主配置文件

     1 <?xml version="1.0" encoding="UTF-8" ?>
     2 <!DOCTYPE configuration
     3 PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
     4 "http://mybatis.org/dtd/mybatis-3-config.dtd">
     5 <configuration>
     6 
     7     <!--配置别名 -->
     8     <typeAliases>
     9         <package name="com.jmu.beans" />
    10     </typeAliases>
    11 
    12     <mappers>
    13         <!-- <mapper resource="com/jmu/dao/mapper.xml"/> -->
    14         <!--xml文件和dao同名 -->
    15         <package name="com.jmu.dao" />
    16 
    17     </mappers>
    18 
    19 </configuration>
    mybatis.xml

    四、Mapper动态代理方式生成Dao代理对象

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
     4     xsi:schemaLocation="
     5         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
     6         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
     7 
     8 
     9     <!--注册数据源:C3P0 -->
    10     <bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    11         <property name="driverClass" value="${jdbc.driver}" />
    12         <property name="jdbcUrl" value="${jdbc.url}" />
    13         <property name="user" value="${jdbc.user}" />
    14         <property name="password" value="${jdbc.password}" />
    15     </bean>
    16 
    17     <!-- 注册属性文件 -->
    18     <context:property-placeholder location="classpath:jdbc.properties" />
    19     
    20     <bean id="mySqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    21        <property name="configLocation" value="classpath:mybatis.xml"></property>
    22        <property name="dataSource" ref="myDataSource"></property>
    23     </bean>
    24     
    25     
    26     <!--生成Dao的代理对象 -->
    27     <bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
    28         <property name="sqlSessionFactory" ref="mySqlSessionFactory" />
    29         <property name="mapperInterface" value="com.jmu.dao.IStudentDao" />
    30     </bean>
    31 
    32     <!-- 注册Service -->
    33     <bean id="studentService" class="com.jmu.service.StudentServiceImpl">
    34         <property name="dao" ref="studentDao" />
    35     </bean>
    36 </beans>
    applicationContext.xml

    五、测试

     1 import java.util.List;
     2 
     3 import org.junit.Before;
     4 import org.junit.Test;
     5 import org.springframework.context.ApplicationContext;
     6 import org.springframework.context.support.ClassPathXmlApplicationContext;
     7 
     8 import com.jmu.beans.Student;
     9 import com.jmu.service.IStudentService;
    10 
    11 public class MyTest {
    12 
    13     private IStudentService service;
    14 
    15     @Before
    16     public void before() {
    17         //创建容器对象
    18         String resource = "applicationContext.xml";
    19         ApplicationContext ac=new ClassPathXmlApplicationContext(resource);  
    20         service = (IStudentService) ac.getBean("studentService");
    21     }
    22     
    23     @Test
    24     public void test01() {
    25         Student student=new Student("张三", 23);
    26         service.addStudent(student);
    27     }
    28     
    29     @Test
    30     public void test02() {
    31         service.removeById(2);        
    32     }
    33 
    34     @Test
    35     public void test03() {
    36         Student student=new Student("王意义", 23);
    37         student.setId(3);
    38         service.modifyStudent(student);
    39     }
    40     @Test
    41     public void test04() {
    42         List<String> names = service.findAllStudentsNames();
    43         System.out.println(names);
    44     }
    45     @Test
    46     public void test05() {
    47         String names = service.findStudentNameById(3);
    48         System.out.println(names);
    49     }
    50     
    51     @Test
    52     public void test06() {
    53         List<Student> students=service.findAllStudents();
    54         for (Student student : students) {
    55             System.out.println(student);
    56         }
    57     }
    58     
    59     @Test
    60     public void test07() {
    61         Student student=service.findStudentById(3);
    62         System.out.println(student);
    63     }
    64     
    65     
    66 }
    MyTest

    对于IstudentDao.xml中没有写到的List<String> selectAllStudentsNames()和String selectStudentNameById(int id)进行修改

    六、支持扫描的动态代理

    对于上面的情况,如果有多个Dao接口,就要写多次的以下字段

    <!--生成Dao的代理对象 -->
        <bean id="studentDao" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <property name="sqlSessionFactory" ref="mySqlSessionFactory" />
            <property name="mapperInterface" value="com.jmu.dao.IStudentDao" />
        </bean>
     1 <!--生成Dao的代理对象 
     2      当前配置会被本包中所有的接口生成代理对象
     3     -->
     4     <bean  class="org.mybatis.spring.mapper.MapperScannerConfigurer">
     5         <property name="sqlSessionFactoryBeanName" value="mySqlSessionFactory" />
     6         <property name="basePackage" value="com.jmu.dao" />
     7     </bean>
     8 
     9     <!-- 注册Service -->
    10     <bean id="studentService" class="com.jmu.service.StudentServiceImpl">
    11          <!-- 这里的Dao的注入需要使用ref属性,且其作为接口的简单类名 -->
    12         <property name="dao" ref="studentDao" />
    13     </bean>
    applicationContext


  • 相关阅读:
    Java的内存结构(Memory Structure)和垃圾收集(Garbage Collection)图解
    走遍天下的三大vb控制结构
    程序员快速阅读,绝对不是神话
    Android4.0.3源码分析——开机流程之Zygote
    云端的天使
    提高班的“伞”
    Android 4.0.3 源代码结构分析(一)
    如何在Java中选择Map/List/Set
    关于Hadoop中reducer端combiner的一些思考
    vb添加GIF动态图片
  • 原文地址:https://www.cnblogs.com/hoje/p/8488931.html
Copyright © 2011-2022 走看看