SSM项目搭建

环境:jdk1.8.0_171,IntelliJ IDEA 2018.1.4,apache-maven-3.3.9,MySQL Server 5.5
(可以翻阅我之前的文章有安装教程)

一、创建maven项目

1.1创建maven工程
在这里插入图片描述
1.2指定项目坐标
在这里插入图片描述
1.3创建项目的保存地址
在这里插入图片描述
1.4指定maven本地仓库地址
在这里插入图片描述
1.5配置maven参数 (参数作用:所有资源尽可能都先从本地仓库查找,只有很少数据才会去网络进行下载。)
在这里插入图片描述
1.6选择自动导入
在这里插入图片描述

二、编写pom.xml:下载所需要的依赖包

 <dependencies> 
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-context</artifactId>  
      <version>5.0.2.RELEASE</version> 
    </dependency>  
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-webmvc</artifactId>  
      <version>5.0.2.RELEASE</version> 
    </dependency>  
    <dependency> 
      <groupId>org.aspectj</groupId>  
      <artifactId>aspectjweaver</artifactId>  
      <version>1.8.7</version> 
    </dependency>  
    <dependency> 
      <groupId>mysql</groupId>  
      <artifactId>mysql-connector-java</artifactId>  
      <version>5.1.30</version> 
    </dependency>  
    <dependency> 
      <groupId>com.alibaba</groupId>  
      <artifactId>druid</artifactId>  
      <version>1.1.10</version> 
    </dependency>  
    <dependency> 
      <groupId>javax.servlet</groupId>  
      <artifactId>servlet-api</artifactId>  
      <version>2.5</version> 
    </dependency>  
    <dependency> 
      <groupId>javax.servlet</groupId>  
      <artifactId>jsp-api</artifactId>  
      <version>2.0</version> 
    </dependency>  
    <dependency> 
      <groupId>log4j</groupId>  
      <artifactId>log4j</artifactId>  
      <version>1.2.17</version> 
    </dependency>  
    <dependency> 
      <groupId>junit</groupId>  
      <artifactId>junit</artifactId>  
      <version>4.12</version> 
    </dependency>  
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-test</artifactId>  
      <version>5.0.2.RELEASE</version> 
    </dependency>  
    <dependency> 
      <groupId>jstl</groupId>  
      <artifactId>jstl</artifactId>  
      <version>1.2</version> 
    </dependency>  
    <dependency> 
      <groupId>org.mybatis</groupId>  
      <artifactId>mybatis</artifactId>  
      <version>3.4.5</version> 
    </dependency>  
    <dependency> 
      <groupId>org.mybatis</groupId>  
      <artifactId>mybatis-spring</artifactId>  
      <version>1.3.1</version> 
    </dependency>  
    <!--事务配置 -->  
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-tx</artifactId>  
      <version>5.0.2.RELEASE</version> 
    </dependency>  
    <dependency> 
      <groupId>org.springframework</groupId>  
      <artifactId>spring-jdbc</artifactId>  
      <version>5.0.2.RELEASE</version> 
    </dependency> 
  </dependencies> 

三、单独 Spring 环境(用Service测试)

(测试IOC注入,可以注入,证明单独的Spring环境写好了)
结构:1实体类,2逻辑层,3接口,4spring配置文件,5测试类

在这里插入图片描述
1.LOLUser

package huanmingjie.fengmingmen.module.user.domain;

public class LOLUser {
    private String name;
    private String sex;

    public LOLUser() {
    }

    public LOLUser(String name, String sex) {
        this.name = name;
        this.sex = sex;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }
}

2.逻辑层(注意加上@Service注解,不然spring发现不了)

package huanmingjie.fengmingmen.module.user.service;

import huanmingjie.fengmingmen.module.user.domain.LOLUser;
import huanmingjie.fengmingmen.service.user.ILOLUserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LOLUserService implements ILOLUserService {
    public List<LOLUser> findAll() {
        System.out.println("单独的Spring,测试IOC容器注入");
        return null;
    }
}

3.接口

package huanmingjie.fengmingmen.service.user;

import huanmingjie.fengmingmen.module.user.domain.LOLUser;

import java.util.List;

public interface ILOLUserService {
    List<LOLUser> findAll();
}

4.spring配置文件(配置扫描逻辑层)

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

    <!--扫描service-->
    <context:component-scan base-package="huanmingjie.fengmingmen.module.user.service">
    </context:component-scan>


</beans>

5.测试类(注入逻辑层,实行调用测试)
流程:通过@ContextConfiguration(“classpath:bean.xml”)找到spring配置类,然后通过扫描@service找到逻辑层的类,测试成功证明ioc注入成功,spring单独环境完成。

package huanmingjie.fengmingmen.module.user;


