SpringMVC_Spring_Mybits框架整合记录
2017-06-21 10:41
337 查看
SpringMVC_Spring_Mybits框架整合记录
之前学习了Struts2,Hibernate,Spring等框架的基本知识以及它们之间的整合,接下来过渡到了SpringMVC,Mybits,Spring了,总体感觉后者更加方便更加好用,在此简单记录下SSM框架的整合过程。SpringMVC介绍:
SpringMVC是一个视图层框架,和Struts2框架的功能一致,都是直接负责和客户交互的。还记得Spring框架的6大核心功能吗,它们分别是IOC容器,JDBC,ORM,WEB,AOP,以及SpringEE,其中SpringMVC就属于WEB模块中。SpringMVC jar包:
spring-web-3.2.5.RELEASE.jar 官方包中
spring-webmvc-3.2.5.RELEASE.jar 官方包中
Spring核心jar包:
spring-beans-3.2.5.RELEASE.jar 官方包中
spring-context-3.2.5.RELEASE.jar 官方包中
spring-core-3.2.5.RELEASE.jar 官方包中
spring-expression-3.2.5.RELEASE.jar 官方包中
SpringORM核心jar包:
spring-orm-3.2.5.RELEASE.jar 官方包中
spring-jdbc-3.2.5.RELEASE.jar 官方包中
spring-tx-3.2.5.RELEASE.jar 官方包中
SpringAop核心jar包:
aopalliance.jar 需要另外下载,SpringAop也是基于AspectJ开发的
aspectjweaver.jar 需要另外下载,SpringAop也是基于AspectJ开发的
cglib-2.2.jar java cglib动态代理
spring-aop-3.2.5.RELEASE.jar 官方包中
spring-aspects-3.2.5.RELEASE.jar 官方包中
开始整合:
新建项目ssm,并引入所需要的jar文件,注意mysql数据库的驱动包,以及数据库连接池c3p0包.![](http://upload-images.jianshu.io/upload_images/2756952-c89074a4f8fb8dc0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
由于刚开始学习,所以采用jar文件形式引入,后续会学习maven方式引入,jar包引入完毕后,开始写配置文件,在WEB-INFO/web.xml中。
<?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>ssm</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <!-- 配置spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- spring的配置文件 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 核心springmvc核心控制器 --> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <!-- springMVC的配置文件 --> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>*.action</url-pattern> </servlet-mapping> <!-- 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> </web-app>
Spring的配置文件在学习SSH框架中已经基本掌握了,当时是和Hibernate结合,事务管理器是基于Hibernate的,但是Mybits框架效率更高,因为它是基于jdbc的,所以在这里注意配置的事务管理器应该是jdbc的事务管理器。
<?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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <context:component-scan base-package="com.itkimi.group.dao.imp,com.itkimi.group.service.imp"></context:component-scan> <context:annotation-config></context:annotation-config> <!-- 加载mysql配置文件 --> <context:property-placeholder location="classpath:db.properties" /> <!-- 配置c3p0 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${driverClass}"></property> <property name="jdbcUrl" value="${jdbcUrl}"></property> <property name="user" value="${user}"></property> <property name="password" value="${password}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property> <property name="maxPoolSize" value="${maxPoolSize}"></property> <property name="minPoolSize" value="${minPoolSize}"></property> </bean> <!-- 配置sessionfactory mybits提供的Spring插件包中--> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 加载mybits的配置文件 --> <property name="configLocation" value="classpath:mybits.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 配置事物管理 jdbc的事务管理器--> <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 切入点 --> <tx:advice id="advice" transaction-manager="txManager"> <tx:attributes> <tx:method name="save*" read-only="false" propagation="REQUIRED"/> <tx:method name="add*" read-only="false" propagation="REQUIRED"/> <tx:method name="update*" read-only="false" propagation="REQUIRED"/> <tx:method name="delete*" read-only="false" propagation="REQUIRED"/> <tx:method name="remove*" read-only="false" propagation="REQUIRED"/> <tx:method name="find*" read-only="true" propagation="SUPPORTS"/> <tx:method name="*" rollback-for="Throwable" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!-- 配置aop --> <aop:config> <aop:pointcut expression="execution(* com.itkimi.group.service.imp.*.*(..))" id="pt" /> <aop:advisor advice-ref="advice" pointcut-ref="pt" /> </aop:config> </beans>
Spring的配置中基本步骤:
1.数据源,也就是数据库,一般都是c3p0连接池来完成
2.session工厂,表示每次和数据库交互的一次会话
3.事务管理器
4.aop切面的配置
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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd "> <mvc:annotation-driven /> <!-- 开启注解扫描,并指定扫描的位置 --> <context:component-scan base-package="com.itkimi.group.test"></context:component-scan> </beans>
Mybits配置:
<?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> <mappers> <mapper resource="com/itkimi/group/entity/UserMybits.xml"/> </mappers> </configuration>
这里很简单,就是引入了一个外部的实体类映射文件
<?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="userNamespace"> <resultMap type="com.itkimi.group.entity.User" id="user" > <id property="id" column="u_id" /> <result property="username" column="u_username"/> <result property="password" column="u_password"/> </resultMap> <!-- mybits中sql语句需要自己手动书写,这也是它快的原因之一 --> <insert id="save" parameterType="com.itkimi.group.entity.User" > insert into t_user(u_username,u_password) values(#{username},#{password}) </insert> </mapper>
简单看下类结构
![](http://upload-images.jianshu.io/upload_images/2756952-7e1cefee4ab66da0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
dao service层就是一个save方法,sessionFactory同SpringIOC容器中直接获取即可直接操作数据库。这里主要看下controller层,然后进行简单测试
package com.itkimi.group.test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import com.itkimi.group.entity.User; import com.itkimi.group.service.IUserService; @Controller @RequestMapping("/test") public class TestAction { //通过注解形式利用Spring的自动封装技术,完成bean的注入 @Autowired IUserService userServiceImp; @RequestMapping("/register") public String test(User user){ //user 封装了请求数据 //业务层保存数据成功后返回到test.jsp界面 userServiceImp.saveUser(user); return "/test.jsp"; } }
![](http://upload-images.jianshu.io/upload_images/2756952-7e1cefee4ab66da0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![](http://upload-images.jianshu.io/upload_images/2756952-2aa23788882b4d54.gif?imageMogr2/auto-orient/strip)
WEB之路很漫长,坚持再坚持。
相关文章推荐
- spring3+mybatis3框架整合 启动错误记录
- springmvc mybatis 整合 框架源码 bootstrap html5 mysql oracle spring
- intellij idea SSM框架(SpringMVC-Spring-Mybatis)整合
- 待看-springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- 基于maven搭建ssm开发框架(1) SpringMVC和spring的整合
- springmvc、jpa、spring、mongodb和ehcache整合框架demo
- springMVC框架整合--(Spring+SpringMVC+hibernate)
- SSM框架——详细整合教程(Spring 4.0.2+SpringMVC 4.0.2+MyBatis3.2.6)
- maven整合:最新ssm框架实现spring4.2,springmvc4.2,mybatis3.2
- springmvc+spring4.04+mybatis框架整合
- springmvc mybatis 整合 框架源码 bootstrap html5 mysql oracle spring
- spring小例子-springMVC+mybits整合的小例子
- springmvc mybatis 整合 框架源码 bootstrap html5 mysql oracle spring
- springMVC学习笔记---day02 springMVC+spring+mybatis整合开发框架搭建
- springMVC系列之(四) spring+springMVC+hibernate 三大框架整合
- springmvc mybatis 整合 框架源码 bootstrap html5 mysql oracle spring
- springMVC整合spring和hibernate4(适合于框架的搭建)
- SSM框架(一)之SSM框架整合(Spring,SpringMVC,MyBatis)
- SSM框架整合( Spring 、 SpringMVC 和 Mybatis )
- JavaEE框架——Springmvc入门(注解URL核心类详解、文件上传和下载)、和Spring的整合