SSM初步整合一
SSM(spring+springmvc+mybatis)初步整合一配置步骤:
步骤一、导入所需jar包
步骤二 、配置web.xml文件
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <display-name></display-name>
<!--1.针对spring的配置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 注册ServletContext监听器,创建容器对象,并且将ApplicationContext对象放到Application域中 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
<!-- 针对SpringMVC的配置::::::中央调度器:本质上一个serlvet 配置的关于SpringmVC组件 --> <servlet> <servlet-name>springmvc</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.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> <!--乱码过滤器 --> <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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
步骤三、准备我们的mybatis配置文件
<?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="cn/hmy/entity/IUserInfoDAO.xml" /> <!-- 只需配置一次 但小配置文件必须放在dao包中,且与dao中的接口名称相同 <package name="cn.hmy.dao"/> --> </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="cn.hmy.dao.IUserInfoDAO"> <select id="add" parameterType="cn.hmy.entity.UserInfo"> insert into userinfo values(SEQ_SSM.nextval,#{uname},#{uage}) </select> </mapper>
附:也可以使用别名
<?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>
<typeAliases> <package name="cn.hmy.entity"/> </typeAliases>
<mappers> <mapper resource="cn/hmy/entity/IUserInfoDAO.xml" /> <!-- 只需配置一次 但小配置文件必须放在dao包中,且与dao中的接口名称相同 <package name="cn.hmy.dao"/> --> </mappers> </configuration>
在小配置中parameterType="cn.hmy.entity.UserInfo" 就可以省略包名
步骤五、准备jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>添加</title> </head> <body> <form action="${pageContext.request.contextPath }/userAction.do" method="post"> 用户名:<input name="uname"/> <br/> 用户年龄<input name="uage"/><br/> <input type="submit" value="save"/> </form> </body> </html>
步骤四、配置applicationContext.xml文件
注意:
(1).创建sessionFactory的实现类 通过SqlSessionFactoryBean工厂对象
(2)此处的dao没有实现类 其实现类是通过动态代理得到的
所以我们注册dao时 用到了org.mybatis.spring.mapper.MapperScannerConfigurer 通过它的属性来配置
(3).我们注册的dao没有id属性 ,当service引用其对象时
如果dao接口的名称为IXxxxDao,引用时就原样输出
如果dao接口的名称为XxxxDao,引用时首字母要小写
<?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:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 注册jdbc属性 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="location" value="classpath:jdbc.properties"></property> </bean> <!--注册c3p0数据源 --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.className}"></property> <property name="jdbcUrl" value="${jdbc.url}"></property> <property name="user" value="${jdbc.username}"></property> <property name="password" value="${jdbc.password}"></property> </bean> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:mybatis-config.xml"></property> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置dao --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> <property name="basePackage" value="cn.hmy.dao"></property> </bean> <!--配置service --> <bean id="userService" class="cn.hmy.service.UserInfoServiceImpl"> <property name="dao" ref="IUserInfoDAO"></property> </bean> <!--配置controller --> <bean id="/userAction.do" class="cn.hmy.controller.UserInfoController"> <property name="service" ref="userService"></property> </bean> <!--配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!--配置开启事务操作 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <!--指定在连接方法上应用的事务属性 --> <tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED"/> </tx:attributes> </tx:advice> <!--aop配置 --> <aop:config> <aop:pointcut expression="execution(* *..service.*.*(..))" id="stockPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="stockPointcut"/> </aop:config> </beans>
配置完毕,
下面讲解代码
index.jsp提交后 会将请求提交给对应的controller,controller会模拟servlet拆解请求参数封装成对应的实体
package cn.hmy.controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import cn.hmy.entity.UserInfo; import cn.hmy.service.IUserInfoService; public class UserInfoController implements Controller{ private IUserInfoService service; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String uname=request.getParameter("uname"); Integer uage=Integer.valueOf(request.getParameter("uage")); UserInfo info=new UserInfo(); info.setUage(uage); info.setUname(uname); service.add(info); return new ModelAndView("/welcome.jsp"); } public IUserInfoService getService() { return service; } public void setService(IUserInfoService service) { this.service = service; } }
controller会逐级调用service,dao
package cn.hmy.service; import cn.hmy.dao.IUserInfoDAO; import cn.hmy.entity.UserInfo; public class UserInfoServiceImpl implements IUserInfoService { private IUserInfoDAO dao; public void add(UserInfo info) { dao.add(info); } public IUserInfoDAO getDao() { return dao; } public void setDao(IUserInfoDAO dao) { this.dao = dao; } }
package cn.hmy.dao; import cn.hmy.entity.UserInfo; /** * 不需要dao的实现类,通过代理生成 * @author * */ public interface IUserInfoDAO { public void add(UserInfo info); }
调用完成之后即添加完成之后,会返回ModelANdView视图,至此 我们的SSM初步整合完成