• springMvc项目配置步骤


    spring部分:applicationContext.xml文件配置

    1.配置数据源 dataSource

    <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
      <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
      <property name="url" value="jdbc:mysql://localhost:3306/db_maven"/>
      <property name="username" value="root"/>
      <property name="password" value="oursql"/>
    </bean>

    2.配置sqlSessionFactory,
    2.1 引入dataSource,
    2.2 自动扫描mappers.xml文件,
    2.3 引入mybatis配置文件

    <!-- 配置mybatis的sqlSessionFactory -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
      <property name="dataSource" ref="dataSource" />
      <!-- 自动扫描mappers.xml文件 -->
      <property name="mapperLocations" value="classpath:com/java/user/mappers/*.xml"></property>
      <!-- mybatis配置文件 -->
      <property name="configLocation" value="classpath:mybatis-config.xml"></property>
    </bean>

    3.配置dao扫描器,扫描/mappers/*.xml对应的mapper接口类,需要引入sqlSessionFactory

    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
      <property name="basePackage" value="com.java.user.dao" />
      <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
    </bean>

    4.配置事务

    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager"  class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
      <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 配置事务通知属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
      <!-- 定义事务传播属性 -->
      <tx:attributes>
        <tx:method name="insert*" propagation="REQUIRED" />
        <tx:method name="update*" propagation="REQUIRED" />
        <tx:method name="edit*" propagation="REQUIRED" />
        <tx:method name="save*" propagation="REQUIRED" />
        <tx:method name="add*" propagation="REQUIRED" />
        <tx:method name="new*" propagation="REQUIRED" />
        <tx:method name="set*" propagation="REQUIRED" />
        <tx:method name="remove*" propagation="REQUIRED" />
        <tx:method name="delete*" propagation="REQUIRED" />
        <tx:method name="change*" propagation="REQUIRED" />
        <tx:method name="check*" propagation="REQUIRED" />
        <tx:method name="get*" propagation="REQUIRED" read-only="true" />
        <tx:method name="find*" propagation="REQUIRED" read-only="true" />
        <tx:method name="load*" propagation="REQUIRED" read-only="true" />
        <tx:method name="*" propagation="REQUIRED" read-only="true" />
      </tx:attributes>
    </tx:advice>

    <!-- 配置事务切面 -->
    <aop:config>
      <aop:pointcut id="serviceOperation"  expression="execution(* com.java.user.service.*.*(..))" />
      <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceOperation" />
    </aop:config>

    制定计划、物质驱动、立即执行、反复提醒、阶段反馈、输出博客
  • 相关阅读:
    Java 8实战之读书笔记五:超越Java 8
    Quartz的简单使用
    Quartz实现数据库动态配置定时任务
    Java解析Groovy和Shell的代码
    Spring学习笔记(3)——快速入门
    linux的防火墙端口配置
    气泡提示 纯CSS
    解决LINUX下SQLPLUS时上下左右键乱码问题
    redhat Enterprise Linux 6 VNC安装
    使用mount命令挂载CDROM
  • 原文地址:https://www.cnblogs.com/wxseng/p/9563429.html
Copyright © 2020-2023  润新知