美文网首页
Java实训(7)-- MyBatis 与 Spring 整合

Java实训(7)-- MyBatis 与 Spring 整合

作者: 空心老竹 | 来源:发表于2018-06-13 19:17 被阅读0次

    整合步骤

    1. 导入spring 和 mybatis 的包

    2. 编写配置文件

    3. 编写Service层测试接口

    4. 编写测试类进行整合测试

    1. 导入需要的包

      mybatis-3.4.1.jar
      mysql-connector-java-5.1.31-bin.jar
      log4j-core-2.11.0.jar
      log4j-api-2.11.0.jar
      aopalliance-1.0.jar
      aspectjweaver-1.8.0.M1.jar
      commons-logging-1.1.3.jar
      spring-aop-4.1.7.RELEASE.jar
      spring-aspects-4.1.7.RELEASE.jar
      spring-beans-4.1.7.RELEASE.jar
      spring-context-4.1.7.RELEASE.jar
      spring-core-4.1.7.RELEASE.jar
      spring-expression-4.1.7.RELEASE.jar
      mybatis-spring-1.3.0.jar
      commons-dbcp2-2.1.1.jar
      commons-pool2-2.4.2.jar
      spring-orm-4.1.7.RELEASE.jar
      spring-tx-4.1.7.RELEASE.jar
      spring-jdbc-4.1.7.RELEASE.jar
      
    2. 配置文件编写

      1. 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:aop="http://www.springframework.org/schema/aop"
      xmlns:context="http://www.springframework.org/schema/context" 
      xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/aop
      http://www.springframework.org/schema/aop/spring-aop.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd">
      
          <!-- 整合spring+mybatis时,使用了注解配置组价扫描器,使用spring加载/配置注解代码 -->
          <context:component-scan base-package="com.neuedu.bookstore.service">
          </context:component-scan>
      
          <!-- 整合数据库连接池,后续需要由spring进行数据库事务管理,
          故需要有spring统一管理数据库连接 -->
          <context:property-placeholder location="classpath:db_config.properties"/>
          <bean id="dataSource" class="org.apache.commons.dbcp2.BasicDataSource">
              <property name="driverClassName" value="${driver}"/>
              <property name="url" value="${url}"/>
              <property name="username" value="${dbusername}"/>
              <property name="password" value="${password}"/>
          </bean>
      
          <!-- 整合 spring + mybatis 的数据源 -->
          <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
              <!-- MyBatis 改用spring提供的数据源 -->
              <property name="dataSource" ref="dataSource"></property>
              <!-- 引用mybatis全局配置文件 ,即由spring加载mybatis运行环境-->
              <property name="configLocation" value="classpath:mybatis-config.xml"></property>
          </bean>
      
          <!-- mapper接口是mybatis的组件,需要由mybatis提供的整合类进行扫描 -->
          <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
              <property  name="basePackage" value="com.neuedu.bookstore.mapper"></property>
          </bean>
      </beans>
      
      1. mybatis-config.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>
          <package name="com.neuedu.bookstore.bean"/>
        </typeAliases>    
      
        <mappers>
          <package name="com.neuedu.bookstore.mapper"/>
        </mappers>
      </configuration>
      
      1. db_config.properties
      driver=com.mysql.jdbc.Driver
      url=jdbc:mysql://localhost:3306/ssmdb
      username=javauser
      password=123456
      
    3. 创建一个 IBookService.java 接口文件

      package com.neuedu.bookstore.service;
      
      import java.util.List;
      
      import com.neuedu.bookstore.bean.Book;
      
      public interface IBookService {
          public List<Book> findAllBooks();
      }
      
    4. 实现 IBookService.java 接口 BookServiceImpl.java

      package com.neuedu.bookstore.service.impl;
      
      import java.util.List;
      
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.stereotype.Service;
      import org.springframework.transaction.annotation.Transactional;
      
      import com.neuedu.bookstore.bean.Book;
      import com.neuedu.bookstore.mapper.BookMapper;
      import com.neuedu.bookstore.service.IBookService;
      
      /**
       * 在业务层进行spring+mybatis的代码整合,在业务层调用mybatis进行数据库操作,同时需要进行业务管理
       * @author admin
       */
      @Service
      @Transactional
      public class BookServiceImpl implements IBookService{
      
          @Autowired
          private BookMapper bookMapper;
      
          @Override
          public List<Book> findAllBooks() {
              Book example = new Book();
              List<Book> list = bookMapper.findByExample(example);
              return list;
          }
      }
      
    5. 创建一个测试类 TestBookService.java,运行测试类,正常显示数据即整合完成

      package test;
      
      import java.util.List;
      
      import org.apache.logging.log4j.LogManager;
      import org.apache.logging.log4j.Logger;
      import org.junit.Test;
      import org.junit.Before;
      import org.springframework.context.ApplicationContext;
      import org.springframework.context.support.ClassPathXmlApplicationContext;
      
      import com.neuedu.bookstore.bean.Book;
      import com.neuedu.bookstore.bean.Category;
      import com.neuedu.bookstore.service.IBookService;
      
      public class TestBookService{
          
          private Logger logger = LogManager.getLogger(TestBookService.class);
      
          protected ApplicationContext context;
          protected IBookService bookService;
          
          @Before
          public void init() {
              //初始化spring运行环境
              try {
                  String configLocation = "applicationContext.xml";
                  context = new ClassPathXmlApplicationContext(configLocation);
                  bookService = context.getBean(IBookService.class);
              } catch (Exception e) { 
                  e.printStackTrace();
              }
          }
      
          @Test
          public void testFindAllBooks() {
              List<Book> books = bookService.findAllBooks();
              for(Book book :books) {
                  logger.info(book);
              }
          }
      }
      

    相关文章

      网友评论

          本文标题:Java实训(7)-- MyBatis 与 Spring 整合

          本文链接:https://www.haomeiwen.com/subject/hsrseftx.html