一.首先配置web.xml
1.文件头
<?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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>66_ssm_student</display-name>
2.字符编码过滤器
<filter>
<filter-name>CharactorEncoding</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>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>CharactorEncoding</filter-name>
<url-pattern>*</url-pattern>
</filter-mapping>
3.配置springmvc的DispatcherServlet,DispatcherServlet是SpringMVC的核心控制器,就像是SpringMVC的心脏,几乎所有的请求都会经过这个控制器,通过它,大大的降低了模块之间的耦合度。所有学SpringMVC的同学们第一步肯定都是先配置这个Servlet,不然还写啥SpringMVC啊
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 当servlet-name的值对应不上xml配置文件的时候,我们需要自己去配置文件路径通常我们把spring的配置文件放在src下,所以路径一定要加上classpath:文件名-->
<init-param>
<!-- contextConfigLocation是固定值 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-*.xml</param-value>
</init-param>
<!-- 设置为被第一个启动的servlet -->
<load-on-startup>1</load-on-startup>
</servlet>
4.<servlet-mapping>含有<servlet-name>和<url-pattern>
<servlet-name>:Servlet的名字,唯一性和一致性,与<servlet>元素中声明的名字一致。
<url-pattern>:指定相对于Servlet的URL的路径。该路径相对于web应用程序上下文的根路径。<servlet-mapping>将URL模式映射到某个Servlet,即该Servlet处理的URL
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
二,在config文件夹下创建spring-context.xml.配置spring上下文
1.开头
<?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-4.0.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-4.0.xsd">
2.扫描包路径 ,通常情况下我们在创建spring项目的时候在xml配置文件中都会配置这个标签,配置完这个标签后,spring就会去自动扫描base-package对应的路径或者该路径的子包下面的java文件,如果扫描到文件中带有@Service,@Component,@Repository,@Controller等这些注解的类,则把这些类注册为bean
注:在注解后加上例如@Component(value=”abc”)时,注册的这个类的bean的id就是adc.
<context:component-scan base-package="com.student"></context:component-scan>
3.自动注入
Spring为我们提供了一种极为方便注册这些BeanPostProcessor的方式,即使用<context:annotation- config/>隐式地向 Spring容器注册AutowiredAnnotationBeanPostProcessor、RequiredAnnotationBeanPostProcessor、CommonAnnotationBeanPostProcessor以及PersistenceAnnotationBeanPostProcessor这4个BeanPostProcessor
<context:annotation-config></context:annotation-config>
三,然后在config文件夹下创建spirng-mvc.xml,配置spring-mvc
1.开头
<?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-4.0.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-4.0.xsd">
2.请求映射
Spring 3.0.x中使用了mvc:annotation-driven后,默认会帮我们注册默认处理请求,参数和返回值的类,其中最主要的两个类:DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter ,分别为HandlerMapping的实现类和HandlerAdapter的实现类,从3.1.x版本开始对应实现类改为了RequestMappingHandlerMapping和RequestMappingHandlerAdapter。
<!-- 配置HandlerMapping 得到beanname找到对应的Controller--> 这个不写默认提供
<bean class="org.springframework.web.servlet.mvc.support.ControllerBeanNameHandlerMapping"/>
<!-- 请求映射 -->
<mvc:annotation-driven></mvc:annotation-driven>
<!-- 支持静态资源访问 -->
<mvc:default-servlet-handler/>
<!-- 配置视图解析器 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/view/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
<!-- 配置拦截器 exclude-mapping不拦截
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/*.do"/>
<mvc:exclude-mapping path="/upload.dp"/>
<bean class="com.interceptor.PassInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
-->
<!--上传文件--!>
<!-- 就是帮忙设置上传的一些参数 id 必须填写
配置上传文件的参数
-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding" value="utf-8"/>
<property name="maxInMemorySize" value="10240"/><!-- 缓冲区大小 -->
<property name="uploadTempDir" value="/upload/" /><!-- 上传的文件夹 -->
<property name="maxUploadSize" value="-1" /><!-- -1没有最大上传大小限制 -->
</bean>
四.config下创建mybaits.xml配置文件
1.开头
<?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">
2.mapper类的简写
<configuration>
<!-- 让mapper中的类可以短写 -->
<typeAliases>
<package name="com.student.entity"/>
</typeAliases>
</configuration>
3.分页助手
<configuration>
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
</plugin>
</plugins>
</configuration>
五.在config下配置spring-mybatis.xml配置文件
1.开头
<?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:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.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">
2.配置数据源 ,有三种
(1)配置spring原生连接池 spring-jdbc
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="user01"></property>
<property name="password" value="123"></property>
</bean>
(2)配置dbcp连接池,BasicDataSource提供了close()方法关闭数据源,需要设定destroy-method=”close”属性, 以便Spring容器关闭时,数据源能够正常关闭;
<bean id="dataSource2"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver"></property>
<property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="username" value="user01"></property>
<property name="password" value="123"></property>
</bean>
(3) 配置c3p0连接池,dbcp没有自动回收空闲连接的功能,c3p0有自动回收空闲连接功能 ;
两者主要是对数据连接的处理方式不同,C3P0提供最大空闲时间,DBCP提供最大连接数;
前者当连接超过最大空闲连接时间时,当前连接就会自动断开,DBCP当连接数超过最大连接数时,所有连接都会被断开;
<bean id="dataSource3"
class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass"
value="oracle.jdbc.driver.OracleDriver"></property>
<property name="jdbcUrl"
value="jdbc:oracle:thin:@localhost:1521:orcl"></property>
<property name="user" value="user01"></property>
<property name="password" value="123"></property>
</bean>
3.配置SqlsessionFactory,如果是spring和mybaits整合之后的配置文件,一般以这种方式实现,SqlSessionFactory的创建:
<bean id="sqlSessionFactory"
class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource2"></property>
<property name="configLocation" value="classpath:mybatis.xml"></property>
4.配置MapperScannerConfig
<bean id="mapperScanner"
class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.student.dao"></property>
<property name="sqlSessionFactory" ref="sqlSessionFactory"></property>
</bean>
5.-- 事务管理 -->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理走注解,交给txManager来负责
-->
<tx:annotation-driven transaction-manager="txManager"/>
六,写mapper文件
1.头
<?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">
2,映射名称空间,namespace属性
在MyBatis中,Mapper中的namespace用于绑定Dao接口的,即面向接口编程。
它的好处在于当使用了namespace之后就可以不用写接口实现类,业务逻辑会直接通过这个绑定寻找到相对应的SQL语句进行对应的数据处理
.<mapper namespace="com.student.dao.IStudentMapper">
3.<!-- 1.代码片段 -->,替代*
<sql id="allColumns">
student_id,student_name,student_age,student_gender,student_score,
delete_flag,create_time,modify_time,drop_time
</sql>
4.结果集映射 -
<resultMap type="Student" id="studentMap">
<id column="student_id" property="studentId"/>
<result column="student_name" property="studentName"/>
<result column="student_age" property="studentAge"/>
<result column="student_gender" property="studentGender"/>
<result column="student_score" property="studentScore"/>
<result column="delete_flag" property="deleteFlag"/>
<result column="create_time" property="createTime"/>
<result column="modify_time" property="modifyTime"/>
<result column="drop_time" property="dropTime"/>
</resultMap>
5.增删改查操作
<insert id="save" parameterType="Student">
insert into student(student_id,student_name,
<trim>
<if test="studentAge!=null">student_age,</if>
<if test="studentGender!=null">student_gender,</if>
<if test="studentScore!=null">student_score,</if>
</trim>
create_time)
values(sys_guid(),#{studentName},
<trim>
<if test="studentAge!=null">#{studentAge},</if>
<if test="studentGender!=null">#{studentGender},</if>
<if test="studentScore!=null">#{studentScore},</if>
</trim>
sysdate)
</insert>
<update id="remove" parameterType="Student">
<if test="studentId!=null">
update student set delete_flag='1' where student_id=#{studentId}
and delete_flag='0'
</if>
</update>
<update id="update" parameterType="Student">
update student
<set>
<if test="studentName!=null">student_name=#{studentName},</if>
<if test="studentAge!=null">student_age=#{studentAge},</if>
<if test="studentGender!=null">student_gender=#{studentGender},</if>
<if test="studentScore!=null">student_score=#{studentScore},</if>
</set>
where student_id=#{studentId} and delete_flag='0'
</update>
<select id="queryAll" resultMap="studentMap">
select <include refid="allColumns"></include>
from student
</select>
<select id="queryById" resultMap="studentMap" parameterType="String">
select <include refid="allColumns"></include>
from student
<where>
<if test="value!=null">and student_id=#{studentId}</if>
and delete_flag='0'
</where>
</select>
作者:ccc6ut
链接:https://www.jianshu.com/p/d29c9df79af6
來源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。