本站网址
www.kjyfx.com

『Java』动态 Web项目(SSM整合框架)常规配置

一、配置文件放置结构

二、相关配置文件

1.web.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
    </welcome-file-list>

 <!-- 配置前端控制器 -->
    <servlet>
         <servlet-name>springmvc</servlet-name>
         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
         <init-param>
             <param-name>contextConfigLocation</param-name>
             <param-value>classpath:spring/*.xml</param-value>
         </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

 <!-- 编码过滤器 -->
    <filter>
         <filter-name>encodingFilter</filter-name>
         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
         <init-param>
             <param-name>encoding</param-name>
             <param-value>utf-8</param-value>
         </init-param>
    </filter>
    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>  
</web-app>

 

2.applicationContext.xml 配置文件

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">

<!-- 1.加载jdbc.properties文件的位置 -->
    <context:property-placeholder location="classpath:jdbc.properties"/>

<!-- 2.配置druid连接池 ,id是固定值,class是druid连接池类的全路径 -->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <!-- 配置连接数据库的基本信息 -->
        <property name="driverClassName" value="${db.driverClassName}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>

<!-- 3.整合spring和mybatis框架 将SqlSession等对象的创建交给Spring容器 id值(sqlSessionFactory)是固定值 -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <!-- 3.1.指定mybatis核心配置文件的位置 -->
        <property name="configLocation"  value="classpath:mybatis/mybatis-config.xml"></property>
        <!-- 3.2.配置连接池(数据源) ref指向连接池bean对象的id值 -->
        <property name="dataSource" ref="dataSource"></property>
        <!-- 3.3、扫描所有的 XxxMapper.xml映射文件,读取其中配置的SQL语句 -->
        <property name="mapperLocations" value="classpath:mybatis/mapper/*.xml"/>
    </bean>

<!-- 4、定义mapper接口扫描器 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <!-- 扫描所有XxxMapper接口,将接口实例的创建交给spring容器 -->
        <property name="basePackage"  value="com.kjyfx.dao"/>
    </bean>

<!-- 5.配置需要扫描的包(service层):spring自动去扫描 base-package下的类, 
       如果扫描到的类上有 @Controller、@Service、@Component等注解,
       将会自动将类注册为bean(即由spring创建实例) -->
<context:component-scan  base-package="com.kjyfx.service"> </context:component-scan>
</beans>

3.springmvc-config.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:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc
                        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
                        http://www.springframework.org/schema/beans
                        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
                        http://www.springframework.org/schema/context
                        http://www.springframework.org/schema/context/spring-context-4.0.xsd">

    <!-- 1.配置前端控制器放行静态资源(html/css/js等,否则静态资源将无法访问) -->
    <mvc:default-servlet-handler/>

    <!-- 2.配置注解驱动,用于识别注解(比如@Controller) -->
    <mvc:annotation-driven></mvc:annotation-driven>
 
    <!-- 3.配置需要扫描的包:spring自动去扫描 base-package 下的类, 
      如果扫描到的类上有 @Controller、@Service、@Component等注解, 
      将会自动将类注册为bean  -->
    <context:component-scan base-package="com.kjyfx.controller"> </context:component-scan>

    <!-- 4.配置内部资源视图解析器 prefix:配置路径前缀 suffix:配置文件后缀 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/> 
    </bean>
</beans

 

4.jdbc.properties配置文件

db.driverClassName=com.mysql.jdbc.Driver
db.url=jdbc:mysql://localhost:3306/food?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
db.username=root
db.password=root

 

5.log4j.properties配置文件

# Global logging configuration
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

6.关于XXXMapper.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
   PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
   "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<!--namespace值为对应接口的全路径 -->
<mapper namespace="com.kjyfx.dao.UserMapper">
    <!-- 写入相关的sql语句 -->

</mapper>

 

7.pom.xml中基础依赖(根据需要添加)

<!-- 集中定义依赖版本号 --> 
<properties> 
    <junit.version>4.10</junit.version> 
    <spring.version>4.1.3.RELEASE</spring.version> 
    <mybatis.version>3.2.8</mybatis.version> 
    <mybatis.spring.version>1.2.2</mybatis.spring.version> 
    <mysql.version>5.1.32</mysql.version> 
    <druid.version>1.1.6</druid.version> 
    <slf4j.version>1.6.4</slf4j.version> 
    <jstl.version>1.2</jstl.version> 
    <servlet-api.version>2.5</servlet-api.version> 
    <jsp-api.version>2.0</jsp-api.version>
    <jackson.version>2.4.2</jackson.version> 
</properties>
<dependencies>
    <!-- 单元测试 -->
    <dependency> 
        <groupId>junit</groupId> 
        <artifactId>junit</artifactId> 
        <version>${junit.version}</version>
        <scope>test</scope> 
    </dependency>
    
    <!-- 整合spring框架(包含springmvc) 这个jar文件包含springmvc开发时的核心类, 同时也会将依赖的相关jar文件引入进来(spring的核心jar文件也包含在内) --><!-- 整合spring框架(包含springmvc) 这个jar文件包含springmvc开发时的核心类, 同时也会将依赖的相关jar文件引入进来(spring的核心jar文件也包含在内) --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> </dependency>
    <dependency> 
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId> 
        <version>${spring.version}</version> 
    </dependency>

    <!--这个jar文件包含对Spring对JDBC数据访问进行封装的所有类 -->
    <dependency>
        <groupId>org.springframework</groupId> 
        <artifactId>spring-jdbc</artifactId>
        <version>${spring.version}</version>
    </dependency>
    <dependency> 
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>${spring.version}</version> 
    </dependency>
    
    <!-- 整合mybatis框架 -->
    <dependency>
        <groupId>org.mybatis</groupId> 
        <artifactId>mybatis</artifactId>
        <version>${mybatis.version}</version> 
    </dependency>
    <dependency> 
        <groupId>org.mybatis</groupId> 
        <artifactId>mybatis-spring</artifactId> 
        <version>${mybatis.spring.version}</version>
     </dependency>

    <!-- mysql驱动 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>${mysql.version}</version>
    </dependency>

    <!-- druid连接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>${druid.version}</version>
    </dependency>    
    
    <!-- 整合log4j -->
    <dependency> 
        <groupId>org.slf4j</groupId> 
        <artifactId>slf4j-log4j12</artifactId>
        <version>${slf4j.version}</version> 
    </dependency>

    <!-- Jackson Json处理工具包 -->
    <dependency> 
        <groupId>com.fasterxml.jackson.core</groupId> 
        <artifactId>jackson-databind</artifactId> 
        <version>${jackson.version}</version> 
    </dependency>

    <!-- Servlet/JSP/JSTL -->
    <dependency> 
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId> 
        <version>${servlet-api.version}</version> 
        <scope>provided</scope>
    </dependency>
    <dependency> 
        <groupId>javax.servlet</groupId> 
        <artifactId>jsp-api</artifactId> 
        <version>${jsp-api.version}</version> 
        <scope>provided</scope> 
    </dependency>
    <dependency>
        <groupId>jstl</groupId>
        <artifactId>jstl</artifactId>
        <version>${jstl.version}</version> 
    </dependency>

</dependencies>

配置文件下载:https://www.lanzous.com/b07l6ijyb

 

赞(2) 打赏
未经允许不得转载:所有资源由个人搜集整理自互联网,仅用于学习交流使用,请勿用于其他商业用途。如有侵权,请联系2448941602@qq.com删除科技云分享 » 『Java』动态 Web项目(SSM整合框架)常规配置
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

更多资源,尽在微信公众号【科技云分享】

关注公众号加入Q群

我叫科技云,感谢您的支持~

支付宝扫一扫打赏

微信扫一扫打赏