当前位置: 代码迷 >> J2EE >> 如何样让springMVC里的Controller干,service,dao的事
  详细解决方案

如何样让springMVC里的Controller干,service,dao的事

热度:33   发布时间:2016-04-17 23:39:57.0
怎么样让springMVC里的Controller干,service,dao的事?
纠结了好多天,

我现在有一个 Controller 我让他继承了 SqlSessionDaoSupport,

然后在Controller 中直接 用

this.getSqlSession().delete("com.ssmm.dao.UserMapper.deleteByPrimaryKey", xxx);

去访问数据库,可是现在问题来。。。。

学。。


额。。。 怎么样把事务也放到 Controller 中?? 
现在spring事务放倒service 层是生效的,放到Controller中就没有效果。

求各位大牛指导一下。。。。
下面是我的配置spring.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:aop="http://www.springframework.org/schema/aop" 
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.2.xsd
http://www.springframework.org/schema/aop 
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
">

<!-- 自动扫描(自动注入) -->
<context:component-scan base-package="com.ssmm.service" />


<context:component-scan base-package="com.ssmm" use-default-filters="false">
<context:include-filter type="regex" expression="com.ssmm.(service|dao).*"/>
</context:component-scan>

<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config.properties" />

<!-- JNDI方式配置数据源 -->
<!-- <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="${jndiName}"></property> </bean> -->

<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" />

    <property name="filters" value="config" />
        <property name="connectionProperties" value="config.decrypt=true" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />

<property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />

<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />

<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />

<!-- 监控数据库 -->
<!-- <property name="filters" value="stat" /> -->
<!-- <property name="filters" value="mergeStat" /> -->
</bean>

<!-- myBatis文件 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:spring-mybatis.xml" />  
<property name="mapperLocations" value="classpath:com/ssmm/mapping/*.xml" />
</bean>

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.ssmm.dao" />
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
</bean>

<!-- 配置事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>


<!-- 注解方式配置事物 -->
<tx:annotation-driven transaction-manager="transactionManager" />

<!-- 拦截器方式配置事物 -->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="show*" propagation="REQUIRED" />
  相关解决方案