第一个SSM框架之配置文件

文件目录

  • resource
    • mybatis
      • db.properties
      • sqlMapConfig.xml
    • spring
      • spring-dao.xml
      • spring-service.xml
      • springmvc.xml
    • log4j.properties

代码

mybatis
  • db.properties
driverClass=com.mysql.jdbc.Driver
user=root
password=root
jdbcUrl=jdbc:mysql://localhost:3306/ssm-pro
  • sqlMapConfig.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">
<!--MyBatis的核心配置文件-->
<configuration>
	<settings>
         <!--配置log4j日志-->
		<setting name="logImpl" value="LOG4J"/>
	</settings>
<typeAliases>
    <!--类型别名可为 Java 类型设置一个缩写名字。 它仅用于 XML 配置,意在降低冗余的全限定类名书写。-->
	<!-- <typeAlias type="com.entity.User" alias="user"/> -->
	<!-- 批量定义别名    别名就是类名 -->
    <!--也可以指定一个包名,MyBatis 会在包名下面搜索需要的 Java Bean,比如:-->
	<package name="com.entity"/>
</typeAliases>
</configuration>
spring
  • spring-dao
<?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"

       xsi:schemaLocation="
       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--读取数据库配置文件-->
    <!--
	<context:property-placeholder>的作用是向Spring容器中注入一个属性占位解析器,用来处理BeanDefinition中的各种占位符,用配置文件的信息替换占位符。这是个自定义标签,Spring会使用PropertyPlaceholderBeanDefinitionParser解析它。
	-->
    <context:property-placeholder location="classpath:mybatis/db.properties"></context:property-placeholder>

    <!--配置数据源-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${driverClass}"></property>
        <property name="user" value="${user}"></property>
        <property name="password" value="${password}"></property>
        <property name="jdbcUrl" value="${jdbcUrl}"></property>
    </bean>


    <!--配置SqlSessionFactory-->
    <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!--指定核心配置文件-->
        <property name="configLocation" value="classpath:mybatis/sqlMapConfig.xml"></property>
        <!--配置数据源-->
        <property name="dataSource" ref="dataSource"></property>
        
        <!--配置分页插件-->
        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageInterceptor">
                    <property name="properties">
                        <!--使用下面的方式配置参数,一行配置一个-->
                        <value>
                            <!--方言-->
                            helperDialect=mysql
                            reasonable=true
                        </value>
                    </property>
                </bean>
            </array>
        </property>
    </bean>
    <!--配置Mapper扫描-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.dao"></property>
    </bean>
</beans>
  • spring-service
<?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/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd

       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!--组件扫描  service-->
    <context:component-scan base-package="com.service"></context:component-scan>

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

    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>

            <tx:method name="delete*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>


            <tx:method name="update*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/>

            <tx:method name="find*" isolation="REPEATABLE_READ" read-only="true" propagation="REQUIRED"/>


        </tx:attributes>
    </tx:advice>

    <!--将通知织入目标对象-->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(* com.service.*ServiceImpl.*(..))"/>

        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc"></aop:advisor>

    </aop:config>
</beans>
  • 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">

    <!--配置扫描controller包-->
    <context:component-scan base-package="com.controller"></context:component-scan>

    <!--注解映射器和适配器   开启注解驱动  -->
    <mvc:annotation-driven conversion-service="conversionService"></mvc:annotation-driven>

    <mvc:view-resolvers>
        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <!--/WEB-INF/jsp/product_list.jsp-->
            <property name="prefix" value="/pages/"></property>
            <property name="suffix" value=".jsp"></property>
        </bean>
    </mvc:view-resolvers>


    <!--配置转换器-->
    <bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
        <property name="converters">
            <set>
                <bean class="com.conveter.MyDateConverter"></bean>
            </set>
        </property>
    </bean>

    <!--配置异常处理器-->
    <bean class="com.exception.MyHandlerException"></bean>

    <!--配置上传解析器                     org.springframework.web.multipart.commons.CommonsMultipartResolver-->
    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="10000000"></property>
        <!-- 设定默认编码 -->
        <property name="defaultEncoding" value="UTF-8"></property>
    </bean>

    <!--放行静态资源-->
    <mvc:resources location="/img/" mapping="/img/**"/>
    <mvc:resources location="/plugins/" mapping="/plugins/**"/>
    <mvc:resources location="/css/" mapping="/css/**"/>

</beans>

log4j.properties

###\u5C06\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u63A7\u5236\u53F0###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m %n
###\u628A\u65E5\u5FD7\u4FE1\u606F\u8F93\u51FA\u5230\u6587\u4EF6\u4E2D###
log4j.appender.file=org.apache.log4j.FileAppender
log4j.appender.file.File=sysInfo.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %m %n
###\u8BBE\u7F6E\u65E5\u5FD7\u7684\u4F18\u5148\u7EA7\u522B###
log4j.rootLogger=debug,stdout,file

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值