eclipse下配置安裝ssm圖文教程(web版)


eclipse下配置安裝ssm圖文教程(web版)

一、安裝所需jar

1.1  mybatis安裝包

 

 

可以進入GitHubhttps://github.com/mybatis/mybatis-3/releases下載所需版本,本文版本是mybatis-3.4.2

Mybatis實現緩存的jar:

 

1.2  spring安裝包

 

補充:spring配置aop的aspect包

 

可以進入spring官網的http://projects.spring.io/spring-framework/

下載所需版本,本文版本是spring-framework-4.3.0.RELEASE

 

1.3  springMVC安裝包

 

springMVC是以spring應用為基礎的,沒有官網提供下載,可以百度下載。

 

hibernate-validatorspringMVC校驗所需包)

 

1.4 ssm的框架間的連接包或擴展包

 

springMVC實現json交互所需jar:與下方的jar任選其一

 

文件上傳的jar:

 

Mybatisspring連接,mysql數據庫連接的jar:

 

 

特別注意:可能出現相同的包導入多個版本引發沖突,請根據自己的情況保留一個合適的版本

 

二、安裝步驟

2.1 創建動態web項目

項目結構如下:

 

 

2.2  創建並配置mybatis的核心配置文件SqlMapConfig.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>  

<!--全局setting配置,根據需要添加-->

 

 

 

<!-- With this configuration, can now be used anywhere that

a package where MyBatis will search for beans -->

<!-- 配置別名 -->

<typeAliases>  

 <!-- 批量掃描別名 -->

 <package name="com.test.ssm.custom"/> 

</typeAliases>

 

<!-- Register all interfaces in a package as mappers -->

<!-- 配置mapper

 由於使用spring和mybatis的整合包進行mapper掃描,這里就不需要配置了。

 必須遵循:mapper.xml和mapper接口必須同名且在同一目錄下

 --> 

<!--

<mappers>  

<package name="org.mybatis.builder"/>

</mappers> -->

</configuration>

 

2.3  創建spring的配置spring-*.xml*包括:daoservicetransaction)的頭部信息

配置如下:

<?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"

    xmlns:mvc="http://www.springframework.org/schema/mvc"

    xsi:schemaLocation="

        http://www.springframework.org/schema/beans

        http://www.springframework.org/schema/beans/spring-beans.xsd

        http://www.springframework.org/schema/tx

        http://www.springframework.org/schema/tx/spring-tx.xsd

        http://www.springframework.org/schema/aop

        http://www.springframework.org/schema/aop/spring-aop.xsd

        http://www.springframework.org/schema/context

        http://www.springframework.org/schema/context/spring-context.xsd

        http://www.springframework.org/schema/mvc

        http://www.springframework.org/schema/mvc/spring-mvc.xsd">

        

</beans>

 

2.4  配置spring管理mybatisdao配置文件spring-dao.xml

配置(不含頭部信息)如下:

<!-- 加載指定properties文件 -->

<context:property-placeholder location="classpath:db.properties" />

 

<!-- 配置數據庫連接 -->

<!-- 配置數據源dbcp -->

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"

destroy-method="close">

<!-- results in a setDriverClassName(String) call -->

<property name="driverClassName" value="${jdbc.driverClassName}" />

<property name="url" value="${jdbc.url}" />

<property name="username" value="${jdbc.username}" />

<property name="password" value="${jdbc.password}" />

</bean>

 

<!-- 加載指定的properties文件 -->

    <context:property-placeholder location="classpath:db.properties"/>

 

<!-- 配置sqlSessionFactory -->

<bean id="sqlSessionFactory"  class="org.mybatis.spring.SqlSessionFactoryBean">

  <!-- 數據庫連接池 -->

  <property name="dataSource" ref="dataSource"/>

  <!-- 加載mybatis的全局配置文件 -->

  <property name="configLocation" value="classpath:mybatis/SqlMapConfig.xml"/>

</bean>

 

<!-- mapper掃描器 -->

<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">

<!-- 掃描包路徑,如果需要掃描多個包,中間使用使用半角逗號隔開 -->

<property name="basePackage" value="com.test.ssm.mapper,com.test.ssm.mapper2" />

<!-- 此屬性在對應的類中定義存在為String類型,所傳入值為String類型 -->

<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />

<!-- 下方的引用bean會失敗是原因:在org.mybatis.spring.mapper.MapperScannerConfigurer

