ssm 简单整合

ssm整合个人小结

个人感觉SSM 比SSH 整合跟简单 ,spring 和Hiberbate 整合和 spring --myBatis 差不多,但sturuts2 比 spring mvc 复杂 ,因为spring mvc 和 spring本身就是一体的

不用过多的配置。

无论什么框架整合spring ,控制权都在spring 中**

myBatis-----spring--------spring mvc

myBatis-----spring:

需要整合:将MyBatis的SqlSessionFactory 交给Spring

spring-----spring mvc:

就是将Spring - SpringMVC 各自配置一遍

0,进行整合的第一步

下载所需的jar包

1.jar
mybatis-spring.jar

spring-tx.jar

pring-jdbc.jar

spring-expression.jar
spring-context-support.jar

spring-core.jar

spring-context.jar
spring-beans.jar

spring-aop.jar

spring-web.jar

commons-logging.jar

commons-dbcp.jar

ojdbc.jar/或者mysql.jar

mybatis.jar

log4j.jar

commons-pool.jar

1,整合spring 和 myBatis

myBatis 本来需要: conf.xml 可以被spring 的applicationContext.xml 包含在一起

conf.xml(数据源、mapper.xml) --可省,将该文件中的配置 全部交由spring管理

通过mapper.xml 将 类,表 建立映射关系

applicationContext.xml:

<!--将MyBatis的SqlSessionFactory 交给Spring  Dao 交给spring-->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName"
			value="sqlSessionFactory" />
		<property name="basePackage" value="test.mapper" />
		<!--上面basePackage所在的property的作用: 将org.lanqiao.mapper包中,所有的接口 产生与之对应的 动态代理对象 
			(对象名 就是 首字母小写的接口名) :studentMapper.querystudentBYNO(); -->
	</bean>
	
	<!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	<!-- conf.xml : 数据源,mapper.xml -->
	<!-- 配置MyBatis需要的核心类:SqlSessionFactory -->
	<!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations"
			value="classpath:test/mapper/*.xml"></property>
	</bean>\
2,整合spring mvc 和 spring

创建 appliactionContext-controller.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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan
		base-package="test.controller"/>
	<!-- 视图解析器-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="prefix" value="/views/"/>
	 	<property name="suffix" value=".jsp"/>
	</bean>
	<!-- SPringMVC基础配置、标配 -->
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

3,ssm 完全整合

[外链图片转存失败(img-KQovMLBb-1565513778473)(C:\Users\11276\AppData\Roaming\Typora\typora-user-images\1565511666388.png)]

db.properties:数据库连接数据

driver=com.mysql.jdbc.Driver
url=jdbc:mysql:///mybatis
username=root
password=1024

log4j.properties:日志配置文件:

log4j.rootLogger=DEBUG, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

appliactionContext.xml:spring 配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	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">
	<!-- 加载db.properties -->
	<bean id="config"
class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer">
		<property name="locations">
			<array>
				<value>classpath:db.properties</value>
			</array>
		</property>
	</bean>
	<!--controller -->
	<!-- <bean id="studentController"
		class="test.controller.studentContorller">
		<property name="studentService" ref="studentService"/>
		</bean> 
	-->
	<!--Service -->
	<bean id="studentService"
		class="test.serviceimpl.studentServiceimpl">
		<property name="studentMapper" ref="studentMapper" />
	</bean>
	
	<!--将MyBatis的SqlSessionFactory 交给Spring -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName"
			value="sqlSessionFactory" />
		<property name="basePackage" value="test.mapper" />
		<!--上面basePackage所在的property的作用: 将org.lanqiao.mapper包中,所有的接口 产生与之对应的 动态代理对象 
			(对象名 就是 首字母小写的接口名) :studentMapper.querystudentBYNO(); -->
	</bean>
	<!-- 配置配置数据库信息(替代mybatis的配置文件conf.xml) -->
	<bean id="dataSource"
		class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="${driver}"></property>
		<property name="url" value="${url}"></property>
		<property name="username" value="${username}"></property>
		<property name="password" value="${password}"></property>
	</bean>
	<!-- conf.xml : 数据源,mapper.xml -->
	<!-- 配置MyBatis需要的核心类:SqlSessionFactory -->
	<!-- 在SpringIoc容器中 创建MyBatis的核心类 SqlSesionFactory -->
	<bean id="sqlSessionFactory"
		class="org.mybatis.spring.SqlSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<!-- 加载mapper.xml路径 -->
		<property name="mapperLocations"
			value="classpath:test/mapper/*.xml"></property>
	</bean>
