目录
web.xml
首先编写web.xml
配置文件
下面添加的代码都是添加在<web-app></web-app>
中的
<!--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>
applicationContext.xml
文件没有就新建,右击resources
文件夹,然后按照下图方式新建
新建的位置是在resources
文件夹中,新建出来的就是spring
的配置文件了,新建出来我们先不管它
然后配置spring MVC
的前端控制器
<!--2.配置springMVC的前端控制器,拦截所有的请求-->
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
我们要在与web.xml
同级的目录中新建一个dispatcherServlet-servlet.xml
,新建的方法上面有
然后我们要添加字符编码过滤器
<!--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>
<init-param>
<param-name>forceRequestEncoding</param-name>
<param-value>ture</param-value>
</init-param>
<init-param>
<param-name>forceResponseEncoding</param-name>
<param-value>ture</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
然后继续
<!--4.使用Rest风格的URL,将页面普通的post请求转为指定的delete,put请求-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
这样子我们的web.xml
就配置好了
<?xml version="1.0" encoding="UTF-8"?>
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!--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>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</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>
<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>
<!--4.使用Rest风格的URL,将页面普通的post请求转为指定的delete,put请求-->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
SpringMVC的配置文件
接下来配置spring mvc
了
首先配置只扫描控制器
<!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<!--只扫描控制器-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
然后配置视图解析器
<!--配置视图解析器,方便页面返回-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
这里要在WEB-INF
文件夹中新建一个名字叫views
的文件夹
然后就到两个标准配置了
<!--两个标准配置-->
<!--将SpringMVC不能处理的请求交给Tomcat处理-->
<mvc:default-servlet-handler/>
<!--
有了这个配置能支持springMVC更高级的一些功能,例如JSR303校验,快捷的ajax请求等等
还有就是映射动态请求
-->
<mvc:annotation-driven/>
到这里我们的SpringMVC
的配置文件就基本配置完了
<?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/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">
<!--SpringMVC的配置文件,包含网站跳转逻辑的控制,配置-->
<context:component-scan base-package="com.atguigu" use-default-filters="false">
<!--只扫描控制器-->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:include-filter>
</context:component-scan>
<!--配置视图解析器,方便页面返回-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--前缀-->
<property name="prefix" value="/WEB-INF/views/"></property>
<!--后缀-->
<property name="suffix" value=".jsp"></property>
</bean>
<!--两个标准配置-->
<!--将SpringMVC不能处理的请求交给Tomcat处理-->
<mvc:default-servlet-handler/>
<!--
有了这个配置能支持springMVC更高级的一些功能,例如JSR303校验,快捷的ajax请求等等
还有就是映射动态请求
-->
<mvc:annotation-driven/>
</beans>
spring的配置文件
接下来到spring
的配置文件了,还记得我们之前新建的applicationContext.xml
文件吗?
首先
<!--扫描除了控制器以外的-->
<context:component-scan base-package="com.atguigu" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
配置数据源
<!--
spring的配置文件,这里主要配置和业务逻辑相关的
比如说数据源,事物控制等等都在这里控制。
-->
<!--引入配置文件-->
<context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
文件dbconfig.properties
放在resources
文件夹中
jdbc.jdbcUrl=jdbc:mysql://localhost:3306/ssm_crud?useSSL=false&useUnicode=true&characterEncoding=UTF8&serverTimezone=GMT
jdbc.driverClass=com.mysql.cj.jdbc.Driver
jdbc.user=root
jdbc.password=1230
然后配置和MyBatis
的整合
<!--配置和MyBatis的整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定MyBatis全局配置文件的位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--指定数据源-->
<property name="dataSource" ref="pooledDataSource"></property>
<!--指定MyBatis的mapper文件的位置,扫描所有.xml文件-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
文件mybatis-config.xml
也是放在resources
文件夹下
另外在resources
文件夹下新建一个文件夹mapper
存放MyBatis
的mapper
文件
还有配置扫描器
<!--配置扫描器,将mybatis接口的实现加入到IOC容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有dao接口的实现,加入到IOC容器中-->
<property name="basePackage" value="com.atguigu.crud.dao"></property>
</bean>
到这里和MyBatis
的整合就做完了
然后是事物控制的配置
<!--事物控制的配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制住数据源-->
<property name="dataSource" ref="pooledDataSource"></property>
</bean>
<!--可以开启基于注解的事物,也可以使用xml配置形式的事物,(比较重要的都是使用配置事物)-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* com.atguigu.crud.service..*(..))"/>
<!--配置事物增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!--配置事物增强,也就是配置事物如何切入,transaction-manager的值填的是你上面自己定义的id-->
<tx:advice id="txAdvice">
<tx:attributes>
<!--所有方法都是事物方法-->
<tx:method name="*"></tx:method>
<!--以get开头的所有方法,read-only代表的是只读的-->
<tx:method name="get" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
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
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/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!--扫描除了控制器以外的-->
<context:component-scan base-package="com.atguigu" >
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"></context:exclude-filter>
</context:component-scan>
<!--
spring的配置文件,这里主要配置和业务逻辑相关的
比如说数据源,事物控制等等都在这里控制。
-->
<!--引入配置文件-->
<context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
<bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="user" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置和MyBatis的整合-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--指定MyBatis全局配置文件的位置-->
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<!--指定数据源-->
<property name="dataSource" value="pooledDataSource"></property>
<!--指定MyBatis的mapper文件的位置-->
<property name="mapperLocations" value="classpath:mapper/*.xml"></property>
</bean>
<!--配置扫描器,将mybatis接口的实现加入到IOC容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!--扫描所有dao接口的实现,加入到IOC容器中-->
<property name="basePackage" value="com.atguigu.crud.dao"></property>
</bean>
<!--事物控制的配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<!--控制住数据源-->
<property name="dataSource" value="pooledDataSource"></property>
</bean>
<!--可以开启基于注解的事物,也可以使用xml配置形式的事物,(比较重要的都是使用配置事物)-->
<aop:config>
<!--切入点表达式-->
<aop:pointcut id="txPoint" expression="execution(* com.atguigu.crud.service..*(..))"/>
<!--配置事物增强-->
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPoint"/>
</aop:config>
<!--配置事物增强,也就是配置事物如何切入,transaction-manager的值填的是你上面自己定义的id-->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--所有方法都是事物方法-->
<tx:method name="*"></tx:method>
<!--以get开头的所有方法,read-only代表的是只读的-->
<tx:method name="get" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
</beans>
MyBatis
的配置文件
首先我们到GitHub
中找到MyBatis
的项目
链接在这里
点进去后找到Getting Started
我们要的是第一个示例里面的表头:
复制表头到你的xml
文件中就可以开始在<configuration></configuration>
内写配置了
如果不会配置可以参考官方文档
<?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>
<!--启用驼峰命名法-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="ture"/>
</settings>
<!--类型别名的配置-->
<typeAliases>
<package name="com.atguigu.crud.bean"></package>
</typeAliases>
</configuration>
到这里基本配置就写完了,有什么没写的后面再加