mybatis整合spring案例

本文介绍了一个包含Spring与MyBatis整合的配置案例,详细展示了如何通过XML配置文件设置数据库连接池、SqlSessionFactory及Mapper扫描等关键步骤。

项目共四个文件夹其中src,config,test三个源文件夹一个lib文件夹

   ApplicationContext.xml

    配置如下: <?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>
    
    <!-- 整合会话工厂归spring管理 -->
    
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 指定mybatis核心配置文件 -->
        <property name="configLocation" value="classpath:SqlMapConfig.xml"/>
        <!-- 指定会话工厂使用的数据源 -->
        <property name="dataSource" ref="dataSource"/>
    </bean>
    
    <!-- 使用包扫描方式批量引入mapper -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 指定要扫描的包的全路径名称 -->
        <property name="basePackage" value="com.xhe.mapper"/>
    </bean>
</beans>

SqlMapConfig.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>
        <!-- 
            使用包扫描的方式批量定义别名 
            定以后别名等于类名,不区分大小写,但是建议按照java命名规则来,首字母小写,以后每个单词的首字母大写
        -->
        <package name="com.xhe.pojo"/>
    </typeAliases>
    
    <!-- 由spring管理引入mapper -->
    <mappers>
        <!-- <mapper resource="User.xml"/>
        <mapper class="com.xhe.mapper.UserMapper"/> -->
        
        <!-- 
        使用class属性引入接口的全路径名称:
        使用规则:
            1. 接口的名称和映射文件名称除扩展名外要完全相同
            2. 接口和映射文件要放在同一个目录下
         -->
        
        
        <!-- 使用包扫描的方式批量引入Mapper接口 
                使用规则:
                1. 接口的名称和映射文件名称除扩展名外要完全相同
                2. 接口和映射文件要放在同一个目录下
        -->
        <!--  <package name="cn.xhe.mapper"/>-->
    </mappers>
</configuration>

 

注意:使用mybatis会自动根据接口生成实现类  

转载于:https://my.oschina.net/xhe2016/blog/835676

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值