基本概念css
使用SSM(Spring,SpringMVC和Mybatis)html
Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著做Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是爲了解決企業應用開發的複雜性而建立的。Spring使用基本的JavaBean來完成之前只可能由EJB完成的事情。然而,Spring的用途不只限於服務器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用均可以從Spring中受益。 簡單來講,Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器框架。java
Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裏面。Spring MVC 分離了控制器、模型對象、分派器以及處理程序對象的角色,這種分離讓它們更容易進行定製。mysql
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對象)映射成數據庫中的記錄。web
2、根據圖來理解使用SSM添加數據spring
一、首先使用idea建立一個maven項目sql
2.在彈出的窗體中選擇maven,而後勾選要建的maven模板--這裏選webApp數據庫
3.而後填入相應的maven項目組信息(這個是比較隨意的)express
這裏填寫本身的maven本地倉庫路徑apache
maven會自動建立須要的一些配置信息以及目錄結構,在這段時間裏咱們能夠查找須要的jar包並在maven配置文件pom.xml裏面進行配置,見下面步驟:
這裏若是不知道要用到什麼jar包就去百度SpringMVC須要的jar包,而後在maven的官方連接單獨查找jar包來配置pom.xml,實例以下(這裏我示範去maven官網查找xml配置的部分,具體須要的jar包去我後面展現的pom.xml裏面查找):
登陸http://mvnrepository.com/ 示例查找spring-beans
點擊查找結果
這裏能夠看到最新版以及使用人數最多的版本,本身選擇--配置文件裏面的jar包版本最好選擇同一個版本避免版本衝突;
咱們選擇第一個進入網頁,裏面能夠看到maven的配置pom.xml文件寫法,點擊代碼直接複製(自動複製);
而後將複製的代碼拷貝到pom.xml文件中去便可,maven會自動下載所須要的jar包,咱們不須要理會
maven pom.xml,maven會自動到庫裏面下載須要的jar包到maven倉庫
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.jpp</groupId> <artifactId>SpringMVC_SSM</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <name>SpringMVC_SSM Maven Webapp</name> <url>http://maven.apache.org</url> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>3.8.1</version> <scope>test</scope> </dependency> <dependency> <groupId>javaee</groupId> <artifactId>javaee-api</artifactId> <version>5</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aspects</artifactId> <version>4.2.1.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>4.2.3.release</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.3.RELEASE</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>4.1.9.RELEASE</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.2.2</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>1.3.0</version> </dependency> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.17.1-GA</version> </dependency> <dependency> <groupId>cglib</groupId> <artifactId>cglib</artifactId> <version>2.2.2</version> </dependency> <dependency> <groupId>c3p0</groupId> <artifactId>c3p0</artifactId> <version>0.9.1.2</version> </dependency> <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc6</artifactId> <version>11.2.0.1.0</version> </dependency> </dependencies> <build> <finalName>SpringMVC_SSM</finalName> </build> </project>
等系統構建完成之後,咱們就能夠看到目錄結構
不全的能夠按照需求來補全文件結構,maven項目中的文件結構分爲Sources,Tests,Resources,Test Resources,Excluded幾種,咱們須要詳細區分各個文件夾的類型:
配置SpringMVC
一切就緒後接下來咱們繼續配置SpringMVC的具體信息:
首先須要配置Web.xml這個沒必要多說,網站項目運行第一個加載的就是web.xml,進入
src->main->webapp->WEB-INF->web.xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <!-- 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> </web-app>
而後在Resources資源文件夾下新建applicationContext.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="${jdbc.driverClass}"></property> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property> <property name="user" value="${jdbc.user}"></property> <property name="password" value="${jdbc.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.jpp.dao"></property> </bean> <!--04.配置service--> <bean id="userService" class="cn.jpp.service.UserInfoServiceImpl"> <property name="dao" ref="IUserInfoDAO"></property> </bean> <!-- 05.配置action --> <bean id="/userAction.do" class="cn.jpp.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
jdbc.driverClass=oracle.jdbc.driver.OracleDriver
jdbc.jdbcUrl=jdbc\:oracle\:thin\:@localhost\:1521\:orcl
jdbc.user=system
jdbc.password=1
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.jpp.entity"/> </typeAliases> <mappers> <!--<mapper resource="cn/happy/entity/IUserInfoDAO.xml" /> --> <package name="cn.jpp.dao"/> </mappers> </configuration>
配置實體
package cn.jpp.entity; /** * Created by 景佩佩 on 2017/1/16. */ public class UserInfo { private Integer id; private String name; private Integer age; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
配置實體的小配置(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.jpp.dao.IUserInfoDAO"> <select id="add" parameterType="UserInfo"> insert into userinfo values(SEQ_NUM.nextval,#{name},#{age}) </select> </mapper>
建立dao接口
package cn.jpp.dao; import cn.jpp.entity.UserInfo; /** * Created by 景佩佩 on 2017/1/16. */ public interface IUserInfoDAO { public void add(UserInfo info); }
提醒:不須要dao的實現類,經過代理生成
建立service
ServiceDao接口
package cn.jpp.service; import cn.jpp.entity.UserInfo; /** * Created by 景佩佩 on 2017/1/16. */ public interface IUserInfoService { public void add(UserInfo info); }
ServiceDaoImpl實現
package cn.jpp.service; import cn.jpp.dao.IUserInfoDAO; import cn.jpp.entity.UserInfo; /** * Created by 景佩佩 on 2017/1/16. */ 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
package cn.jpp.controller; import cn.jpp.entity.UserInfo; import cn.jpp.service.IUserInfoService; import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.mvc.Controller; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by 景佩佩 on 2017/1/16. */ public class UserInfoController implements Controller { private IUserInfoService service; public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception { String uname=request.getParameter("name"); Integer uage=Integer.valueOf(request.getParameter("age")); UserInfo info=new UserInfo(); info.setAge(uage); info.setName(uname); service.add(info); return new ModelAndView("/welcome.jsp"); } public IUserInfoService getService() { return service; } public void setService(IUserInfoService service) { this.service = service; } }
配置頁面
<%@ 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; width:500px; padding:20px; } </style> <title></title> </head> <body> <h1>SSM整合</h1> <form action="${pageContext.request.contextPath }/userAction.do" method="post"> 用戶名:<input name="name"/> <br/> 用戶年齡<input name="age"/><br/> <input type="submit" value="save"/> </form> </body> </html>
將該變量添加到Path中
若是想要修改Maven的本地倉庫位置,則能夠直接在Maven的安裝目錄下找到conf文件下的setting配置文件中,設置localRepository爲本地倉庫位置
從新打開命令提示符cmd(管理員),輸入mvn --version ,如圖所示,則說明安裝成功
首先咱們也要有Oracle數據庫,將Oracle裝載到本地倉庫
咱們先找到F:\app\happy\product\11.2.0\dbhome_1\jdbc\lib
並在pox.xml在配置Oracle
而且建立Oracle表
-- Create table create table USERINFO ( ID NUMBER not null, NAME NVARCHAR2(32) not null, AGE NUMBER not null )
可能還會報一個序列不存在的錯,建立序列
-- Create sequence create sequence SEQ_Num minvalue 1 maxvalue 9999999999999999999999999999 start with 81 increment by 1 cache 20;
得跟實體的配置的系列一致
所有以後呢,加入到Tomact中
運行後,在瀏覽器地址欄輸入:http://localhost:8080/SSM
數據庫數據