</beans>

appliactionContext-controller.xml:spring mvc 配置文件:

<?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:mvc="http://www.springframework.org/schema/mvc"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.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.3.xsd">
	<!-- 开启扫描 -->
	<context:component-scan
		base-package="test.controller"/>
	<!-- 解析器-->
	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
	 	<property name="prefix" value="/views/"/>
	 	<property name="suffix" value=".jsp"/>
	</bean>
	<!-- SPringMVC基础配置、标配 -->
	<mvc:annotation-driven></mvc:annotation-driven>
</beans>

剩下的:

[外链图片转存失败(img-yBRFYyB1-1565513778474)(C:\Users\11276\AppData\Roaming\Typora\typora-user-images\1565512148585.png)]

以上是web 的3层架构:

test.mapper:就是以前的Dao.整合后mapper不用实现接口,因为

<!--将MyBatis的SqlSessionFactory 交给Spring -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<property name="sqlSessionFactoryBeanName"
			value="sqlSessionFactory" />
		<property name="basePackage" value="test.mapper" />
		<!--上面basePackage所在的property的作用: 将org.lanqiao.mapper包中,所有的接口 产生与之对应的 动态代理对象 
			(对象名 就是 首字母小写的接口名) :studentMapper.querystudentBYNO();-->

调用的时候注入方式:

<bean id="studentService"
		class="test.serviceimpl.studentServiceimpl">
		<!-- 首字母小写的接口名-->
		<property name="studentMapper" ref="studentMapper" />
	</bean>

其他按照ssh中:contorller----service-----dao-----db

4,测试SSM 是否成功

案例:查询Student 中的一号学生的信息:

<?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="test.mapper.studentMapper">
	<select id="queryStudentByStuno" parameterType="int" 			resultType="test.domain.student">
		select * from student where stuno = #{stuNo}
	</select>
	<select id="addStudent" parameterType="test.domain.student" >
		insert into student(stuno,stuname,stuage) values(#{stuNo},#{stuName},#{stuAge})
	</select>
</mapper>

contorller----service-----dao-----db

studentContorlle.java:

package test.controller;

import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import test.domain.student;
import test.service.studentService;
@RequestMapping("controller")
@Controller
public class studentContorller {
	@Autowired//自动装配
	@Qualifier("studentService")//根据名字进行自动装配
	private studentService studentService;

	public void setStudentService(studentService studentService) {
		this.studentService = studentService;
	}	
	@RequestMapping("queryStudentByNo/{stuNo}")
	public String queryStudentByNo(@PathVariable("stuNo") Integer stuNo ,Map<String, Object> map) {
		student student = studentService.queryStudentByStuno(stuNo);
		map.put("student", student);
		return "result";
	}
}

index.jsp: 开始页面

<%@ 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>
	<a href="controller/queryStudentByNo/1">一号学生</a>
</body>
</html>

result.jsp:转页面

<%@ 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>
	${requestScope.student.stuNo}//el
	${requestScope.student.stuName}
	${requestScope.student.stuAge}
</body>
</html>

查询结果:![1565513646824](C:\Users\11276\AppData\Roaming\Typora\typora-user-images\1565513646824.png

​ 2019.8.11

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值