常用配置方式转载

1.db.properties配置文件

    oracle.driver=oracle.jdbc.driver.OracleDriver
    oracle.url=jdbc:oracle:thin:@localhost:1521:ssm?useUnicode=true&characterEncoding=utf8
    oracle.username=root
    oracle.password=root
     
    mysql.driver=com.mysql.jdbc.Driver
    mysql.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=utf8

    #如url使用的是本地数据库且端口是3306,可以省略 localhost:3306

    # jdbc:mysql:///ssm?useUnicode=true&characterEncoding=utf8

    mysql.username=root
    mysql.password=root

2.log4j.properties

    log4j.rootCategory=info,stdout
    log4j.appender.stdout=org.apache.log4j.ConsoleAppender
    log4j.appender.Threshold=info
    log4j.appender.stdout.Target=System.out
    log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
    log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} [%t] [%c] [%p] - %m%n
    
    log4j.logger.org.springframework=WARN
    log4j.logger.java.sql=debug
    log4j.logger.java.sql.ResultSet=debug
    log4j.logger.java.sql.Connection=debug
    log4j.logger.org.apache.ibatis=INFO
    log4j.logger.com.beiyou.ws=INFO
    log4j.logger.com.beiyou.game=INFO
    log4j.logger.com.beiyou.log=INFO
 

3.applicationContext-mybatis.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: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.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd" default-autowire="byName">
            <!-- 1. 扫描包  @Service @Resource-->
            <context:component-scan base-package="com.yh.service.impl"></context:component-scan>
            <!-- 2. 加载属性文件 -->
            <context:property-placeholder location="classpath:db.properties"/>
            <!-- 3. 获取数据源,spring原始连接池 -->
             <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
                <property name="driverClassName" value="${mysql.driver}"></property>
                <property name="url" value="${mysql.url}"></property>
                <property name="username" value="${mysql.username}"></property>
                <property name="password" value="${mysql.password}"></property>
            </bean>
            <!-- 阿里巴巴druid连接池 -->
            <!-- <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
                <property name="driverClassName" value="${mysql.driver}"></property>
                <property name="url" value="${mysql.url}"></property>
                <property name="username" value="${mysql.username}"></property>
                <property name="password" value="${mysql.password}"></property>
            </bean> -->
            <!-- c3p0连接池 -->
            <!-- <bean id="dataSource" class="org.apache.tomcat.dbcp.dbcp2.BasicDataSource">
                   <property name="driverClassName" value="${mysql.driver}"></property>
                <property name="url" value="${mysql.url}"></property>
                <property name="username" value="${mysql.username}"></property>
                <property name="password" value="${mysql.password}"></property>
                </bean> -->
                <!--proxool连接池-->
             <!-- <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
                <property name="driver" value="${proxool.driver}"></property>
                <property name="driverUrl" value="${proxool.driverUrl}"></property>
                <property name="user" value="${proxool.user}"></property>
                <property name="password" value="${proxool.password}"></property>
                <property name="alias" value="${proxool.alias}" />
                <property name="houseKeepingSleepTime" value="${proxool.houseKeepingSleepTime}" />
                <property name="prototypeCount" value="${proxool.prototypeCount}" />
                <property name="maximumConnectionCount" value="${proxool.maximumConnectionCount}" />
                <property name="minimumConnectionCount" value="${proxool.minimumConnectionCount}" />
                <property name="simultaneousBuildThrottle" value="${proxool.simultaneousBuildThrottle}" />
                <property name="maximumConnectionLifetime" value="${proxool.maximumConnectionLifetime}" />
                <property name="houseKeepingTestSql" value="${proxool.houseKeepingTestSql}" />
        </bean> -->
            <!-- 4. SQLSessionFactory实例 -->
            <bean id="factory" class="org.mybatis.spring.SqlSessionFactoryBean">
                <property name="configLocation" value="classpath:mybatis.xml"></property>
            </bean>
            <!-- 5. Mapper扫描器 -->
            <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
                <property name="basePackage" value="com.yh.mapper"></property>
                <property name="sqlSessionFactoryBeanName" value="factory"></property>
            </bean>
            <!-- 6.事务管理器 -->
            <bean id="txManage" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"></bean>
            <!-- 7.声明式事务 -->
            <tx:advice id="txAdvice" transaction-manager="txManage">
                <tx:attributes>
                    <tx:method name="ins*"/>
                    <tx:method name="upd*"/>
                    <tx:method name="del*"/>
                    <tx:method name="*" read-only="true"/>
                </tx:attributes>
            </tx:advice>
            <!-- 8.配置aop -->
            <aop:config>
                <aop:pointcut expression="execution(* com.yh.service.impl.*.*(..))" id="mypoint"/>
                <aop:advisor advice-ref="txAdvice" pointcut-ref="mypoint"/>
            </aop:config>
    </beans>

4.mybatis.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE configuration
      PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
      "http://mybatis.org/dtd/mybatis-3-config.dtd">
    <configuration>
             <!-- 对象命名空间 -->  
        <typeAliases>  
             <package name="com.yh.pojo"/>
        </typeAliases>  
    </configuration>

5.springmvc.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/mvc
            http://www.springframework.org/schema/mvc/spring-mvc.xsd
            http://www.springframework.org/schema/context
            http://www.springframework.org/schema/context/spring-context.xsd">
       <!-- 1. 扫描控制器包,如果由Spring扫描Controller会导致父子容器问题,出现声明式事务无效,事务无法回滚. -->
       <context:component-scan base-package="com.yh.controller"></context:component-scan>
       <!-- 2. 注解驱动,让注解生效 -->
       <mvc:annotation-driven></mvc:annotation-driven>
       <!-- 3. 自定义视图解析器 -->
       <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
               <property name="prefix" value="/WEB-INF/page/"></property>
               <property name="suffix" value=".jsp"></property>
       </bean>
       <!-- Multipart解析器 -->
       <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
               <property name="defaultEncoding" value="utf-8"></property>
       </bean>
       <mvc:resources location="/WEB-INF/images/" mapping="/images/**"></mvc:resources>
       <mvc:resources location="/WEB-INF/css/" mapping="/css/**"></mvc:resources>
       <mvc:resources location="/WEB-INF/js/" mapping="/js/**"></mvc:resources>
    </beans>

6.所需jar包


jar包地址:链接: https://pan.baidu.com/s/1wM2AEGgCy8lCUZTkzdctoQ 密码: mfge


---------------------
作者:smilecattobelucky
来源:CSDN
原文:https://blog.csdn.net/springyh/article/details/80088187
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值