β

SSM整合

Harries Blog™ 51 阅读
<!-- SpringMVC -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-webmvc</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- Spring-JDBC,要和spring-webmvc的版本一致 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-jdbc</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- MyBatis -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis</artifactId>
			<version>3.4.6</version>
		</dependency>

		<!-- MyBatis-Spring 整合jar包 -->
		<dependency>
			<groupId>org.mybatis</groupId>
			<artifactId>mybatis-spring</artifactId>
			<version>1.3.2</version>
		</dependency>

		<!-- MySQL驱动jar包 -->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<version>5.1.28</version>
		</dependency>

		<!-- DBCP连接池 -->
		<dependency>
			<groupId>commons-dbcp</groupId>
			<artifactId>commons-dbcp</artifactId>
			<version>1.4</version>
		</dependency>

		<!-- 添加jackson,自动转换为JSON数据 -->
		<dependency>
			<groupId>com.fasterxml.jackson.core</groupId>
			<artifactId>jackson-databind</artifactId>
			<version>2.9.4</version>
		</dependency>

		<!-- 添加jstl标签库 -->
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<version>1.2</version>
		</dependency>

		<!-- Junit -->
		<dependency>
			<groupId>junit</groupId>
			<artifactId>junit</artifactId>
			<version>4.12</version>
		</dependency>


		<!-- 导入aspectj依赖 -->
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>1.8.13</version>
		</dependency>

		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>1.8.13</version>
		</dependency>

		<!-- 导入spring的aop -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-aop</artifactId>
			<version>3.2.8.RELEASE</version>
		</dependency>

		<!-- 添加文件上传的依赖 -->
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.3.1</version>
		</dependency>

		<dependency>
			<groupId>commons-io</groupId>
			<artifactId>commons-io</artifactId>
			<version>2.4</version>
		</dependency>

配置 数据库 连接信息 — db.properties文件

url=jdbc:mysql://localhost:3306/db_blog3?useUnicode=true&characterEncoding=utf8
driver=com.mysql.jdbc.Driver
user=root
password=root
initSize=2
maxSize=10

Mybatis和Spring整合 — spring-dao.xml

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<!-- 组件扫描 用于自动创建mapper包下的所有接口的对象,否则将不能使用@Resource注解的方式注入接口对象 -->
	<context:component-scanbase-package="cn.tedu.blog.mapper"/>
	
	<!-- 配置MapperScannerConfigurer -->
	<beanclass="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 用于配置持久层接口在哪里,指定mapper的包 -->
		<propertyname="basePackage"value="cn.tedu.blog.mapper"/>
	</bean>
	
	<!-- 加载db.properties,其中定义了数据库的配置信息 -->
	<util:propertiesid="dbConfig"location="classpath:db.properties"/>

	<!-- 数据源 -->
	<beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
		<propertyname="url"value="#{dbConfig.url}"/>
		<propertyname="driverClassName"value="#{dbConfig.driver}"/>
		<propertyname="username"value="#{dbConfig.user}"/>
		<propertyname="password"value="#{dbConfig.password}"/>
		<propertyname="initialSize"value="#{dbConfig.initSize}"/>
		<propertyname="maxActive"value="#{dbConfig.maxSize}"/>
	</bean>


	<!-- 配置SqlSessionFactoryBean -->
	<beanclass="org.mybatis.spring.SqlSessionFactoryBean">
		<!-- 用于配置数据库连接池 -->
		<propertyname="dataSource"ref="dataSource"/>
		<!-- 用于配置持久层映射文件在哪里,所有的xml文件,使用通配符 -->
		<propertyname="mapperLocations"value="classpath:mappers/*.xml"/>
	</bean>
	

	<!-- 配置事务管理器 -->
	<beanid="transactionManager"class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入数据源,这里使用的是上面配置好的DataSource -->
		<propertyname="dataSource"ref="dataSource"></property>
	</bean>
	
	<!-- 开启事务注解 ,transaction-manager指定的是上面配置的事务管理器的id-->
	<tx:annotation-driventransaction-manager="transactionManager"/>
</beans>

Spring与SpringMVC不需要整合

springMVC.xml
controller
<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<!-- 组件扫描 自动创建对象 -->
	<context:component-scanbase-package="cn.tedu.blog.controller"/>

	<!-- 配置ViewResolver视图解析器 -->
	<bean
		class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<propertyname="prefix"value="/"/>
		<propertyname="suffix"value=".jsp"/>
	</bean>

	<!-- 配置驱动,用于@ResponseBody的使用 -->
	<mvc:annotation-driven></mvc:annotation-driven>

	<mvc:interceptors>
		<mvc:interceptor>
			<!-- 配置拦截器的路径 -->
			<mvc:mappingpath="/blogger/*"/>
			<mvc:mappingpath="/blogType/*"/>
			<!-- 配置不拦截的路径 -->
			<mvc:exclude-mappingpath="/blogger/showLogin.do"></mvc:exclude-mapping>
			<mvc:exclude-mappingpath="/blogger/login.do"></mvc:exclude-mapping>
			<mvc:exclude-mappingpath="/blogger/showInfo.do"></mvc:exclude-mapping>

			<beanclass="cn.tedu.blog.interceptor.LoginInterceptor"></bean>
		</mvc:interceptor>
	</mvc:interceptors>
	
	<!-- 上传组件的解析器 -->
	<beanid="multipartResolver"
		class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
		<!-- 上传文件大小 -->
		<propertyname="maxUploadSize"value="10000000"></property>
		<!-- 请求的编码格式,必须和jSP的pageEncoding属性一致,以便正确读取表单的内容,默认为ISO-8859-1 -->
		<propertyname="defaultEncoding"value="utf-8"></property>
	</bean>

</beans>

业务层 的配置文件 – spring-service.xml

<?xml version="1.0" encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
	xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:util="http://www.springframework.org/schema/util"
	xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xsi:schemaLocation="
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
		http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
		http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
		http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
		http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
		http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
		http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd">

	<!-- 组件扫描 -->
	<context:component-scanbase-package="cn.tedu.blog.service"/>

</beans>

配置 web.xml 文件

<!-- 解决POST提交方式的中文乱码的过滤器 -->
	<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>
	</filter>
	<filter-mapping>
		<filter-name>CharacterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<!-- 配置SpringMVC的前端控制器 -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<!-- springmvc的配置文件 -->
			<param-value>classpath:springMVC.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>*.do</url-pattern>
	</servlet-mapping>
	
	<!-- 使用ContextLoaderListener配置spring的监听器,主要是在启动的时候加载spring的配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
			<!-- 指定所有的spring配置文件,这里使用 * 通配符 -->
			<param-value>classpath:spring-*.xml</param-value>
	</context-param>
	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

原文

https ://chenjiabing666. git hub.io/2018/05/27/SSM整合/

本站部分文章源于互联网,本着传播知识、有益学习和研究的目的进行的转载,为网友免费提供。如有著作权人或出版方提出异议,本站将立即删除。如果您对文章转载有任何疑问请告之我们,以便我们及时纠正。 PS:推荐一个微信公众号: askHarries 或者qq群:474807195,里面会分享一些资深架构师录制的视频录像:有Spring,MyBatis,Netty源码分析,高并发、高性能、分布式、微服务架构的原理,JVM性能优化这些成为架构师必备的知识体系。还能领取免费的学习资源,目前受益良多

转载请注明原文出处: Harries Blog™ » SSM整合

作者:Harries Blog™
追心中的海,逐世界的梦
原文地址:SSM整合, 感谢原作者分享。