import huanmingjie.fengmingmen.service.user.ILOLUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class LOLTest {

    @Autowired
    private ILOLUserService lolUserService;

    @Test
    public void test() {
        lolUserService.findAll();
    }


}

6.测试
在这里插入图片描述

四、配置项目

在这里插入图片描述
1.(Project)配置项目的JDK,项目编译文件class地址
在这里插入图片描述
2.(Modules)指定java类,资源文件,模块编译文件calss地址
在这里插入图片描述
在这里插入图片描述

3.(Libraries)maven自动引入依赖包,不用处理
在这里插入图片描述
4.(facets),指定静态资源的访问路径,web.xml在下面的单独springMVC,这里可以先跳过,待会补上
在这里插入图片描述
在这里插入图片描述
5.artifacts
在这里插入图片描述
在这里插入图片描述
6.settings指定utf-8编码
在这里插入图片描述
在这里插入图片描述
7.指定版本8在这里插入图片描述

五、单独 SpringMVC 环境(用Controller测试)

(用于和前端交互,数据交互成功,证明SpringMVC 环境完成)
结构:1 web.xml,2 springMVC,3 LOLUserController,4 lol.jsp, 5.配置tomcat

在这里插入图片描述

1.web.xml(配置前端控制器:拦截前端的东西交给springMVC处理,编码过滤器)

(利用插件JBLJavaToWeb生成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_2_5.xsd"
         version="2.5">

    <!--springMVC配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--springMVC配置编码过滤器-->
    <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>
  1. springMVC.xml(扫描controller,SpringMVC视图解析器:处理controller返回的东西进行处理,注解支持)
<?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/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context.xsd
      ">

    <!--扫描controller-->
    <context:component-scan base-package="huanmingjie.fengmingmen.module.user.controller">
    </context:component-scan>

    <!--SpringMVC视图解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/pages/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--SpringMVC注解支持-->
    <mvc:annotation-driven></mvc:annotation-driven>

</beans>

  1. LOLUserController (@Controller)
package huanmingjie.fengmingmen.module.user.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/lolUser")
public class LOLUserController {


    @RequestMapping("/all")
    public String findAll() {
        System.out.println("SpringMVC也OK了");
        return "lol";
    }
    
}


4.lol.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>lol</title>
</head>
<body>
        lol
</body>
</html>

5.配置tomcat
在这里插入图片描述6.测试:
在这里插入图片描述
在这里插入图片描述

六、Spring 与 SpringMVC 整合

1 web.xml 配置监听器 2 controller 中注入 service

  1. 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_2_5.xsd"
         version="2.5">

    <!--配置监听器-->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!--监听bean.xml-->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:bean.xml</param-value>
    </context-param>

    <!--springMVC配置前端控制器-->
    <servlet>
        <servlet-name>dispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:springMVC.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcherServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!--springMVC配置编码过滤器-->
    <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>

2.controller 中注入 service

package huanmingjie.fengmingmen.module.user.controller;

import huanmingjie.fengmingmen.service.user.ILOLUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/lolUser")
public class LOLUserController {

	//注入 service 
    @Autowired
    private ILOLUserService lolUserServicel;

    @RequestMapping("/all")
    public String findAll() {
    	//调用service方法 
        lolUserServicel.findAll();
        return "lol";
    }

}

编译后测试(或者直接删除classes,target再运行tomcat):
在这里插入图片描述
在这里插入图片描述

七、单独 Mybatis 环境

1 SqlMapConfig.xml 2 ILOLUserDao 接口 3 ILOLUserDao.xml 4 test

在这里插入图片描述
1.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>

    <!--指定mysql环境-->
    <environments default="mysql">
        <environment id="mysql">
            <!--配置事务-->
            <transactionManager type="JDBC"></transactionManager>
            <!--配置数据源-->
            <dataSource type="POOLED">
                <property name="driver" value="com.mysql.jdbc.Driver"></property>
                <property name="url" value="jdbc:mysql://localhost:3306/huanmingjie?characterEncoding=UTF8"></property>
                <property name="username" value="root"></property>
                <property name="password" value="root"></property>
            </dataSource>
        </environment>
    </environments>

    <!--加载映射文件-->
    <mappers>
        <mapper resource="huanmingjie/fengmingmen/dao/user/ILOLUserDao.xml"></mapper>
    </mappers>

</configuration>
  1. ILOLUserDao
package huanmingjie.fengmingmen.dao.user;

import huanmingjie.fengmingmen.module.user.domain.LOLUser;

import java.util.List;

public interface ILOLUserDao {
    List<LOLUser> findAll();
}

3.ILOLUserDao.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="huanmingjie.fengmingmen.dao.user.ILOLUserDao">

    <select id="findAll" resultType="huanmingjie.fengmingmen.module.user.domain.LOLUser">
         select * from lol_user
    </select>

</mapper>

4.test

package huanmingjie.fengmingmen.dao;

import huanmingjie.fengmingmen.dao.user.ILOLUserDao;
import huanmingjie.fengmingmen.module.user.domain.LOLUser;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class LOLUserDaoTest {

    public static void main(String[] args) throws IOException {
        //输入流读取文件(交给IOC)
        InputStream inputStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory sqlSessionFactory = builder.build(inputStream);
        SqlSession session = sqlSessionFactory.openSession();
        ILOLUserDao loluserDao = session.getMapper(ILOLUserDao.class);
        System.out.println(loluserDao.getClass());
        List<LOLUser> list = loluserDao.findAll();
        for (LOLUser loluser : list) {
            System.out.println(loluser.getName());
        }
        session.close();
        inputStream.close();
    }
}

在这里插入图片描述

八、Spring 整合 Mybatis

1 service (注入dao) 2 bean.xml 3 jdbc.properties 4 test

在这里插入图片描述

  1. service (注入dao)
package huanmingjie.fengmingmen.module.user.service;

import huanmingjie.fengmingmen.dao.user.ILOLUserDao;
import huanmingjie.fengmingmen.module.user.domain.LOLUser;
import huanmingjie.fengmingmen.service.user.ILOLUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class LOLUserService implements ILOLUserService {

    @Autowired
    private ILOLUserDao loluserDao;

    public List<LOLUser> findAll() {
        return loluserDao.findAll();
    }
}

2.bean.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:contet="http://www.springframework.org/schema/context"
       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
       http://www.springframework.org/schema/aop
       http://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--扫描service-->
    <context:component-scan base-package="huanmingjie.fengmingmen.module.user.service">
    </context:component-scan>

    <!--加载 jdbc.properties -->
    <contet:property-placeholder location="classpath:jdbc.properties" ></contet:property-placeholder>

    <!--连接池-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${driver}"></property>
        <property name="url" value="${url}"></property>
        <property name="username" value="${username}"></property>
        <property name="password" value="${password}"></property>
        <!--<property name="initialSize" value="${initialSize}"></property>-->
        <!--<property name="maxActive" value="${maxActive}"></property>-->
    </bean>

    <!--spring接管Mybatis的Session工厂-->
    <bean id="sessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

    <!--扫描dao-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="huanmingjie.fengmingmen.dao.user"></property>
    </bean>

    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
            <tx:method name="*" propagation="REQUIRED" read-only="false"></tx:method>
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <!--配置切入点表达式-->
        <aop:pointcut id="pt" expression="execution(* huanmingjie.fengmingmen.service.*.*(..))"></aop:pointcut>
        <!--建立通知和切入点表达式的关系-->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pt"></aop:advisor>
    </aop:config>


</beans>

3.jdbc.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/huanmingjie?characterEncoding=UTF8
username=root
password=root

4.test

package huanmingjie.fengmingmen.dao;

import huanmingjie.fengmingmen.module.user.domain.LOLUser;
import huanmingjie.fengmingmen.service.user.ILOLUserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:bean.xml")
public class UserDaoTest {

    @Autowired
    private ILOLUserService loluserService;

    @Test
    public void test() {
        System.out.println(loluserService);
        System.out.println(loluserService.getClass());
        System.out.println(loluserService.findAll());
        List<LOLUser> all = loluserService.findAll();
        for (LOLUser loluser : all) {
            System.out.println(loluser.getName());
        }
    }


}

测试:
在这里插入图片描述

九、整合完成最终测试:

  1. controller
package huanmingjie.fengmingmen.module.user.controller;

import huanmingjie.fengmingmen.module.user.domain.LOLUser;
import huanmingjie.fengmingmen.service.user.ILOLUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/lolUser")
public class LOLUserController {

    @Autowired
    private ILOLUserService lolUserServicel;

    @RequestMapping("/all")
    public ModelAndView findAll() {
        List<LOLUser> list = lolUserServicel.findAll();
        ModelAndView mv = new ModelAndView();
        //返回数据的KEY-VALUE
        mv.addObject("lol", list);
        //拼接的页面名称
        mv.setViewName("lol");
        return mv;
    }

}

2.lol.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: imall
  Date: 2019/12/19
  Time: 12:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>user</title>
</head>
<body style="background-image: url(/statics/image/LOLBackGround.jpg);">
<table width="300px" border="1" cellpadding="0" cellspacing="0" align="center"
       style="background: white;text-align: center;margin-top: 250px">
    <tr>
        <td>用户名称</td>
        <td>用户性别</td>
    </tr>
    <c:forEach var="LOL" items="${user}">
        <tr>
            <td>${LOL.name}</td>
            <td>${LOL.sex}</td>
        </tr>
    </c:forEach>

</table>
</body>
</html>

3.访问http://localhost:8080/lolUser/all
在这里插入图片描述

十、美观一下:

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值