一、基本概念
使用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>
可以運行了,但是數據庫要有UserInfo這個表。