头部:
<?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>