ssm整合(spring-springmvc-mybatis)

一、导入jar包

1.导入srping和springmvc的jar包

spring和springmvc的jar包

2.导入mybatis的jar包

mybatis的jar包
注:mybatis和spring整合是要导入桥接的jar包
在这里插入图片描述

3.这里我使用的是mysql数据库和druid连接池导入相应的jar包

在这里插入图片描述在这里插入图片描述

4.导入其他包aspectj,jackson,jst,文件上传组件

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述
导入好所有的jar包后,把重复的低版本的jar删除。

2、配置web.xml文件

<!-- 开启编码过滤器 -->
	<filter>
		<description>字符集过滤器</description>
		<filter-name>encodingFilter</filter-name>
		<filter-class>
		    org.springframework.web.filter.CharacterEncodingFilter
		</filter-class>
		<init-param>
			<description>字符集编码</description>
			<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>
	
	<!-- Spring的监听器 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:config/spring.xml</param-value>
	</context-param>
	<listener>
	    <listener-class>
	        org.springframework.web.context.ContextLoaderListener
	    </listener-class>
	</listener>
	
	<!-- 防止spring内存溢出监听器 -->
	<listener>
		<listener-class>
			org.springframework.web.util.IntrospectorCleanupListener
		</listener-class>
	</listener>
	
	<!-- springmvc配置 -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:config/springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	
	<!-- 配置session超时时间,单位分钟(默认为30分钟) -->
	<session-config>
	    <session-timeout>15</session-timeout>
	</session-config>
	
	<!-- 配置HiddenHttpMethodFilter过滤器实现PUT,DELETE请求 -->
	<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>
	
	<!-- 配置Druid监控器 -->
	<servlet>
		<servlet-name>DruidStatView</servlet-name>
		<servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	    <servlet-name>DruidStatView</servlet-name>
	    <url-pattern>/druid/*</url-pattern>
	</servlet-mapping>

3、项目结构,配置spring和springmvc还有mybatis配置文件。与映射文件

在这里插入图片描述

3.1spring.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:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	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-4.2.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
		
	<!-- SpringIOC的配置文件,排除@Controller和@ControllerAdvice注解修饰的类 -->
	<context:component-scan base-package="com.znsd.ssm">
		<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
		<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
	</context:component-scan>
	
	<!-- 配置druid连接池属性 -->
	<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
	     <property name="url" value="jdbc:mysql://localhost:3306/clazzsystem" />
	     <property name="username" value="用户名" />
	     <property name="password" value="密码" />
	
	     <property name="filters" value="stat" />
	
	     <property name="maxActive" value="20" />
	     <property name="initialSize" value="1" />
	     <property name="maxWait" value="60000" />
	     <property name="minIdle" value="1" />
	
	     <property name="timeBetweenEvictionRunsMillis" value="60000" />
	     <property name="minEvictableIdleTimeMillis" value="300000" />
	
	     <property name="testWhileIdle" value="true" />
	     <property name="testOnBorrow" value="false" />
	     <property name="testOnReturn" value="false" />
	
	     <property name="poolPreparedStatements" value="true" />
	     <property name="maxOpenPreparedStatements" value="20" />
	
	     <property name="asyncInit" value="true" />
	 </bean>
	 
	 <!-- Spring扫描所有的mapper文件 -->
	<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 设置连接池对象 -->
		<property name="dataSource" ref="dataSource" />
		<!-- Mappper所在的包路径 -->
		<property name="mapperLocations" value="classpath:com/znsd/ssm/dao/mapper/*.xml" />
		<!-- 指定mybatis的配置文件 -->
		<property name="configLocation" value="classpath:config/mybatis.xml"></property>
	</bean>
	<!-- Spring 扫描DAO包 -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- DAO包所在的包路径 -->
		<property name="basePackage" value="com.znsd.ssm.dao" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>

	<!-- 配置事务 -->
	<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 事务的通知方式 -->
	<tx:advice id="txAdvice" transaction-manager="txManager">
		<tx:attributes>
			<tx:method name="find*" propagation="REQUIRED" read-only="true" />
			<tx:method name="search*" propagation="REQUIRED" read-only="true" />
			<tx:method name="query*" propagation="REQUIRED" read-only="true" />

			<tx:method name="add*" propagation="REQUIRED" />
			<tx:method name="submit*" propagation="REQUIRED" />
			<tx:method name="save*" propagation="REQUIRED" />
			<tx:method name="insert*" propagation="REQUIRED" />

			<tx:method name="del*" propagation="REQUIRED" />
			<tx:method name="remove*" propagation="REQUIRED" />

			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="modify*" propagation="REQUIRED" />

			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>

	<!-- AOP切面拦截事务 -->
	<aop:config>
	    <aop:pointcut id="serviceMethod"
	        expression="execution(* com.znsd.ssm.service.*.*.*(..))" />
	    <aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethod" />
	</aop:config>
</beans>

3.2springmvc.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:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.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.2.xsd">
	
	<!-- 扫描器 -->
	<context:component-scan base-package="com.znsd.ssm" />
	
	<!-- 配置视图解析器 将视图返回字符串解析到:/WEB-INF/views/返回值.jsp 下 -->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<!-- 视图前缀 -->
		<property name="prefix" value="/WEB-INF/views/" />
		<!-- 视图后置 -->
		<property name="suffix" value=".jsp" />
	</bean>
	
	<!-- 配置文件上传组件 -->
	<bean id="multipartResolver" 
	    class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 指定默认的编码格式 -->
		<property name="defaultEncoding" value="UTF-8" />
		<!-- 指定允许上传的文件大小,单位Byte-->
		<property name="maxUploadSize" value="512000" />
	</bean>
	
	<!-- 将静态资源交由tomcat来处理 -->
	<mvc:default-servlet-handler />
	
	<!-- 注册类型转换器 -->
	<mvc:annotation-driven />
</beans>

##3.3mybatis.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>
	<!--  通过全局配置文件设置延迟加载 -->
	<settings>
		<!-- 全局启用或禁用延迟加载。当禁用时,所有关联对象都会即时加载。 -->
		<setting name="lazyLoadingEnabled" value="true" />
		<!-- 当启用时,有延迟加载属性的对象在被调用时将会完全加载任意属性。否则,每种属性将会按需要加载。 -->
		<setting name="aggressiveLazyLoading" value="false" />
		<!-- 开启或者关闭二级缓存。 -->
		<setting name="cacheEnabled" value="true"/>
	</settings>
	<!-- 取别名,这个必须按照一定的顺序配置 -->
	<typeAliases>
		<!-- 将这个包下面的所有的类全部倒入进来,命名规则就是类名首字母小写 -->
		<package name="com.znsd.ssm.entitys" />
	</typeAliases>
</configuration>

##3.4映射文件

<?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">
<!-- 配置映射文件 -->
<mapper namespace="com.znsd.ssm.dao.StudentDao">
	<!-- result方式映射属性  -->
	<resultMap type="com.znsd.ssm.entitys.Student" id="studentMap">
		<id property="stuId" column="stuId"/>
		<result property="userName" column="userName"/>
		<result property="passWord" column="passWord"/>
		<result property="sex" column="sex"/>
		<result property="age" column="age"/>
		<result property="birthday" column="birthday"/>
		
		<!-- 多对一映射班级信息 fetchType="lazy"设置延迟加载 -->
	    <association property="clazz" column="claId" fetchType="lazy" select="com.znsd.ssm.dao.ClazzDao.selectClazz">
	    </association> 
	</resultMap>
	<!-- 查询 -->
	<select id="selectStudentAll" resultMap="studentMap">
	   select * from tb_student
	</select>
	<!-- 查询使用#{}当参数为一个时可以随便命名,用${}需要在参数指定名字 -->
	<select id="selectStudent" parameterType="int" resultType="com.znsd.ssm.entitys.Student">
	   select * from tb_student where stuId= #{stuId}
	</select>
	<!-- 增加 -->
	<insert id="addStudent" useGeneratedKeys="true" keyProperty="stuId" parameterType="com.znsd.ssm.entitys.Student" >
	   INSERT INTO tb_student(userName,passWord,sex,age,birthday,claId) VALUES(#{userName},#{passWord},#{sex},#{age},#{birthday},#{clazz.claId});
	</insert>
	<!-- 修改 -->
	<update id="updateStudent" >
	   update tb_student 
	  <set>
			<if test="sex!=null and sex!=''">
				sex = #{sex},
			</if>
			<if test="age!= null and age!=''">
				age = #{age},
			</if>
			<if test="clazz.claId != null and clazz.claId!=''">
				claId = #{clazz.claId},
			</if>
		</set>
		where stuId = #{stuId}
	</update>
	<!-- 删除 -->
	<delete id="delStudent" parameterType="com.znsd.ssm.entitys.Student">
	   delete from tb_student where stuId= #{delId}
	</delete>
</mapper>

<?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">
<!-- 配置映射文件 -->
<mapper namespace="com.znsd.ssm.dao.ClazzDao">
	<resultMap type="com.znsd.ssm.entitys.Clazz" id="clazzMap">
		<id property="claId" column="claId"/>
		<result property="claName" column="claName"/>
	</resultMap>
	<!-- 查询全部 -->
	<select id="selectClazzAll" resultMap="clazzMap">
		select * from tb_clazz  
	</select>
	<!-- 查询单个 -->
	<select id="selectClazz" resultMap="clazzMap">
		select * from tb_clazz where claId=#{claId}
	</select>
	<!-- 增加 -->
	<insert id="addClazz" useGeneratedKeys="true" keyProperty="stuId" parameterType="com.znsd.ssm.entitys.Clazz" >
	   INSERT INTO tb_clazz(claName) VALUES(#{claName});
	</insert>
	<!-- 修改 -->
	<update id="updateClazz" parameterType="com.znsd.ssm.entitys.Clazz">
	   update tb_clazz set claName= #{claName} where claId= #{claId}
	</update>
	<!-- 删除 -->
	<delete id="delClazz" parameterType="com.znsd.ssm.entitys.Clazz">
	   delete from tb_clazz where claId= #{claId}
	</delete>
</mapper>

4测试使用

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<% response.sendRedirect("studentlist"); %>
</body>
</html>
@Controller
public class StudentControl {
	@Autowired
	private StudentService studentService;
	@Autowired
	private ClazzService clazzService;
	
	@RequestMapping(value = "/studentlist",method = RequestMethod.GET)
	public String hello(HttpSession session) {
		List<Student>	studentList=studentService.queryStudentList();
		session.setAttribute("studentList", studentList);
		
		List<Clazz> clazzList = clazzService.queryClazzList();
		session.setAttribute("clazzList", clazzList);
		return "studentlist";
	}
}

测试成功
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值