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包.
由于刚开始学习,所以采用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>
简单看下类结构
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";
}
}