Mybatis的配置文件
编写所需配置文件
spring
框架所需配置文件: applicationContext.xml
springmvc
框架所需配置文件: springmvc-config.xml
mybatis
框架所需配置文件: mybatis-config.xml
db
数据库连接所需配置文件: db.properties
日志框架所需配置文件: log4j.properties
Web程序所需配置文件: web.xml
pom.xml
导入所需依赖的jar包
<?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>PhoneMall</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>PhoneMall 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>
</properties>
<dependencies>
<!--1. Spring框架的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--Aop的辅助依赖Aspects-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--Spring-Jdbc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--Spring-test-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--2. Springmvc框架的依赖-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.5.RELEASE</version>
</dependency>
<!--3. mybatis框架的依赖:核心依赖和辅助依赖-->
<!--3.1 mybatis框架的核心依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.4</version>
</dependency>
<!--3.2 mybatis框架的辅助依赖-->
<!--导入commons-logging的jar包-->
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!--导入log4j依赖-->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>ognl</groupId>
<artifactId>ognl</artifactId>
<version>3.1.12</version>
</dependency>
<!--4. mybatis-spring框架整合的依赖-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.0</version>
</dependency>
<!--5. mysql数据库的依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<!--6. 数据库连接池druid的依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.14</version>
</dependency>
<!--7. MyBatis分页PageHelper的依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
<!--8. Servlet4.0的依赖-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<!--9. Jsp的依赖-->
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.2</version>
</dependency>
<!--10. Jstl的依赖:taglibs-->
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-spec</artifactId>
<version>1.2.5</version>
</dependency>
<dependency>
<groupId>org.apache.taglibs</groupId>
<artifactId>taglibs-standard-impl</artifactId>
<version>1.2.5</version>
</dependency>
<!--11. 工具类的依赖-->
<!--非空校验工具类-->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
<!--上传的工具类-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!--下载的工具类-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!--12. 蚂蚁金服的支付的依赖-->
<dependency>
<groupId>com.alipay.sdk</groupId>
<artifactId>alipay-sdk-java</artifactId>
<version>3.3.49.ALL</version>
</dependency>
<!--13. 单元测试的依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
applicationContext.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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--1. 先去读取数据库的配置文件: db.properties-->
<context:property-placeholder location="classpath:db.properties" />
<!--2. [IOC]定义数据源: id class-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<!--2.1 设置数据源的属性: name=数据库的驱动 value-->
<property name="driverClassName" value="${druid.driverClassName}">
</property>
<!--2.2 设置数据源的属性: name=数据库的链接 value-->
<property name="url" value="${druid.url}"></property>
<!--2.3 设置数据源的属性: name=数据库的用户名 value-->
<property name="username" value="${druid.username}"></property>
<!--2.4 设置数据源的属性: name=数据库的密码 value-->
<property name="password" value="${druid.password}"></property>
</bean>
<!--3. [IOC]定义事务管理器: 数据源事务管理-->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--3.1 设置事务管理器的属性: name=数据源的id ref数据源的id -->
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--4. 开启事务的注解,关联定义过的管理器-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--5. [IOC] 定义MyBatis的控制翻转(IOC): 核心类是 SqlSessionFactoryBean -->
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<!--5.1 设置MyBatis的属性: name=数据源的id ref数据源的id -->
<property name="dataSource" ref="dataSource" />
<!--5.2 设置mybatis配置文件所在的位置: name=数据库的驱动 value-->
<property name="configLocation" value="classpath:mybatis-config.xml" />
<!--配置mybatis全局的配置文件-->
<!--5.3 配置映射文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml" />
<!--配置在resources/mapper下的映射文件-->
</bean>
<!--6. [IOC]定义MyBatis接口扫描信息 核心类是 MapperScannerConfig -->
<bean id="mapperScannerConfigurer"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描的基础包:cn.javabs.phonemall.mapper-->
<property name="basePackage" value="cn.javabs.phonemall.mapper" />
</bean>
<!-- 7. 扫描Service层、Mappper层 的注解 -->
<context:component-scan base-package="cn.javabs.phonemall" />
</beans>
springmvc-config.xml
的编写
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc
https://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!--1. 开启springmvc的注解扫描配置: 可以使用@Controller这种方式-->
<context:component-scan base-package="cn.javabs.phonemall.controller" />
<!--2. 开启mvc的注解驱动-->
<mvc:annotation-driven />
<!--3. 配置静态资源-->
<mvc:resources mapping="/resource/js/**" location="/resource/js/">
</mvc:resources>
<mvc:resources mapping="/resource/css/**" location="/resource/css/">
</mvc:resources>
<mvc:resources mapping="/resource/images/**" location="/resource/images/">
</mvc:resources>
<mvc:resources mapping="/resource/fonts/**" location="/resource/fonts/">
</mvc:resources>
<!--4. 配置视图解析器 -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value=""></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!--5. 配置拦截器 -->
<mvc:interceptors>
<mvc:interceptor>
<!--1. 拦截: 配置登录拦截器-->
<mvc:mapping path="/admin/**/"/>
<bean class="cn.javabs.phonemall.controller.AdminInterceptor">
</bean>
</mvc:interceptor>
</mvc:interceptors>
<!--6. 配置多媒体文件解析器 用于文件的上传操作-->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--设置文件上传的大小,最大允许上传的尺寸为5MB--><!--1024*1024*10/2=5242880-->
<property name="maxUploadSizePerFile" value="5242880"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
编写mybatis-config.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>
<!--1. 分页插件 pagehelper -->
<plugins>
<plugin interceptor="com.github.pagehelper.PageHelper">
</plugin>
</plugins>
</configuration>
编写db.properties
#定义数据库驱动
druid.driverClassName=com.mysql.jdbc.Driver
#定义数据库连接
druid.url=jdbc:mysql://localhost:3306/phonemall
#定义数据库用户名
druid.username=root
#定义数据库密码
druid.password=root
#定义初始连接数
druid.initialSize=0
#定义最大连接数
druid.maxActive=20
#定义最大空闲
druid.maxIdle=20
#定义最小空闲
druid.minIdle=1
#定义最长等待时间
druid.maxWait=60000
编写log4j.properties
#定义LOG输出级别
log4j.rootLogger=INFO,Console,File
#定义日志输出目的地为控制台
log4j.appender.Console=org.apache.log4j.ConsoleAppender
log4j.appender.Console.Target=System.out
#可以灵活地指定日志输出格式,下面一行是指定具体的格式
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern=[%c] - %m%n
#文件大小到达指定尺寸的时候产生一个新的文件
log4j.appender.File = org.apache.log4j.RollingFileAppender
#指定输出目录
log4j.appender.File.File = logs/ssm.log
#定义文件最大大小
log4j.appender.File.MaxFileSize = 10MB
# 输出所以日志,如果换成DEBUG表示输出DEBUG以上级别日志
log4j.appender.File.Threshold = ALL
log4j.appender.File.layout = org.apache.log4j.PatternLayout
log4j.appender.File.layout.ConversionPattern =[%p] [%d{yyyy-MM-dd HH\:mm\:ss}][%c]%m%n
编写web.xml
导入
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--1. spring的监听器 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-
class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--2. springmvc的前端控制器 -->
<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:springmvc-config.xml</param-value>
</init-param>
<!--启动方式:程序一启动就加载本配置-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--3. 核心过滤器 -->
<filter>
<filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
版权声明:本文为博客园博主「任福」的原创文章,转载请附上原文出处链接及本声明。