原创

JavaWeb之SSM框架配置文件篇

0. 引言

本文致力于解决框架配置文件编写繁琐的问题,故将每个框架所用到的配置文件汇总到此博客中,希望能提升从业者的开发效率。

1. Spring

  • Maven坐标(包含SpringMVC的包)pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 <modelVersion>4.0.0</modelVersion>

 <groupId>com.wz</groupId>
 <artifactId>code</artifactId>
 <version>1.0-SNAPSHOT</version>
 <packaging>war</packaging>

 <name>code Maven Webapp</name>
 <!-- FIXME change it to the project's website -->
 <url>http://www.example.com</url>

 <properties>
   <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
   <maven.compiler.source>1.8</maven.compiler.source>
   <maven.compiler.target>1.8</maven.compiler.target>
   <!-- Spring版本控制 -->
   <spring-version>4.3.7.RELEASE</spring-version>
 </properties>


 <dependencies>
   <!-- Spring核心模块 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-beans</artifactId>
     <version>${spring-version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context</artifactId>
     <version>${spring-version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-context-support</artifactId>
     <version>${spring-version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-core</artifactId>
     <version>${spring-version}</version>
   </dependency>
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-expression</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- 可选模块,按需添加 -->
   <!-- AOP -->
   <!-- 基于代理的AOP支持 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aop</artifactId>
     <version>${spring-version}</version>
   </dependency>
   <!-- 提供与AspectJ的集成 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-aspects</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- 提供一些类级工具支持和类加载器实现,在以在某些应用服务器中使用 -->
   <!-- 用于JVM引导的检测代理 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-instrument</artifactId>
     <version>${spring-version}</version>
   </dependency>
   <!-- tomcat的instrument实现 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-instrument-tomcat</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- JDBC支持包,包括数据源设置和JDBC访问支持 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-jdbc</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- JMS支持包,包括用于发送/接收JMS消息的帮助程序类 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-jms</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- 支持消息体系结构和协议 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-messaging</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- 集成ORM框架如Hibernate、JPA等 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-orm</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- Object/XML映射 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-oxm</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- 支持单元测试和集成测试Spring组件-->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-test</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- Spring事务 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-tx</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- 提供基本的面向Web的集成功能 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-web</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- SpringMVC -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- Portlet环境中使用的MVC实现 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-webmvc-portlet</artifactId>
     <version>${spring-version}</version>
   </dependency>

   <!-- WebSocket和SockJS基础架构,包括STOMP消息传递支持 -->
   <dependency>
     <groupId>org.springframework</groupId>
     <artifactId>spring-websocket</artifactId>
     <version>${spring-version}</version>
   </dependency>
 </dependencies>
</project>
  • SpringConfig.xml 命名空间
<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"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd">
  • Spring AOP的XML配置
<aop:config>
        <aop:aspect id="logAdvice" ref="logger">
            <aop:pointcut id="pt" expression="execution(* com.wz.service.Impl.*.*(..))"/>
            <aop:before method="qianzhi" pointcut-ref="pt"></aop:before>
            <aop:after-returning method="houzhi" pointcut-ref="pt"></aop:after-returning>
            <aop:after-throwing method="yichang" pointcut-ref="pt"></aop:after-throwing>
            <aop:after method="zuizhong" pointcut-ref="pt"></aop:after>
            <aop:around method="huanrao" pointcut-ref="pt"></aop:around>
        </aop:aspect>
</aop:config>

2. SpringMVC

  • SpringMVC配置文件 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:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:contex="http://www.springframework.org/schema/context"
       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/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc.xsd
        ">
    <!--开启注解扫描-->
    <contex:component-scan base-package="com.wz"></contex:component-scan>

    <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>
    <!--开启springmvc注解支持-->
    <mvc:annotation-driven/>
</beans>

3. Mybaties

  • Maven坐标 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>mybaties</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.5</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.45</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.12</version>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>
  • SqlMapperConfig
<?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>
    <properties resource="jdbcConfig.properties"></properties>
    <environments default="mysql">
        <environment id="mysql">
            <transactionManager type="JDBC"></transactionManager>
            <dataSource type="pooled">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="com/wz/dao/IUserDao.xml"></mapper>
        <mapper resource="com/wz/dao/IAccountDao.xml"></mapper>
    </mappers>
</configuration>
  • jdbcConfig.properties
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db?useSSL=False
username=root
password=123456
  • Dao层映射文件命名空间及相关语法
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.wz.dao.IUserDao">
    <sql id="defaultUser">
        select * from USER
    </sql>
    <select id="findAll" resultType="com.wz.domain.User">
        select * from USER;
    </select>
    <select id="saveUser" parameterType="com.wz.domain.User">
        insert into USER (name,birthday,sex,address) values(#{name},#{birthday},#{sex},#{address});
    </select>

    <update id="updateUser" parameterType="com.wz.domain.User">
        update USER set name=#{name},birthday=#{birthday},sex=#{sex},address=#{address} where id=#{id};
    </update>

    <delete id="deleteUser" parameterType="Integer">
        delete from USER where id=#{id};
    </delete>

    <select id="findById" parameterType="Integer" resultType="com.wz.domain.User">
        select * from USER where id=#{id};
    </select>

    <select id="findUserByVO" parameterType="com.wz.domain.QueryVO" resultType="com.wz.domain.User">
        select * from USER where name like #{user.name};
    </select>
    
    <select id="findUserInIds" resultType="com.wz.domain.User" parameterType="com.wz.domain.QueryVO">
        <include refid="defaultUser"></include>
        <where>
        sex='M'
            <if test="ids != null and ids.size()>0">
                <foreach collection="ids" open="and id in (" close=")" item="uid" separator=",">
                    #{uid}
                </foreach>
            </if>
        </where>
    </select>
</mapper>

4. 其它

  • log4j.properties
### 设置###
log4j.rootLogger = debug,stdout,D,E

### 输出信息到控制抬 ###
log4j.appender.stdout = org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target = System.out
log4j.appender.stdout.layout = org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH:mm:ss,SSS} method:%l%n%m%n

### 输出DEBUG 级别以上的日志到=E://logs/error.log ###
log4j.appender.D = org.apache.log4j.DailyRollingFileAppender
log4j.appender.D.File = E://logs/log.log
log4j.appender.D.Append = true
log4j.appender.D.Threshold = DEBUG 
log4j.appender.D.layout = org.apache.log4j.PatternLayout
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n

### 输出ERROR 级别以上的日志到=E://logs/error.log ###
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File =E://logs/error.log 
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR 
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
正文到此结束