<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"> <!-- 1.加載jdbc.properties文件的位置 --> <context:property-placeholder location="classpath:jdbc.properties"/> <!-- 2.配置druid連接池 ,id是固定值,class是druid連接池類的全路徑 --> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <!-- 配置連接數據庫的基本信息 --> <property name="driverClassName" value="${db.driverClassName}"></property> <property name="url" value="${db.url}"></property> <property name="username" value="${db.username}"></property> <property name="password" value="${db.password}"></property> </bean> <!-- 3.整合spring和mybatis框架 將SqlSession等對象的創建交給Spring容器 id值(sqlSessionFactory)是固定值 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 3.1.指定mybatis核心配置文件的位置 --> <property name="configLocation" value="classpath:mybatis/mybatis-config.xml"></property> <!-- 3.2.配置連接池(數據源) ref指向連接池bean對象的id值 --> <property name="dataSource" ref="dataSource"></property> <!-- 3.3、掃描所有的 XxxMapper.xml映射文件,讀取其中配置的SQL語句 --> <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/> </bean> <!-- 4、定義mapper接口掃描器 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 掃描所有XxxMapper接口,將接口實例的創建交給spring容器 --> <property name="basePackage" value="com.tsvv.dao"/> </bean> <!-- 5.配置需要掃描的包(service層):spring自動去掃描 base-package下的類, 如果掃描到的類上有 @Controller、@Service、@Component等注解, 將會自動將類注冊為bean(即由spring創建實例) --> <context:component-scan base-package="com.tsvv.service"> </context:component-scan> </beans>
jdbc.properties:
db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql:///yonghedb?characterEncoding=utf-8
db.username=root
db.password=root
