Smal1_Sun
Smal1_Sun
2016-03-23 09:17

关于springmvc+mybatis 注入问题

  • spring
  • mybatis

service:

 package com.yhq.service.biz;

import com.yhq.service.po.User;

public interface UserBiz {
    void insert(User u);
}

impl:

 package com.yhq.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yhq.service.api.UserMapper;
import com.yhq.service.biz.UserBiz;
import com.yhq.service.po.User;

@Service
public class UserBizImpl implements UserBiz {

    @Autowired
    private UserMapper userMapper;

    public void insert(User u) {
        // TODO Auto-generated method stub
        userMapper.insert(u);
    }
}

database.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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <aop:aspectj-autoproxy/>
    <!-- 数据源配置,使用应用内的DBCP数据库连接池  -->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <property name="driverClassName"><value>${jdbc.driverClassName}</value></property>
        <property name="url"><value>${jdbc.url}</value></property>
        <property name="username"><value>${jdbc.username}</value></property>
        <property name="password"><value>${jdbc.password}</value></property>
        <property name="maxActive"><value>${jdbc.maxActive}</value></property>
        <property name="maxWait"><value>${jdbc.maxWait}</value></property>
        <property name="maxIdle"><value>${jdbc.maxIdle}</value></property>
        <property name="initialSize"><value>${jdbc.initSize}</value></property>
        <property name="removeAbandoned"><value>true</value></property>
        <property name="testWhileIdle"><value>true</value></property>
        <property name="testOnBorrow"><value>false</value></property>
        <property name="validationQuery"><value>SELECT 1</value></property>
    </bean>

    <!-- Transaction manager for a single JDBC DataSource -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="configLocation" value="classpath:sqlmap-config.xml" />
        <property name="mapperLocations">
            <value>classpath*:com/yhq/service/map/*Mapper.xml</value>
        </property>
    </bean>

    <bean id="sqlSession"  class="org.mybatis.spring.SqlSessionTemplate" scope="prototype">
        <constructor-arg index="0" ref="sqlSessionFactory" />
    </bean>

    <!-- 连接事务的注解配置 -->
    <tx:annotation-driven transaction-manager="transactionManager" />

    <bean id="txInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
        <property name="transactionManager">
            <ref bean="transactionManager"/>
        </property>
        <property name="transactionAttributes">
            <props>
                <prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop>
                <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>

            </props>
        </property>
    </bean>

    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="sqlSessionTemplateBeanName" value="sqlSession"/>
        <property name="basePackage">
            <value>
                 com.yhq.service.api
            </value>
        </property>
    </bean>

    <bean id="aspectJExpressionPointcutAdvisor" class="org.springframework.aop.aspectj.AspectJExpressionPointcutAdvisor">
        <property name="advice">
            <ref bean="txInterceptor" />
        </property>
        <property name="expression">
            <value><![CDATA[execution(* com.yhq..*.*BizImpl.*(..)) && @within(org.springframework.stereotype.Service) &&
            (execution(* create*(..)) || execution(* delete*(..)) || execution(* update*(..)) || execution(* save*(..)))]]></value>
        </property>
    </bean>
</beans>

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:mvc="http://www.springframework.org/schema/mvc"
       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-3.0.xsd">

       <mvc:annotation-driven/>
       <context:component-scan base-package="com.yhq">
       </context:component-scan>

       <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
              <property name="order" value="0" />
              <property name="cache" value="true" />
              <property name="prefix" value=""/>
              <property name="suffix" value=".html"/>
              <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView" />
              <property name="exposeSpringMacroHelpers" value="true" />
              <property name="exposeRequestAttributes" value="true" />
              <property name="exposeSessionAttributes" value="true" />
              <property name="requestContextAttribute" value="rc" />
              <property name="contentType" value="text/html;charset=UTF-8" />
       </bean>

       <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
              <property name="templateLoaderPath" value="/WEB-INF/pages/" />
              <property name="defaultEncoding" value="UTF-8" />
              <property name="freemarkerSettings">
                     <props>
                            <prop key="template_update_delay">0</prop>
                            <prop key="locale">zh_CN</prop>
                            <prop key="default_encoding">UTF-8</prop>
                            <prop key="number_format">0.##########</prop>
                            <prop key="datetime_format">yyyy-MM-dd HH:mm:ss</prop>
                            <prop key="classic_compatible">true</prop>
                            <prop key="template_exception_handler">ignore</prop>
                     </props>
              </property>
       </bean>
</beans> 
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答