<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean>
@Override
public SqlSessionFactory getObject() throws Exception {
if (this.sqlSessionFactory == null) {
afterPropertiesSet();
}
return this.sqlSessionFactory;
}
@Override
public void afterPropertiesSet() throws Exception {
notNull(dataSource, "Property 'dataSource' is required");
notNull(sqlSessionFactoryBuilder, "Property 'sqlSessionFactoryBuilder' is required");
state((configuration == null && configLocation == null) || !(configuration != null && configLocation != null),
"Property 'configuration' and 'configLocation' can not specified with together");
this.sqlSessionFactory = buildSqlSessionFactory();
}
compile group: 'com.alibaba', name: 'druid', version: '1.1.2'
<!--阿里巴巴的数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_user}"/>
<property name="password" value="${jdbc_password}"/>
</bean>
jdbc_url=jdbc:mysql://localhost:3306/cnblogs?serverTimezone=Asia/Shanghai&characterEncoding=utf8 jdbc_user=root jdbc_password=root
<!--加载配置文件路径--> <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="classpath:properties/datasource.properties"></property> </bean> <!--获取配置属性之后动态替换标签--> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"/> </bean>
<!--采用自动扫描方式创建Mapper Bean--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.cnblogs.dao"></property> </bean>
compile group: 'org.springframework', name: 'spring-tx', version: '4.3.10.RELEASE' compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.10.RELEASE' <!--事务管理器--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!--使用声明式事务管理方式--> <tx:annotation-driven transaction-manager="transactionManager"/>
compile group: 'org.springframework', name: 'spring-webmvc', version: '4.3.10.RELEASE' compile group: 'org.springframework', name: 'spring-tx', version: '4.3.10.RELEASE' compile group: 'org.springframework', name: 'spring-jdbc', version: '4.3.10.RELEASE' compile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.1.0' compile group: 'org.mybatis', name: 'mybatis-spring', version: '1.3.1' compile group: 'org.mybatis', name: 'mybatis', version: '3.4.5' compile group: 'mysql', name: 'mysql-connector-java', version: '6.0.6' compile group: 'com.alibaba', name: 'druid', version: '1.1.2'
<?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:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!--spring可以自动去扫描base-pack下面的包或者子包下面的Java文件-->
<context:component-scan base-package="com.cnblogs.controller,com.cnblogs.service,com.cnblogs.dao"/>
<!--自动注册RequestMappingHandlerMapping与RequestMappingHandlerAdpter-->
<mvc:annotation-driven/>
<!-- 对静态资源文件的访问-->
<mvc:default-servlet-handler/>
<!--加载配置文件路径-->
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="location" value="classpath:properties/datasource.properties"></property>
</bean>
<!--获取配置属性之后动态替换标签-->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
<property name="properties" ref="configProperties"/>
</bean>
<!--阿里巴巴的数据库连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}"/>
<property name="username" value="${jdbc_user}"/>
<property name="password" value="${jdbc_password}"/>
</bean>
<!--构建SqlSessionFactory-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:config/mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:mappers/*.xml"/>
</bean>
<!--采用自动扫描方式创建Mapper Bean-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.cnblogs.dao"></property>
</bean>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!--使用声明式事务管理方式-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/content"></property>
<property name="suffix" value=".jsp"></property>
</bean>
</beans>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有