spring mybatis 框架集成之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:jdbc="http://www.springframework.org/schema/jdbc"  
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:jpa="http://www.springframework.org/schema/data/jpa"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

数据库连接池的配置:

    <!-- 获取数据库配置文件 -->
    <bean id="config" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location">
    		<value>classpath:db-config.properties</value>
    	</property>
    </bean>
    
    <!-- 获取数据源 -->
    <bean id="ds" class="org.apache.commons.dbcp.BasicDataSource">
    	<property name="driverClassName">
    		<value>${driverClass}</value>
    	</property>
    	<property name="url">
    		<value>${url}</value>
    	</property>
    	<property name="username">
    		<value>${username}</value>
    	</property>
    	<property name="password">
    		<value>${password}</value>
    	</property>
    </bean>

mybatis之SqlSessionFactory的配置:

如果mapper文件都在同一包下可以这样写:

<property name="mapperLocations" value="classpath:com/xxx/pojo/login/*.xml"/>

如果在不同的包下则如下写法。spring会自动加载并且按照dao的namespace寻找对应的mapper

    <!-- 配置SqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="ds" />
        <property name="mapperLocations">
        	<list>
				<value>classpath:com/xxx/pojo/login/*.xml</value>
				<value>classpath:com/xxx/pojo/loginInfo/*.xml</value> 	
        	</list>
        </property>
    </bean>

注解以及转发的配置

<!-- 配置MyBatis注解 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.diannuo.dao" />
        <property name="annotationClass" value="com.diannuo.annotation.MyBatisRepository" />
    </bean>
    
    <!-- 开启注解扫描 -->
    <context:component-scan base-package="com.diannuo"/>
    
    <!-- 开启requestMapping注解扫描 -->
    <mvc:annotation-driven/>
    
    <!-- 处理请求转发 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    	<property name="prefix" value="/WEB-INF"/><!-- jsp页面的存放目录 -->
    	<property name="suffix" value=".jsp"/><!-- 页面的后缀名 -->
    </bean>

 

转载于:https://my.oschina.net/MrBamboo/blog/776195

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值