• MyBatis3-与Spring 4集成


    继续使用前一篇的例子http://www.cnblogs.com/EasonJim/p/7052368.html,实际项目中,通常会用Spring来管理DataSource等。充分利用Spring基于接口的编程,以及AOP、IOC带来的方便。用Spring来管理MyBatis与管理Hibernate有很多类似的地方。

    下面是实际整合步骤,集成最简单的Spring功能:

    0、项目结构:

    1、POM引入相关依赖,配置如下:

            <!-- Spring Core -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-core -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-core</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- Spring Context -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-context -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- Spring JDBC -->
            <!-- http://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-jdbc</artifactId>
                <version>4.1.4.RELEASE</version>
            </dependency>
    
            <!-- mysql-connector-java -->
            <!-- http://mvnrepository.com/artifact/mysql/mysql-connector-java -->
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.38</version>
            </dependency>
    
            <!-- mybatis -->
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis</artifactId>
                <version>3.4.4</version>
            </dependency>
    
            <!-- mybatis-spring -->
            <!-- https://mvnrepository.com/artifact/org.mybatis/mybatis-spring -->
            <dependency>
                <groupId>org.mybatis</groupId>
                <artifactId>mybatis-spring</artifactId>
                <version>1.3.1</version>
            </dependency>
    
            <!-- commons-dbcp -->
            <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
            <dependency>
                <groupId>commons-dbcp</groupId>
                <artifactId>commons-dbcp</artifactId>
                <version>1.4</version>
            </dependency>

    2、配置beans.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"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <!--采用DBCP连接池 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://127.0.0.1:3306/Test?serverTimezone=UTC" />
            <property name="username" value="root" />
            <property name="password" value="root" />
        </bean>
        
        <!-- 新建SessionFactonry -->
        <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
            <!--dataSource属性指定要用到的连接池 -->
            <property name="dataSource" ref="dataSource" />
            <!--configLocation属性指定mybatis的核心配置文件 -->
            <property name="configLocation" value="Configuration.xml" />
        </bean>
    
        <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
            <!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例 -->
            <property name="sqlSessionFactory" ref="sqlSessionFactory" />
            <!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象 -->
            <property name="mapperInterface" value="com.jsoft.testmybatis.inter.IUserOperation" />
        </bean>
    
    </beans>

    org.mybatis.spring.SqlSessionFactoryBean与org.mybatis.spring.mapper.MapperFactoryBean实现了Spring的接口,并产生beans对象。

    3、去除之前在Configuration.xml配置的数据库连接信息,改成如下配置:

    <?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 alias="User" type="com.jsoft.testmybatis.models.User" />
            <typeAlias alias="Article" type="com.jsoft.testmybatis.models.Article" />
        </typeAliases>
    
        <mappers>
            <mapper resource="com/jsoft/testmybatis/models/User.xml" />
        </mappers>
        
    </configuration>

    4、最后是测试代码的核心部分,完全去除了MyBatis新建SessionFactory的部分,代码如下所示:

            ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
    
            IUserOperation mapper = (IUserOperation) ctx.getBean("userMapper");
            // 测试id=1的用户查询,根据数据库中的情况,可以改成你自己的.
            System.out.println("得到用户id=1的用户信息");
            User user = mapper.selectUserByID(1);
            System.out.println(user.getUserAddress());
    
            // 得到文章列表测试
            System.out.println("得到用户id为1的所有文章列表");
            List<Article> articles = mapper.getUserArticles(1);
    
            for (Article article : articles) {
                System.out.println(article.getContent() + "--" + article.getTitle());
            }

    5、测试结果:

    可以看出,效果和没有和Spring整合时基本一致。只是代码上简化了操作。

    测试工程:https://github.com/easonjim/5_java_example/tree/master/mybatis/test5

    参考:

    http://www.yihaomen.com/article/java/309.htm

  • 相关阅读:
    Python Socket 网络编程之粘包现象
    Socket 通信流程和 Python 网络编程基础
    WAF 技术原理
    Python 反射机制(自省)
    Python中创建对象的内部流程、metaclass和type类
    理解Python可迭代对象、迭代器、生成器
    Python 面向对象编程 总结
    python类的继承
    【macOS】关闭mac的Microsoft AutoUpdate
    tampermonkey修改页面音频播放地址
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7052388.html
Copyright © 2020-2023  润新知