SSM(Maven集成)


ssm全稱:Spring+SpringMVC+Mybatishtml

ssm簡介:java

一、Spring

        Spring是一個開源框架,Spring是於2003 年興起的一個輕量級的Java 開發框架,由Rod Johnson 在其著做Expert One-On-One J2EE Development and Design中闡述的部分理念和原型衍生而來。它是爲了解決企業應用開發的複雜性而建立的。Spring使用基本的JavaBean來完成之前只可能由EJB完成的事情。然而,Spring的用途不只限於服務器端的開發。從簡單性、可測試性和鬆耦合的角度而言,任何Java應用均可以從Spring中受益。 簡單來講,Spring是一個輕量級的控制反轉(IoC)和麪向切面(AOP)的容器框架。web

二、SpringMVC

    Spring MVC屬於SpringFrameWork的後續產品,已經融合在Spring Web Flow裏面。Spring MVC 分離了控制器、模型對象、分派器以及處理程序對象的角色,這種分離讓它們更容易進行定製。spring

三、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對象)映射成數據庫中的記錄。sql


建立步驟以下:數據庫

1、使用IntelliJ IDEA 建立Maven管理的JAVA Web項目express

①新建項目,選擇Maven Moduleapache

 

在本地磁盤用戶的。.m2目錄下生成settings文件:api

 

 

Maven工程搭建完畢!!!服務器


idea嚴格目錄區分:main下必須書寫java和resources,test也如此。


 

② 開發步驟:

書寫pom.xnl下載所需jar

<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.hq</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>
    <!--junit配置-->
    <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-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>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>4.2.3.release</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>

 

3.因爲Maven對oracle數據庫的不支持,咱們須要將Oracle裝載到本地倉庫:

 (1).查詢Oracle安裝目錄:D:\app\asus\product\11.2.0\dbhome_1\jdbc\lib

    配置Maven3.3.9 bin到環境變量

而後能夠經過cmd命令行 mvn -version測試有沒有測試成功!!!

(2).定位到Oracle安裝目錄的jdbc文件夾

 


4.搭建分層架構---實現添加學生

 


實體類UserInfo:

package cn.hq.entity;
 
/**
 * Created by hq 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;
    }
}
 dao接口: 無需dao實現類,經過代理生成

public interface IUserInfoDao {
    public void add(UserInfo info);
 
}
 service層:

public interface IUserInfoService {
 
    public void add(UserInfo info);
}
 service實現類:

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 httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
 
        String uname=httpServletRequest.getParameter("uname");
        Integer uage=Integer.valueOf(httpServletRequest.getParameter("uage"));
 
        UserInfo info=new UserInfo();
        info.setName(uname);
        info.setAge(uage);
 
        service.add(info);
 
        return new ModelAndView("/welcome.jsp");
    }
 
    public IUserInfoService getService() {
        return service;
    }
 
    public void setService(IUserInfoService service) {
        this.service = service;
    }
}

因爲是整合MyBatis,須要書寫核心配置文件:

小配置:UserInfoDao.xml: 

<?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.hq.dao.IUserInfoDao">
<select id="add" parameterType="UserInfo">
insert into userinfo values(SEQ_Num.nextval,#{name},#{age})
</select>
 
</mapper>

MyBatis配置文件: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.hq.entity"/>
</typeAliases>
 
<mappers>
 
<package name="cn.hq.dao"/>
 
</mappers>
</configuration>

 applicationContext.xml:進行植入dao、service、controller、事務管理器、數據源、sessionFactory 

 

<?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="mapperLocations" value="classpath:cn/hq/dao/IUserInfoDAO.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.hq.dao"></property>
    </bean>
    
    <!--04.配置service-->
     <bean id="userService" class="cn.hq.service.UserInfoServiceImpl">
        <property name="dao" ref="IUserInfoDAO"></property>
    </bean>
 
    <!-- 05.配置action -->
     <bean id="/userAction.do" class="cn.hq.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>

index.jsp頁面:

<body>
<form action="${pageContext.request.contextPath }/userAction.do" method="post">
    用戶名:<input name="uname"/> <br/><br/>
    年齡:<input name="uage"/><br/><br/>
    <input type="submit" value="add"/>
</form>
</body>

 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>

實現效果:

 


注:若出現此類異常--org.apache.ibatis.binding.BindingException: Invalid bound statement (not found)

解決方案:參見博客

http://www.cnblogs.com/jstarseven/p/5803697.html

http://blog.csdn.net/shifangwannian/article/details/48882201 

生活不止眼前的苟且,還有詩和遠方。
          ---高曉鬆
相關文章
相關標籤/搜索