一、基本概念
使用SSM(spring、SpringMVC和Mybatis)
1.1、Spring
Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Development and Design中阐述的部分理念和原型衍生而来。它是为了解决企业应用开发的复杂性而创建的。Spring使用基本的JavaBean来完成以前只可能由EJB完成的事情。然而,Spring的用途不仅限于服务器端的开发。从简单性、可测试性和松耦合的角度而言,任何Java应用都可以从Spring中受益。 简单来说,Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架。
1.2、SpringMVC
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面。Spring MVC 分离了控制器、模型对象、分派器以及处理程序对象的角色,这种分离让它们更容易进行定制。
1.3、MyBatis
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。MyBatis是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO)MyBatis 消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索。MyBatis 使用简单的 XML或注解用于配置和原始映射,将接口和 Java 的POJOs(Plain Old Java Objects,普通的 Java对象)映射成数据库中的记录。
二、根据图来理解使用SSM添加数据
首先导入jar
其次
(一)配置实体
public class UserInfo { private Integer userid;//用户的id private Integer uage;//用户年龄 private String uname;//用户姓名 public Integer getUage() { return uage; } public void setUage(Integer uage) { this.uage = uage; } public String getUname() { return uname; } public void setUname(String uname) { this.uname = uname; } public Integer getUserid() { return userid; } public void setUserid(Integer userid) { this.userid = userid; } }
(二)配置实体的小配置(Mybatis的配置)
<?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.yxj.dao.IUserInfoDAO"> <select id="add" parameterType="cn.yxj.entity.UserInfo"> <!-- <select id="add" parameterType="UserInfo"> 需要别名在mybatis-config.xml--> <!-- 数据库创建表的时候uid和id是关键字 --> <!-- 添加值的顺序要和数据库一样 --> insert into userinfo values(#{uname},#{uage},SEQ_SSM.nextval) </select> </mapper>
(三)创建dao接口
//不需要dao的实现类,通过代理生成 public interface IUserInfoDAO { //添加用户数据 public void add(UserInfo info); }
注意点:不需要dao的实现类,通过代理生成
(四)创建service
ServiceDao接口
public interface IUserInfoService { //添加用户 public void add(UserInfo info); }
ServiceDaoImpl实现
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; } }
(五)配置Controller
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; } }
(六)配置application.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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 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-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd " > <!-- 01.配置数据源 --> <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> </bean> <!-- 1.1 关联jdbc.properties --> <context:property-placeholder location="classpath:jdbc.properties" /> <!-- 02.配置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> <!-- 03.生成dao代理對象 --> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="sqlSessionFactoryBeanName" value="sessionFactory"></property> <property name="basePackage" value="cn.yxj.dao"></property> </bean> <!--04.配置service --> <bean id="userService" class="cn.yxj.service.UserInfoServiceImpl"> <property name="dao" ref="IUserInfoDAO"></property> </bean> <!-- 05.配置action --> <bean id="/userAction.do" class="cn.yxj.controller.UserInfoController"> <property name="service" ref="userService"></property> </bean> <!-- 06.配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"></property> </bean> <!-- 07.配置开启事务操作 --> <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>
(七)配置jdbc.properties
driverClass=oracle.jdbc.driver.OracleDriver jdbcUrl=jdbc\:oracle\:thin\:@localhost\:1521\:orcl user=T2 password=T2
(八)配置 mybatis-config.xml
<?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.yxj.entity"/> </typeAliases> --> <mappers> <mapper resource="cn/yxj/entity/IUserInfoDAO.xml" /> </mappers> </configuration>
(九)配置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> <!-- 1表示正常初始化配置文件 init-param 0、-1相当于没有设置 --> <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>
(十)配置页面
<%@ 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%>"> <style type="text/css"> form{ margin:0px auto; border:1px solid red; width:500px; padding:20px; } </style> <title></title> </head> <body> <h1>SSM整合</h1> <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>