做了一个屏蔽进数据库的操作:
Applicaition.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd"> <!-- 配置数据源,记得去掉myBatis-config.xml的数据源相关配置 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="com.mysql.jdbc.Driver" /> <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis2?useUnicode=true&characterEncoding=UTF-8" /> <property name="user" value="root" /> <property name="password" value="123456" /> </bean> <!-- 配置session工厂 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:mybatis-config.xml" /> </bean> <!-- 配置事务管理器,管理数据源事务处理 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> </bean> <!-- 配置事务通知 --> <tx:advice id="advice" transaction-manager="transactionManager"> <tx:attributes> <!-- 默认只处理运行时异常,可加rollback-for="Exception/Throwable"等处理所有异常或包括错误 --> <tx:method name="insert*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="update*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="delete*" propagation="REQUIRED" rollback-for="Exception" /> <tx:method name="*" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- 配置SessionTemplate,已封装了繁琐的数据操作 --> <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate" scope="prototype"> <constructor-arg name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean> <!-- 配置切面织入的范围,后边要把事务边界定在service层 --> <aop:config> <aop:advisor advice-ref="advice" pointcut="execution(* com.liuyang.crm.service.Imp.*.*(..))" /> </aop:config> <context:component-scan base-package="com.liuyang" /> <!--context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan --> </beans>
防止错误数据进入的屏蔽
<context:component-scan base-package="com.liuyang" /> <!--context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" /> </context:component-scan -->
这里其实注销掉,也是好使的.加上了就出现文章上边的找不到url地址注入的错误.
spring-mvc.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:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" 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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd "> <!-- 同时开启json格式的支持 --> <mvc:annotation-driven></mvc:annotation-driven> <context:component-scan base-package="com.liuyang"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan> </beans>
这里配置屏蔽就可以,这样数据库同样不进错误数据.
<context:component-scan base-package="com.liuyang"> <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" /> <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service" /> </context:component-scan>
@Override public int insertDept(Dept dept) throws Exception { int i = 0; i = deptDao.insertDept(dept); Integer.parseInt("aa"); return i; }
希望对您有帮助,感谢您的观看.