您的位置:首页 > 编程语言 > Java开发

eclipse下配置安装ssm图文教程

2017-05-15 17:24 537 查看
eclipse下配置安装ssm图文教程(web版)

一、安装所需jar包

1.1  mybatis安装包

 


 

可以进入GitHub的https://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-validator(springMVC校验所需包)

 

1.4 ssm的框架间的连接包或扩展包

 


springMVC实现json交互所需jar包:与下方的jar任选其一

 


文件上传的jar包:

 


Mybatis和spring连接,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(*包括:dao、service、transaction)的头部信息

配置如下:

<?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管理mybatis的dao配置文件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管理service的service配置文件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  配置log4j的properties配置文件

配置如下:

# 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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: