zoukankan      html  css  js  c++  java
  • Spring和Mybatis的整合

    • 创建工程,导入所依赖的jar包(这里我们使用DBCP连接池来管理数据源)                                                                 image                                                                              image  
    • 原始Dao整合方法:需要我们编写Dao接口和Dao实现类,在Dao实现类中注入SqlSessionFactory

    1. 在applicationContext.xml中配置SqlSessionFactory

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <!-- 配置外部资源文件 -->
        <context:property-placeholder location="config/jdbc.properties" />
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="username" value="${username}"></property>
            <property name="password" value="${password}"></property>
            <property name="driverClassName" value="${driverClassName}"></property>
            <property name="url" value="${url}"></property>
    
            <property name="maxActive" value="30"></property>
            <property name="maxIdle" value="10"></property>
            <property name="minIdle" value="5"></property>
            <property name="maxWait" value="5000"></property>
        </bean>
    
        <!-- 配置会话工厂SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 加载SqlMapConfig.xml文件 -->
            <property name="configLocation" value="config/SqlMapConfig.xml"></property>
        </bean>
    
        <!-- 配置userDao -->
        <bean id="userDao" class="service.UserDaoImpl">
            <!-- 注入会话工厂SqlSessionFactory -->
            <property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
        </bean>
    </beans>

    2. 编写Dao接口和Dao实现类

    package service;
    
    import bean.User;
    
    public interface UserDao {
        //根据用户信息查询用户信息
        public User findUserById(Integer id);
    }
    package service;
    
    import org.apache.ibatis.session.SqlSession;
    import org.mybatis.spring.support.SqlSessionDaoSupport;
    
    import bean.User;
    
    public class UserDaoImpl extends SqlSessionDaoSupport implements UserDao {
        /**
         * 由于继承了SqlSessionDaoSupport类,该类有一个 
         * public void setSqlSessionFactory(SqlSessionFactory sqlSessionFactory)方法,
         * 所以可以通过Spring容器注入会话工厂
         */
        
        @Override
        public User findUserById(Integer id) {
            //从工厂中得到会话
            SqlSession sqlSession = this.getSqlSession();
            User user = sqlSession.selectOne("config.userMapper.getUser", 1);
            return user;
        }
    }

    3. userMapper.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:SQL映射文件所在的路路径 -->
    <mapper namespace="config.userMapper">
        <!-- parameterType:参数id的值的类型 resultType:查询的结果返回的类型 -->
        <select id="getUser" parameterType="Integer" resultType="bean.User">
            select * from users where id=#{id}
        </select>
    </mapper>

    4. JUnit测试

    package test;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import bean.User;
    import service.UserDao;
    
    public class SpringMybatisTest {
        private ApplicationContext applicationContext;
    
        @Before
        public void setUp() {
            // 构造Spring容器
            applicationContext = new ClassPathXmlApplicationContext(
                    "config/applicationContext.xml");
        }
    
        @Test
        public void test() {
            UserDao userDao = (UserDao) applicationContext.getBean("userDao");
            User user = userDao.findUserById(1);
            System.out.println(user.getAge());
        }
    }
    • mapper动态代理整合方法:MyBatis使用动态代理的方法创建mapper对象,只需要程序员开发mapper接口(也就是Dao接口)和mapper映射文件,且mapper接口和mapper映射文件需要遵循一些规则:

          a. userMapper.xml的namespace是UserMapper.java的全限定名

           b. userMapper.xml中的statement的id值是UserMapper.java中的方法名,parameterTyep是方法的参数列表,resultType是方法的返回值。

    然后在Spring容器中配置mapper扫描器,由扫描器扫描出mapper接口和mapper映射文件,从而动态的生成mapper接口的动态代理对象(mapperj接口的实现类对象)。产生的动态代理对象在Spring容器中自动注册,同时,一个扫描器可以扫描出多个mapper接口,可以生成多个

    mapper接口的动态代理对象。

    1. 在Spring容器中配置mapper扫描器(mapper扫描器类在整合包中)

    applicationContext.xml文件:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xmlns:tx="http://www.springframework.org/schema/tx"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring-1.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
    
        <!-- 配置外部资源文件 -->
        <context:property-placeholder location="config/jdbc.properties" />
    
        <!-- 配置数据源 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="username" value="${username}"></property>
            <property name="password" value="${password}"></property>
            <property name="driverClassName" value="${driverClassName}"></property>
            <property name="url" value="${url}"></property>
    
            <property name="maxActive" value="30"></property>
            <property name="maxIdle" value="10"></property>
            <property name="minIdle" value="5"></property>
            <property name="maxWait" value="5000"></property>
        </bean>
    
        <!-- 配置会话工厂SqlSessionFactory -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!-- 数据源 -->
            <property name="dataSource" ref="dataSource"></property>
            <!-- 加载SqlMapConfig.xml文件 -->
            <property name="configLocation" value="config/SqlMapConfig.xml"></property>
        </bean>
        
        <!-- Spring容器中配置mapper扫描器,
              由于产生的动态代理对象在Spring容器中自动注册,所以id默认值为mapper接口名(首字母小写) -->
        <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
            <!-- 指定扫描包的路径,就是mapper接口的路径 -->
            <property name="basePackage" value="service"></property>
            <!-- 配置sqlSessionFactoryBeanName,也就是配置会话工厂 -->
            <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
        </bean>
    </beans>

    2. UserMapper.java类

    package service;
    
    import bean.User;
    
    public interface UserMapper {
        //根据用户信息查询用户信息
        public User findUserById(Integer id);
    }

    3. userMpper.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:UserMapper.java的全限定名 -->
    <mapper namespace="service.UserMapper">
        <!—id是UserMapper.java接口中方法名,parameterTyep:UserMapper.java接口中方法的参数类型,resultType:方法的返回值类型-->
        <select id="findUserById" parameterType="Integer" resultType="bean.User">
            select * from users where id=#{id}
        </select>
    </mapper>

    4. JUnit测试

    package test;
    
    import org.junit.Before;
    import org.junit.Test;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    import bean.User;
    import service.UserDao;
    import service.UserMapper;
    
    public class SpringMybatisTest {
        private ApplicationContext applicationContext;
    
        @Before
        public void setUp() {
            // 构造Spring容器
            applicationContext = new ClassPathXmlApplicationContext(
                    "config/applicationContext.xml");
        }
    
        @Test
        public void testFindUserById(){
            //从Spring容器中获取mapper动态代理对象
            UserMapper userMapper = (UserMapper) applicationContext.getBean("userMapper");
            User user = userMapper.findUserById(1);
            System.out.println(user.getName());
        }
    }
  • 相关阅读:
    基于Twisted的简单聊天室
    小学题的python实现
    初识Go(8)
    初识Go(7)
    初识Go(6)
    初识Go(5)
    初识Go(4)
    初识Go(3)
    初识Go(2)
    初识Go(1)
  • 原文地址:https://www.cnblogs.com/shi-blog/p/4320625.html
Copyright © 2011-2022 走看看