spring与mybatis整合

整合配置文件

实质就是将mybatis的配置文件SqlSessionConfiguration.xml的配置信息整合到spring的核心配置文件applicationContext.xml中

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://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--扫描包注解 : 不需要手动的在xml中进行配置类 而通过注解即可 spring在扫描包的时候 忽略 controller层-->
    <context:component-scan base-package="com.imust">
        <!--exclude 不包含-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
    </context:component-scan>

    <!--spring的配置文件整合mybatis的配置文件-->
    <!--配置sqlsession工厂-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--注入数据源信息-->
        <property name="dataSource" ref="dataSource"></property>
        <!--注入mapper文件-->
        <property name="mapperLocations" value="classpath:com/imust/dao/*.xml"></property>
        <!--注入实体类-->
        <property name="typeAliasesPackage" value="com.imust.domain"></property>
    </bean>
    <!--数据源的配置-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}"></property>
        <property name="url" value="${jdbc.url}"></property>
        <property name="username" value="${jdbc.username}"></property>
        <property name="password" value="${jdbc.password}"></property>
    </bean>
    <!--引入数据库信息db.properties-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--需要dao创建代理对象 MapperScannerConfigurer 给dao生成代理对象-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.imust.dao"></property>
    </bean>
</beans>

修改UserServiceImpl.java

package com.imust.service.impl;

import com.imust.dao.UserDao;
import com.imust.domain.User;
import com.imust.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    public List<User> findUserList(){
      List<User> userList = userDao.findUserList();

        return userList;
    }
}

修改TestDemoSpring.java

package com.imust;

import com.imust.domain.User;
import com.imust.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

/**
 * 1.加载配置文件
 * 2.注入userService
 */
@RunWith(SpringJUnit4ClassRunner.class)// 引用spring测试类
@ContextConfiguration(locations = "classpath:applicationContext.xml") //加载配置文件
public class TestDemoSpring {

    @Autowired
    private UserService userService;
    @Test
    public void testSpring(){
      List<User> usersList = userService.findUserList();//调用service层的代码
        for (User user : usersList) {//调用service的方法返回
            System.err.println(user);
        }
//    userService.findUserList();
    }
}

运行结果

posted @ 2020-03-15 17:37  搞点薯条  阅读(223)  评论(0编辑  收藏  举报