spring集成Mybatis

2019-03-05  本文已影响1人  黑小白少爷

内容说明

本文介绍了spring如何集成mybatis,使用的是mysql数据库,依赖工具为gradle

  • 环境准备
  • 集成流程
  • 测试

本文demo地址:https://github.com/wheijxiaotbai/HXB_Knowledge.git

分支: hxb_demo_springMybatis

环境准备

[图片上传中...(1551744767252.png-d600-1551746819338-0)]

集成流程

代码目录结构
1551744767252.png
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       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-4.0.xsd
   http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
   http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

    <!-- 导入数据库连接数据文件 -->
    <context:property-placeholder location="classpath:db.properties"/>

    <!-- 数据库连接池 -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
        <property name="maxActive" value="10" />
        <property name="maxIdle" value="5" />
    </bean>

    <!-- Mybatis的工厂 -->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <!-- 核心配置文件的位置 -->
        <property name="configLocation" value="mybatisxml/mybatis-config.xml"/>
    </bean>

    <!--&lt;!&ndash; Dao原始Dao &ndash;&gt;-->
    <!--<bean id="userDa o" class="com.itheima.mybatis.dao.UserDaoImpl">-->
        <!--<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>-->
    <!--</bean>-->
    <!--&lt;!&ndash; Mapper动态代理开发 &ndash;&gt;-->
    <!--<bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">-->
        <!--<property name="sqlSessionFactory" ref="sqlSessionFactoryBean"/>-->
        <!--<property name="mapperInterface" value="user.mapper.UserMapper"/>-->
    <!--</bean>-->

    <!-- Mapper动态代理开发扫描 这之前是通过自己手动获得实现类的,现在通过spring帮我们实现-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 基本包 -->
        <property name="basePackage" value="user.mapper"/>
    </bean>

</beans>

测试

编写测试类

springMybatisTest
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import user.mapper.UserMapper;
import user.model.User;

public class springMybatisTest {

    @Test
    public void testMapper() throws Exception {

        ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserMapper mapper = ac.getBean(UserMapper.class);
        User user = mapper.findUserById(1);
        System.out.print(user.getName());
    }

}
上一篇 下一篇

猜你喜欢

热点阅读