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

    1、导入jar包

    2、创建SqlMapConfig文件

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE configuration
            PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-config.dtd">
    
    <configuration>
    
        <typeAliases>
            <typeAlias type="com.jack.po.User" alias="User"/>
        </typeAliases>
        <mappers>
            <mapper resource="com/jack/dao/user.xml" />
        </mappers>
    </configuration>
    

    3、创建User.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">
    <mapper namespace="test">
        <select id="findUser" parameterType="int" resultType="User">
            SELECT * FROM User WHERE id = #{value}
        </select>
    </mapper>
    

    4、创建Spring的配置文件

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
    		http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
    		http://www.springframework.org/schema/mvc
    		http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    		http://www.springframework.org/schema/context
    		http://www.springframework.org/schema/context/spring-context-3.2.xsd
    		http://www.springframework.org/schema/aop
    		http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
    		http://www.springframework.org/schema/tx
    		http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
        <!--加载配置文件-->
        <context:property-placeholder location="classpath:db.properties" />
        <!--配置数据源,使用dbcp-->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="${jdbc.driver}" />
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.name}" />
            <property name="password" value="${jdbc.pwd}" />
        </bean>
        <!--配置SqlSessionFactory-->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean" >
            <property name="dataSource" ref="dataSource" />
            <property name="configLocation" value="classpath:SqlMapConfig.xml" />
    
        </bean>
        <!--配置接口得实现类-->
        <bean id="userImpl" class="com.jack.dao.impl.UserImple">
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
        </bean>
    </beans>
    

    5、接口文件

    public interface UserDao {
        public User findUser(int id) throws Exception;
    }
    

    6、接口实现

    public class UserImple extends SqlSessionDaoSupport implements UserDao {
    
    	    public User findUser(int id) throws Exception {
    	        SqlSession session = this.getSqlSession();
    	        User user = session.selectOne("findUser",id);
    	        return user;
    	    }
    	
    	}
    

    7、测试代码

    public class Test {
    
        private ApplicationContext applicationContext;
    
        @Before
        public void setUp() throws Exception{
            applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
    
        }
        @org.junit.Test
        public void testFindUserById() throws  Exception{
            UserDao userDao = (UserDao) applicationContext.getBean("userImpl");
            User user = userDao.findUser(1);
            System.out.println(user);
        }
    
    }
    

    8、使用Mapper代理开发方式

    1、通过MapperFactoryBean创建代理对象

    (1)Mapper.xml和mapper接口文件保持不变
    (2)Spring配置文件如下:

    <!-- mapper配置 
    MapperFactoryBean:根据mapper接口生成代理对象
    mapperInterface指定mapper接口
    -->
    <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
        <property name="sqlSessionFactory" ref="sqlSessionFactory"/>
        <property name="mapperInterface" value="com.jack.mapper.Mapper"/>
    </bean>
    

    (3)测试代码:

      public void testFindUserById1() throws  Exception{
            Mapper mapper = (Mapper) applicationContext.getBean("userMapper");
            User user = mapper.findUser(1);
            System.out.println(user);
        }
    

    (4)缺点:每一个mapper都要件bean配置,过于麻烦。

    2、通过MapperScannerConfigurer进行mapper扫描(建议使用)

    (1)Mapper.xml和mapper接口文件保持不变
    (2)如果将mapper.xml和mapper接口的名称保持一致且放在一个目录 则不用在sqlMapConfig.xml中进行配置。
    (3)Spring配置文件如下:

    <!-- mapper批量扫描,从mapper包中扫描出mapper接口,自动创建代理对象并且在spring容器中注册 
    遵循规范:将mapper.java和mapper.xml映射文件名称保持一致,且在一个目录 中
    自动扫描出来的mapper的bean的id为mapper类名(首字母小写)
    -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
    <!-- 指定扫描的包名 
    	如果扫描多个包,每个包中间使用半角逗号分隔
    	-->
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
        <property name="basePackage" value="com.jack.mapper"/>
    </bean>
    

    (4)测试代码:

      public void testFindUserById2() throws  Exception{
            Mapper mapper = (Mapper) applicationContext.getBean("mapper");
            User user = mapper.findUser(1);
            System.out.println(user);
        }
  • 相关阅读:
    spring guides
    Go 汇编入门
    go内存管理
    websocket
    Scheduling In Go
    log-structured-merge-trees
    InfluxDB 内部机制解析
    thread-states
    MySQL高可用方案
    Amazon Aurora解读
  • 原文地址:https://www.cnblogs.com/jack1995/p/7260730.html
Copyright © 2011-2022 走看看