1 采用外部配置文件,连接数据库jdbc.properties
jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.1.102:1521:orcl
jdbc.username=scott
jdbc.password=tiger
2 配置数据库.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:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 开启包扫描 -->
<context:component-scan base-package="com.service.impl"></context:component-scan>
<!-- 扫描加载jdbc配置文件 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:jdbc.properties"></property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 配置SqlSessionFactory -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
<!-- 扫描mapper文件 -->
<property name="mapperLocations" value="classpath:com/aaa/ssm/mapper/*.xml"></property>
</bean>
<!-- 开启dao接口扫描 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 扫描接口包路径,不需要加classpath,如果多个可以用“,”隔开 -->
<property name="basePackage" value="com.aaa.ssm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务扫描 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
注意,开启事务扫描的代码,注意需要借助于tx命名空间,<tx:annotation-driven />用于支持事务注解,transaction-manager属性用于指定使用哪个事务管理器。
<!-- 配置事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!-- 加载数据源 -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事务扫描 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
事务针对的是
com.service.impl包进行扫描,也就是业务层的实现类扫描,负责管理业务逻辑组件里的业务逻辑方法,只有对业务逻辑方法添加事务管理才有实际意义。对于单个DAO方法,CRUD
增加事务管理,意义不大。
业务层代码
/**
* 部门添加
*/
@Transactional
public void add(Dept dept) {
// TODO Auto-generated method stub
dao.add(dept);
System.out.println(1/0);
}
针对1/0除零错,如果不增加事务,dao.add(dept)增加的数据可以插入到数据库,而如果添加了事务处理@Transactional,那么只要报错,则数据不会增加到数据库。主要用在级联更新 或防止操作出异常的情况下。
404页面处理,在web.xml文件里面,增加代码如下:
<!-- 5.jsp配置 -->
<error-page>
<error-code>404</error-code>
<location>/404.jsp</location>
</error-page>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
在项目里面,增加404.jsp页面即可,这样针对系统中出现了404问题的,可以定向跑到404页面,增加界面友好。
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有