搭建SSM框架
2023-04-18 19:02:13 0 举报
AI智能生成
如何使用IDEA搭建SSM框架。
作者其他创作
大纲/内容
一、前期准备工作
1.创建SSM框架项目,选择骨架maven-archetype-webapp构建项目。要写achetypeCatagory、internal
2.groupId + artifactId + version(SNAP)提供当前项目的打包并发布的坐标
3.修改pom.xml,给maven项目添加上各种依赖的jar包
可以引入:
①spring大礼包(core,beans,context,expression,web,aop):org.springframework ~ spring-webmvc
②mysql驱动:mysql ~ mysql-connector-java
③druid数据库连接池:com.alibaba ~ druid
④mybatis持久层框架:org.mybatis ~ mybatis
⑤spring整合mybatis:org.mybatis ~ mybatis-spring
⑥分页插件:com.github.pagehelper ~ pagehelper
⑦spring数据库相关的jdbc,tx(和事务aop有关):org.springframework ~ spring-jdbc
⑧servlet,jsp,jstl:javax.servlet ~ servlet-api、jsp-api、jstl
⑨json解析:com.fasterxml.jackson.core ~ jackson-core、jackson-databind、jackson-annotations
⑩日志log4j:org.slf4j ~ slf4j-api;log4j ~ log4j
11.文件上传:commons-io ~ commons-io;commons-fileupload ~ commons-fileupload
12.校验:org.hibernate.validator ~ hibernate.validator
13.spring整合junit:org.springframework ~ spring-test;junit ~ junit
14.添加 aspectj的支持,它是AOP的具体实现:org.aspectj ~ aspectjweaver
15.类中快速创建各种方法的lombok:org.projectlombok ~ lombok
①spring大礼包(core,beans,context,expression,web,aop):org.springframework ~ spring-webmvc
②mysql驱动:mysql ~ mysql-connector-java
③druid数据库连接池:com.alibaba ~ druid
④mybatis持久层框架:org.mybatis ~ mybatis
⑤spring整合mybatis:org.mybatis ~ mybatis-spring
⑥分页插件:com.github.pagehelper ~ pagehelper
⑦spring数据库相关的jdbc,tx(和事务aop有关):org.springframework ~ spring-jdbc
⑧servlet,jsp,jstl:javax.servlet ~ servlet-api、jsp-api、jstl
⑨json解析:com.fasterxml.jackson.core ~ jackson-core、jackson-databind、jackson-annotations
⑩日志log4j:org.slf4j ~ slf4j-api;log4j ~ log4j
11.文件上传:commons-io ~ commons-io;commons-fileupload ~ commons-fileupload
12.校验:org.hibernate.validator ~ hibernate.validator
13.spring整合junit:org.springframework ~ spring-test;junit ~ junit
14.添加 aspectj的支持,它是AOP的具体实现:org.aspectj ~ aspectjweaver
15.类中快速创建各种方法的lombok:org.projectlombok ~ lombok
<?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.liying</groupId>
<artifactId>SSM01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SSM01 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.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-version>5.3.5</spring-version>
<mysql-version>8.0.26</mysql-version>
<druid-version>1.2.13</druid-version>
<mybatis-version>3.5.2</mybatis-version>
<mybatis-spring-version>3.0.1</mybatis-spring-version>
<pagehelper-version>5.2.0</pagehelper-version>
<jackson-version>2.11.2</jackson-version>
</properties>
<dependencies>
<!-- spring大礼包:
core,beans,context,expression,web,aop
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
<!--↓这是除了某个依赖不用。出现二义性问题时候尝试使用。-->
<!--<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>-->
</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>
<!--mybatis持久层框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
<!--spring数据库相关的jdbc,tx(和事务aop有关)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--servlet,jsp,jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--json解析-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<!--日志log4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.5</version>
</dependency>
<!--校验-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- 添加 aspectj的支持,它是AOP的具体实现 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</project>
<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.liying</groupId>
<artifactId>SSM01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>SSM01 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.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<spring-version>5.3.5</spring-version>
<mysql-version>8.0.26</mysql-version>
<druid-version>1.2.13</druid-version>
<mybatis-version>3.5.2</mybatis-version>
<mybatis-spring-version>3.0.1</mybatis-spring-version>
<pagehelper-version>5.2.0</pagehelper-version>
<jackson-version>2.11.2</jackson-version>
</properties>
<dependencies>
<!-- spring大礼包:
core,beans,context,expression,web,aop
-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
<!--↓这是除了某个依赖不用。出现二义性问题时候尝试使用。-->
<!--<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
</exclusions>-->
</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>
<!--mybatis持久层框架-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis-version}</version>
</dependency>
<!--spring整合mybatis-->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!--分页插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper-version}</version>
</dependency>
<!--spring数据库相关的jdbc,tx(和事务aop有关)-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring-version}</version>
</dependency>
<!--servlet,jsp,jstl-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<!--json解析-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson-version}</version>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.26</version>
</dependency>
<!--日志log4j-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!--文件上传-->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.11.0</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.5</version>
</dependency>
<!--校验-->
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
</dependency>
<!--spring整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- 添加 aspectj的支持,它是AOP的具体实现 -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
</dependencies>
</project>
4.创建项目目录结构
5.准备数据库(前提是安装好了mybatis插件,可以之后给我们反向工程)
6.创建tomcat服务器,未上线阶段时添加 war exploded版本
二、开发阶段
!!!(必须检查MemberController,MemberServiceImpl头上是否有添加注解)
!!!(必须检查MemberController,MemberServiceImpl头上是否有添加注解)
1.选中要反向工程的表,自动生成entity、dao、mapper.xml
2.创建service层,并实现(在实现里调用dao接口)
3.创建controller层并实现
三、配置文件
1.jdbc.properties
数据库连接属性文件
数据库连接属性文件
可配置:
①驱动位置
②数据库地址
③用户名(mysql)
④密码(mysql)
①驱动位置
②数据库地址
③用户名(mysql)
④密码(mysql)
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/member_db
jdbc.user=root
jdbc.password=root
jdbc.url=jdbc:mysql://localhost:3306/member_db
jdbc.user=root
jdbc.password=root
2.log4j.properties
mybatis框架的日志格式属性文件
mybatis框架的日志格式属性文件
直接引:
#log4j默认的配置文件,myBatis框架就去找log4j.properties日志文件
#log4j 级别 变量
#OFF(关闭日志输出)、FATAL(灾难) >ERROR(错误) > WARN(警告)、INFO(消息)> DEBUG(调试)、ALL(所有级别)
log4j.rootCategory=DEBUG, stdout
#定义stdout = 把日志类型信息写在终端上
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#定义stdout日志的布局,使用的是样板布局
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#定义stdout日志布局的转换样板
# %p: 级别 %t: thread %C:类 %M:方法名 %L:行 %m: 日志信息 %n 换行
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd} %p [%t] %C.%M(%L) | %m%n
#只要是调用dao中的方法就会打印日志
#定义日志对应包中的类如果有日志输出的级别必须>=WARN的才输出
log4j.logger.com.liying.dao=DEBUG
#log4j 级别 变量
#OFF(关闭日志输出)、FATAL(灾难) >ERROR(错误) > WARN(警告)、INFO(消息)> DEBUG(调试)、ALL(所有级别)
log4j.rootCategory=DEBUG, stdout
#定义stdout = 把日志类型信息写在终端上
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
#定义stdout日志的布局,使用的是样板布局
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
#定义stdout日志布局的转换样板
# %p: 级别 %t: thread %C:类 %M:方法名 %L:行 %m: 日志信息 %n 换行
log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd} %p [%t] %C.%M(%L) | %m%n
#只要是调用dao中的方法就会打印日志
#定义日志对应包中的类如果有日志输出的级别必须>=WARN的才输出
log4j.logger.com.liying.dao=DEBUG
3.mybatis-config.xml
mybatis的核心配置文件
mybatis的核心配置文件
可配置:
①使用log4j实现mybatis框架的日志
②设置别名(为了让mapper文件那边省事)
③分页插件
①使用log4j实现mybatis框架的日志
②设置别名(为了让mapper文件那边省事)
③分页插件
<?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>
<!--使用log4j实现mybatis框架的日志-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--设置别名-->
<typeAliases>
<package name="com.liying.entity"/>
</typeAliases>
<!--分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="property" value="mysql"/>
</plugin>
</plugins>
</configuration>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--使用log4j实现mybatis框架的日志-->
<settings>
<setting name="logImpl" value="log4j"/>
</settings>
<!--设置别名-->
<typeAliases>
<package name="com.liying.entity"/>
</typeAliases>
<!--分页插件-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="property" value="mysql"/>
</plugin>
</plugins>
</configuration>
4.spring-mvc.xml
springmvc配置文件
配置MVC结构所需要的东西
springmvc配置文件
配置MVC结构所需要的东西
可配置:
①扫描包路径
⑩格式转换器(由于该xml头上引得那些资源,已经定下了各种东西的配置顺序,所以放这)
②注解驱动(如果使用了格式转换器,就要加上conversion-service,不然用不了)
③配置视图处理器
④上传文件
⑤拦截器
⑥静态资源处理
⑦静态资源映射
⑧跨域处理
⑨异常处理
①扫描包路径
⑩格式转换器(由于该xml头上引得那些资源,已经定下了各种东西的配置顺序,所以放这)
②注解驱动(如果使用了格式转换器,就要加上conversion-service,不然用不了)
③配置视图处理器
④上传文件
⑤拦截器
⑥静态资源处理
⑦静态资源映射
⑧跨域处理
⑨异常处理
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.扫描包路径-->
<context:component-scan base-package="com.liying"/>
<!--10.格式转换器(收参数上来时候使用,save。解决日期格式转换的第二种方法。
建了一个StringToDateConvertor,实现Converter<String, Date>接口)-->
<bean name="csfb" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.liying.util.StringToDateConvertor"/>
</list>
</property>
</bean>
<!--2.注解驱动-->
<!--conversion-service="csfb"是用格式转换器时候才要写的,根据↑的name-->
<mvc:annotation-driven conversion-service="csfb"/>
<mvc:annotation-driven/>
<!--3.配置视图处理器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--4.上传文件-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="uploadTempDir" value="/upload"/>
<property name="maxInMemorySize" value="1024000"/>
<property name="maxUploadSize" value="-1"/>
</bean>
<!--5.拦截器-->
<!--<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class=""/>
</mvc:interceptor>
</mvc:interceptors>-->
<!--6.静态资源处理-->
<mvc:default-servlet-handler/>
<!--7.静态资源映射-->
<mvc:resources mapping="/css/**" location="/static/css/"/>
<mvc:resources mapping="/js/**" location="/static/js/"/>
<mvc:resources mapping="/img/**" location="/static/img/"/>
<!--8.跨域处理
````path:那个路径是允许跨域响应的?
allowed-origin:允许哪些跨域IP来访问我
allow-credentials:是否允许相应的cookie
allow-headers:允许哪些请求头上带的值
allowed-methods: 允许的请求来的方式
max-age: 多长时间内不做跨域相应的验证-->
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="*"
allow-credentials="false"
allowed-headers="*"
allowed-methods="GET,POST"
max-age="3600"
/>
</mvc:cors>
<!--9.异常处理-->
<!--<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<map>
<entry key="java.lang.NullPointerException" value="error"/>
<entry key="java.lang.ArrayIndexOutOfBoundsException" value="error"/>
</map>
</property>
</bean>-->
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--1.扫描包路径-->
<context:component-scan base-package="com.liying"/>
<!--10.格式转换器(收参数上来时候使用,save。解决日期格式转换的第二种方法。
建了一个StringToDateConvertor,实现Converter<String, Date>接口)-->
<bean name="csfb" class="org.springframework.context.support.ConversionServiceFactoryBean">
<property name="converters">
<list>
<bean class="com.liying.util.StringToDateConvertor"/>
</list>
</property>
</bean>
<!--2.注解驱动-->
<!--conversion-service="csfb"是用格式转换器时候才要写的,根据↑的name-->
<mvc:annotation-driven conversion-service="csfb"/>
<mvc:annotation-driven/>
<!--3.配置视图处理器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
<!--4.上传文件-->
<bean class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="uploadTempDir" value="/upload"/>
<property name="maxInMemorySize" value="1024000"/>
<property name="maxUploadSize" value="-1"/>
</bean>
<!--5.拦截器-->
<!--<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class=""/>
</mvc:interceptor>
</mvc:interceptors>-->
<!--6.静态资源处理-->
<mvc:default-servlet-handler/>
<!--7.静态资源映射-->
<mvc:resources mapping="/css/**" location="/static/css/"/>
<mvc:resources mapping="/js/**" location="/static/js/"/>
<mvc:resources mapping="/img/**" location="/static/img/"/>
<!--8.跨域处理
````path:那个路径是允许跨域响应的?
allowed-origin:允许哪些跨域IP来访问我
allow-credentials:是否允许相应的cookie
allow-headers:允许哪些请求头上带的值
allowed-methods: 允许的请求来的方式
max-age: 多长时间内不做跨域相应的验证-->
<mvc:cors>
<mvc:mapping path="/**"
allowed-origins="*"
allow-credentials="false"
allowed-headers="*"
allowed-methods="GET,POST"
max-age="3600"
/>
</mvc:cors>
<!--9.异常处理-->
<!--<bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<map>
<entry key="java.lang.NullPointerException" value="error"/>
<entry key="java.lang.ArrayIndexOutOfBoundsException" value="error"/>
</map>
</property>
</bean>-->
</beans>
5.spring-mybatis.xml
spring整合的mybatis的配置文件
配置数据源>>创建 sqlsessionFactory>>生成 mapper 代理
spring整合的mybatis的配置文件
配置数据源>>创建 sqlsessionFactory>>生成 mapper 代理
可配置:
①引入属性文件
②创建数据源
③创建SqlSessionFactory
④扫描dao,做相应的映射
⑤创建事务管理器
⑥创建通知/监听者
⑦设置切面/监听
①引入属性文件
②创建数据源
③创建SqlSessionFactory
④扫描dao,做相应的映射
⑤创建事务管理器
⑥创建通知/监听者
⑦设置切面/监听
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
<!--1.引入属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.创建数据源-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.创建SqlSessionFactory-->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3.1 配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--3.2 读取mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--3.3 指定所有mapper的位置-->
<property name="mapperLocations" value="classpath:com/liying/mapper/*.xml"/>
</bean>
<!--4.扫描dao,做相应的映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--4.1 指定sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--4.2 指定扫描的dao目录-->
<property name="basePackage" value="com.liying.dao"/>
</bean>
<!--5.创建事务管理器-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--6.创建通知/监听者,一旦有问题,就通知事务管理器进行回滚-->
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--7.设置切面/监听,一旦有问题,就报给监听者-->
<aop:config proxy-target-class="true">
<aop:pointcut id="pc" expression="execution(* com.liying.service.impl.*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="pc"/>
</aop:config>
</beans>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.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">
<!--1.引入属性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--2.创建数据源-->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}"/>
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.user}"/>
<property name="password" value="${jdbc.password}"/>
</bean>
<!--3.创建SqlSessionFactory-->
<bean name="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--3.1 配置数据源-->
<property name="dataSource" ref="dataSource"/>
<!--3.2 读取mybatis配置文件-->
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<!--3.3 指定所有mapper的位置-->
<property name="mapperLocations" value="classpath:com/liying/mapper/*.xml"/>
</bean>
<!--4.扫描dao,做相应的映射-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--4.1 指定sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<!--4.2 指定扫描的dao目录-->
<property name="basePackage" value="com.liying.dao"/>
</bean>
<!--5.创建事务管理器-->
<bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--6.创建通知/监听者,一旦有问题,就通知事务管理器进行回滚-->
<tx:advice id="advice" transaction-manager="tx">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="remove*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="update*" propagation="REQUIRED" isolation="DEFAULT" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" isolation="DEFAULT" read-only="true"/>
</tx:attributes>
</tx:advice>
<!--7.设置切面/监听,一旦有问题,就报给监听者-->
<aop:config proxy-target-class="true">
<aop:pointcut id="pc" expression="execution(* com.liying.service.impl.*.*(..))"/>
<aop:advisor advice-ref="advice" pointcut-ref="pc"/>
</aop:config>
</beans>
6.web.xml
主配置文件
主配置文件
可配置:
①读取数据库(spring-mybatis)
②设置字符编码过滤器
③springmvc上下文监听器
④设置servlet,这样才能让程序读取spring-mvc.xml
⑤配置Session失效时间(默认半个小时)
⑥配置欢迎页面
⑦配置错误页面
①读取数据库(spring-mybatis)
②设置字符编码过滤器
③springmvc上下文监听器
④设置servlet,这样才能让程序读取spring-mvc.xml
⑤配置Session失效时间(默认半个小时)
⑥配置欢迎页面
⑦配置错误页面
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--1.读取spring-mybatis(数据库)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!--2.字符编码过滤器-->
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3.springmvc上下文监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--4.servlet-->
<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-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--5.配置Session失效时间(默认半个小时)-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!--6.配置欢迎页面-->
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<!--<welcome-file-list>
<welcome-file>/member/findAll</welcome-file>
</welcome-file-list>-->
<!--7.错误页面-->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
</web-app>
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--1.读取spring-mybatis(数据库)-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mybatis.xml</param-value>
</context-param>
<!--2.字符编码过滤器-->
<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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!--3.springmvc上下文监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--4.servlet-->
<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-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--5.配置Session失效时间(默认半个小时)-->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<!--6.配置欢迎页面-->
<welcome-file-list>
<welcome-file>/WEB-INF/jsp/index.jsp</welcome-file>
</welcome-file-list>
<!--<welcome-file-list>
<welcome-file>/member/findAll</welcome-file>
</welcome-file-list>-->
<!--7.错误页面-->
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/jsp/error.jsp</location>
</error-page>
</web-app>
李颖自存
0 条评论
下一页