SSM框架搭建

欢迎访问我的个人博客:www.ifueen.com

SSM框架搭建

导包

创建好web项目,然后导入相应的jar包

或者在pom.xml中加入依赖

<properties> 

        <!-- spring版本号 --> 

        <spring.version>4.0.2.RELEASE</spring.version> 

        <!-- mybatis版本号 --> 

        <mybatis.version>3.1.1</mybatis.version> 

        <!-- log4j日志文件管理包版本 --> 

        <slf4j.version>1.7.7</slf4j.version> 

        <log4j.version>1.2.17</log4j.version> 

    </properties> 

   

    <dependencies> 

        <dependency> 

            <groupId>junit</groupId> 

            <artifactId>junit</artifactId> 

            <version>4.11</version> 

            <!-- 表示开发的时候引入,发布的时候不会加载此包 --> 

            <scope>test</scope> 

        </dependency> 

        <!-- spring核心包 --> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-core</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

   

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-web</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-oxm</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-tx</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

   

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-jdbc</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

   

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-webmvc</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-aop</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

   

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-context-support</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

   

        <dependency> 

            <groupId>org.springframework</groupId> 

            <artifactId>spring-test</artifactId> 

            <version>${spring.version}</version> 

        </dependency> 

        <!-- mybatis核心包 --> 

        <dependency> 

            <groupId>org.mybatis</groupId> 

            <artifactId>mybatis</artifactId> 

            <version>${mybatis.version}</version> 

        </dependency> 

        <!-- mybatis/spring包 --> 

        <dependency> 

            <groupId>org.mybatis</groupId> 

            <artifactId>mybatis-spring</artifactId> 

            <version>1.1.0</version> 

        </dependency> 

        <!-- 导入java ee jar 包 --> 

        <dependency> 

            <groupId>javax</groupId> 

            <artifactId>javaee-api</artifactId> 

            <version>7.0</version> 

        </dependency> 

        <!-- 导入Mysql数据库链接jar包 --> 

        <dependency> 

            <groupId>mysql</groupId> 

            <artifactId>mysql-connector-java</artifactId> 

            <version>5.1.30</version> 

        </dependency> 

        <!-- 导入dbcp的jar包,用来在applicationContext.xml中配置数据库 --> 

        <dependency> 

            <groupId>commons-dbcp</groupId> 

            <artifactId>commons-dbcp</artifactId> 

            <version>1.2.2</version> 

        </dependency> 

        <!-- JSTL标签类 --> 

        <dependency> 

            <groupId>jstl</groupId> 

            <artifactId>jstl</artifactId> 

            <version>1.2</version> 

        </dependency> 

        <!-- 日志文件管理包 --> 

        <!-- log start --> 

        <dependency> 

            <groupId>log4j</groupId> 

            <artifactId>log4j</artifactId> 

            <version>${log4j.version}</version> 

        </dependency> 


        <!-- 格式化对象,方便输出日志 --> 

        <dependency> 

            <groupId>com.alibaba</groupId> 

            <artifactId>fastjson</artifactId> 

            <version>1.1.41</version> 

        </dependency> 

        <dependency> 

            <groupId>org.slf4j</groupId> 

            <artifactId>slf4j-api</artifactId> 

            <version>${slf4j.version}</version> 

        </dependency> 

        <dependency> 

            <groupId>org.slf4j</groupId> 

            <artifactId>slf4j-log4j12</artifactId> 

            <version>${slf4j.version}</version> 

        </dependency> 

        <!-- log end --> 

        <!-- 映入JSON --> 

        <dependency> 

            <groupId>org.codehaus.jackson</groupId> 

            <artifactId>jackson-mapper-asl</artifactId> 

            <version>1.9.13</version> 

        </dependency> 

        <!-- 上传组件包 --> 

        <dependency> 

            <groupId>commons-fileupload</groupId> 

            <artifactId>commons-fileupload</artifactId> 

            <version>1.3.1</version> 

        </dependency> 

        <dependency> 

            <groupId>commons-io</groupId> 

            <artifactId>commons-io</artifactId> 

            <version>2.4</version> 

        </dependency> 

        <dependency> 

            <groupId>commons-codec</groupId> 

            <artifactId>commons-codec</artifactId> 

            <version>1.9</version> 

        </dependency> 

    </dependencies>

  <build>

    <finalName>ssm01</finalName>

  </build>

配置核心文件

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:context="http://www.springframework.org/schema/context"
       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/context http://www.springframework.org/schema/context/spring-context.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
">
    <!-- 扫描service层 -->
    <context:component-scan base-package="com.ifueen.ssm.service"></context:component-scan>
    <!-- 配置数据库 -->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>
    <!--创建dataSource-->
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
        <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>
    <!-- SqlSessionFactory-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
        <property name="mapperLocations" value="classpath:com/ifueen/ssm/mapper/*Mapper.xml"></property>
        <property name="typeAliasesPackage">
            <value>
                com.ifueen.ssm.domain
                com.ifueen.ssm.query
            </value>
        </property>
    </bean>

    <!-- 扫面mapper 产生实现类 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.ifueen.ssm.mapper"></property>
    </bean>

    <!-- 配置事务 -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <!--开启事务注解的支持,默认会去找一个名称叫做transactionManager的事务管理器 -->
    <tx:annotation-driven transaction-manager="transactionManager" />


</beans>

applicationContext-mvc.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: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/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">
    <!--扫描controller-->
    <context:component-scan base-package="com.ifueen.ssm.controller" />
    <!--静态资源处理-->
    <mvc:default-servlet-handler />
    <!--识别@requestMapper等注解支持-->
    <mvc:annotation-driven />
    <!--配置视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
    </bean>

</beans>

web.xml

<?xml version="1.0" encoding="UTF-8" ?>
<web-app 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_3_1.xsd"
         version="3.1">

  <!-- 配置Spring -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>

  <!--1.2 配置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-mvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>springmvc</servlet-name>
    <!--/*会拦截所有包括jsp-->
    <!--/会拦截所有但是不包含jsp-->
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--
      post请求乱码解决
     get请求实在tomcat猫中配置
  -->
  <filter>
    <filter-name>EncodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>EncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

db.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=123456

准备三层架构

准备好相应的三层架构进行测试

Product

package com.ifueen.ssm.domain;

public class Product {

    private Long id;
    private String productName;
	//getset

}

Service层实现类

package com.ifueen.ssm.service.impl;

import com.ifueen.ssm.domain.Product;
import com.ifueen.ssm.mapper.ProductMapper;
import com.ifueen.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class ProductServiceImpl implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Override
    public List<Product> findAll() {
        return productMapper.findAll();
    }

}

mapper.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">

<!-- namespace表示命名空间  保证它是唯一   cn.itsource.mybatis.dao.impl.ProductDaoImpl + id="getUserById"-->
<mapper namespace="com.ifueen.ssm.mapper.ProductMapper">
    <!-- id名称和ProductMapper接口方法一样-->
    <select id="findAll" resultType="product">
        select * from product
    </select>


</mapper>

Controller类进行测试

package com.ifueen.ssm.controller;

import com.ifueen.ssm.domain.Product;
import com.ifueen.ssm.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private IProductService productService;


    @RequestMapping("/list")
    @ResponseBody
    public List<Product> queryAll(){
        return productService.findAll();
    }

}



然后就可以开启tomcat通过服务器访问了

发布了87 篇原创文章 · 获赞 8 · 访问量 2万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览