中的屬性sqlSessionFactory類型為org.apache.ibatis.session.SqlSessionFactory,而 依賴的sqlSessionFactory的類型為org.mybatis.spring.SqlSessionFactoryBean,

兩者的類型不一致,所有無法依賴 -->

<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory"/> -->

</bean>

 

補充:db.properties配置如下:

jdbc.driverClassName = com.mysql.jdbc.Driver

jdbc.url = jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8

jdbc.username = root

jdbc.password =

 

2.5  配置spring管理serviceservice配置文件spring-service.xml

配置(不含頭部信息)如下:

 <!-- 可以掃描controller、service、... 這里掃描controller,指定serviceImpl的包 -->

   <context:component-scan base-package="com.test.ssm.serviceImpl"></context:component-scan>

   

   <!-- 商品管理的Service -->

  <!--  <bean id="itemsService" class="com.test.ssm.serviceImpl.ItemsServiceImpl"/> -->

 

2.6  配置spring管理事務的transaction配置文件spring-transaction.xml

配置(不含頭部信息)如下:

 <!-- 事務管理器

           mybatis操作數據庫事務控制,spring使用JDBC的事務控制

    -->

   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

     <!-- 數據源

      dataSource在applicationContext-dao.xml配置了

      -->

     <property name="dataSource" ref="dataSource"/>

   </bean>

   

   <!-- 通知給指定的事務管理器transactionManager -->

   <tx:advice id="txAdvice" transaction-manager="transactionManager">

     <tx:attributes>

      <!-- 傳播行為 -->

      <tx:method name="save*" propagation="REQUIRED"/>

      <tx:method name="delete*" propagation="REQUIRED"/>

      <tx:method name="insert*" propagation="REQUIRED"/>

      <tx:method name="update*" propagation="REQUIRED"/>

      <tx:method name="get*" propagation="SUPPORTS" read-only="true"/>

      <tx:method name="select*" propagation="SUPPORTS" read-only="true"/>

     </tx:attributes>

   </tx:advice>

   

   <!-- aop調用 txAdvice-->

   <aop:config>

     <aop:advisor advice-ref="txAdvice" pointcut="execution(* com.test.ssm.serviceImpl.*.*(..))"/>

   </aop:config>

 

2.7  創建並配置springMVC的配置文件springMVC.xml

配置如下:

 <!-- 可以掃描controller、service、... 這里掃描controller,指定controller的包 -->

<context:component-scan base-package="com.test.ssm.controller" />

  

  <!-- 注解的映射器 -->

<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/> -->

<!-- 注解的適配器 -->

<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->

 

<!-- 使用mvc:annotation-driven可以代替注解的適配器和注解映射器 mvc:annotation-driven默認加載了很多的參數綁定方法,比如json轉換解析器就默認加載了

實際開發時使用下方的mvc:annotation-driven -->

  <mvc:annotation-driven></mvc:annotation-driven>

  

  <!-- 視圖解析器 (ViewResolver) 解析jsp視圖,默認使用jstl標簽,classpath下要有jstl的包 -->

  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

    <!-- 定義視圖前綴和后綴 -->

    <property name="prefix" value="/WEB-INF/jsp/"/>

    <property name="suffix" value=".jsp"/>

  </bean>

 

2.8  配置web容器web.xml

配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

  <display-name>ssmTest2</display-name>

  <welcome-file-list>

    <welcome-file>index.html</welcome-file>

    <welcome-file>index.htm</welcome-file>

    <welcome-file>index.jsp</welcome-file>

    <welcome-file>default.html</welcome-file>

    <welcome-file>default.htm</welcome-file>

    <welcome-file>default.jsp</welcome-file>

  </welcome-file-list>

  

  <!-- 配置前端控制器 -->

  <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:spring/springmvc.xml</param-value>

   </init-param>

  </servlet>

  <!-- 配置前端控制器映射器 -->

  <servlet-mapping>

   <servlet-name>springMVC</servlet-name>

   <url-pattern>*.action</url-pattern>

  </servlet-mapping>

  

  <!-- 加載spring容器 -->

  <context-param>

   <param-name>contextConfigLocation</param-name>

   <param-value>classpath:spring/applicationContext-*.xml</param-value>

  </context-param>

  <listener>

   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

  </listener>

  

</web-app>

2.9  配置log4jproperties配置文件

配置如下:

# Global logging configuration

log4j.rootLogger=DEBUG, stdout

# MyBatis logging configuration...

log4j.logger.org.mybatis.example.BlogMapper=TRACE

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

 

2.10  創建補全項目結構

項目結構如下:

 

 

2.11 使用tomcat運行測試

測試類似如下:

 

 

 

三、項目實例測試

3.1 需求分析

根據商品id獲取商品信息

 

3.2 確定動態代理方法和配置文件

 

上方的mapper包是通過逆向代碼工程生成的

ItemsMapper2.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="com.test.ssm.mapper2.ItemsMapper2" >

 <select id="getItemsById" parameterType="int" 

  resultType="itemsCustom">

   select * from items it where it.id=${value}

 </select>

</mapper>

 

3.3 根據ItemsMapper2.xml的配置文件書寫ItemsMapper2的借口方法

方法如下:

package com.test.ssm.mapper.mapper2;

 

import com.test.ssm.custom.ItemsCustom;

 

/**

 * 商品代理對象類

 * @author 

 * 2017/3/16

 */

public interface ItemsMapper2 {

  //根據商品id獲取商品信息

  public ItemsCustom getItemsById(int id);

}

 

3.4  ItemsService調用和ItemServiceImplement方法實現

ItemsService調用:

package com.test.ssm.service;

 

import com.test.ssm.custom.ItemsCustom;

 

/**

 * 商品對象業務接口類

 * @author 

 * 2017/3/16

 */

public interface ItemsService {

//根據商品id獲取商品信息

public ItemsCustom getItemsById(int id);

}

 

ItemServiceImplement方法實現:

package com.test.ssm.serviceImpl;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import com.test.ssm.custom.ItemsCustom;

import com.test.ssm.mapper2.ItemsMapper2;

import com.test.ssm.service.ItemsService;

 

/**

 * 商品對象業務實現類

 * @author

 * 2017/3/16

 */

@Service("itemsService")

public class ItemsServiceImpl implements ItemsService {

@Autowired

ItemsMapper2 itemsMapper2;

//根據商品id獲取商品信息

@Override

public ItemsCustom getItemsById(int id) {

//調用Items代理對象的getItemsById(id)方法

return itemsMapper2.getItemsById(id);

}

 

}

 

3.5 前端控制部分ItemsController實現

ItemsController實現:

package com.test.ssm.controller;

 

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

 

import com.test.ssm.custom.ItemsCustom;

import com.test.ssm.service.ItemsService;

 

/**

 * 商品控制類

 * @author 龍

 * 2017/3/16

 */

@Controller

@RequestMapping("/items")

public class ItemsController {

@Autowired

    ItemsService itemsService;

 

@RequestMapping("/itemsSerach")

public String ItemsCustomSearch(Model model,Integer id){

ItemsCustom itemsCustom = itemsService.getItemsById(id);

model.addAttribute("ItemsCustom",itemsCustom);

return "items/itemsTest";

}

}

 

3.6 前端顯示頁面itemsTest.jsp

itemsTest.jsp內容如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"

    pageEncoding="UTF-8"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<title>商品信息測試類</title>

</head>

<body>

<table>

 <tr>

  <td>商品名稱</td>

  <td>商品價格</td>

  <td>生產日期</td>

  <td>商品描述</td>

  <td>操作</td>

 </tr>

<c:if test="${ItemsCustom != null }">

  <tr>

   <td><input type="text" name="name" id="itemsName" value="${ItemsCustom.name }"/></td>

   <td><input type="text" name="price" id="itemsPrice" value="${ItemsCustom.price }"/></td>

   <td><input type="text" name="createtime" id="itemsDate" 

   value="<fmt:formatDate value='${ItemsCustom.createtime}' pattern='yyyy-MM-dd'/>"/></td>

   <td><input type="text" name="detail" id="itemsDetail" value="${ItemsCustom.detail }"/></td>

   <td><input type="text" name="pic" id="itemsPic" value="${ItemsCustom.pic }"/></td>

  </tr>

</c:if>

</table>

</body>

</html>

 

3.7 測試成功

測試實例:

http://localhost:8080/ssmTest2/items/itemsSerach.action?id=10527

測試結果:

 

以上為個人搭建ssm測試實例,如有不足還請賜教!

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM