SSM框架搭建

MyBatis+Spring+SpringMVC框架搭建

系統(tǒng)的整體架構(gòu)圖:

整體架構(gòu)

1涩惑、添加jar包

<dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>

    <!-- 加入ServletAPI -->
    <dependency>
      <groupId>javax.servlet</groupId>
      <artifactId>javax.servlet-api</artifactId>
      <version>3.1.0</version>
      <scope>provided</scope>
    </dependency>

    <!-- MySQL依賴(lài) start -->
    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>5.1.38</version>
    </dependency>

    <!-- 加入MyBatis 依賴(lài) start -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis</artifactId>
      <version>3.2.8</version>
    </dependency>

    <!-- 引入Spring(包含SpringMVC) 依賴(lài) start -->
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-web</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-oxm</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>


    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-webmvc</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-test</artifactId>
      <version>5.0.8.RELEASE</version>
    </dependency>

    <!-- 引用c3p0 依賴(lài) start
      <dependency>
          <groupId>com.mchange</groupId>
          <artifactId>c3p0</artifactId>
          <version>0.9.2.1</version>
      </dependency>
       -->
    <!-- 引用插件依賴(lài):MyBatis整合Spring,如果mybatis版本在3.4及以上版本
       mybatis-spring的版本要在1.3以上   -->
    <dependency>
      <groupId>org.mybatis</groupId>
      <artifactId>mybatis-spring</artifactId>
      <version>1.2.2</version>
    </dependency>

    <!-- JSTL -->
    <dependency>
      <groupId>jstl</groupId>
      <artifactId>jstl</artifactId>
      <version>1.2</version>
    </dependency>

    <!-- 德魯伊數(shù)據(jù)連接池 -->
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.0.9</version>
    </dependency>

    <!-- pagehelper -->
    <dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper</artifactId>
      <version>4.1.6</version>
    </dependency>
    <!--處理json-->
    <dependency>
      <groupId>com.fasterxml.jackson.core</groupId>
      <artifactId>jackson-databind</artifactId>
      <version>2.9.5</version>
    </dependency>
    <!--導(dǎo)出excel-->
    <dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi</artifactId>
      <version>3.6</version>
    </dependency>
    <!--javaee-->
    <dependency>
      <groupId>javax</groupId>
      <artifactId>javaee-api</artifactId>
      <version>7.0</version>
      <scope>provided</scope>
    </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>


    <!--- Activiti依賴(lài)導(dǎo)入 -->
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-spring</artifactId>
      <version>5.18.0</version>
    </dependency>
    <dependency>
      <groupId>org.activiti</groupId>
      <artifactId>activiti-engine</artifactId>
      <version>5.18.0</version>
      <exclusions>
        <exclusion>
          <artifactId>slf4j-api</artifactId>
          <groupId>org.slf4j</groupId>
        </exclusion>
        <exclusion>
          <artifactId>spring-beans</artifactId>
          <groupId>org.springframework</groupId>
        </exclusion>
        <exclusion>
          <artifactId>jackson-core-asl</artifactId>
          <groupId>org.codehaus.jackson</groupId>
        </exclusion>
        <exclusion>
          <artifactId>commons-lang3</artifactId>
          <groupId>org.apache.commons</groupId>
        </exclusion>
      </exclusions>
    </dependency>
  </dependencies>

2、web.xml配置

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

  <display-name>Archetype Created Web Application</display-name>

  <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>
  </servlet>
  <servlet-mapping>
    <servlet-name>springMvc</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <filter>
    <filter-name>aa</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>aa</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

3桑驱、添加實(shí)體類(lèi)User

package com.fan.entity;

public class User {
    private Integer id;
    private String name;
    private String sex;

    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 String getSex() {
        return sex;
    }

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

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

4竭恬、添加UserDao接口類(lèi)和UserDaoImpl實(shí)現(xiàn)類(lèi)

package com.fan.dao;

import com.fan.entity.User;

import java.util.List;

public interface UserDao {
    public List<User> findAll();
}
package com.fan.dao.impl;

import com.fan.dao.UserDao;
import com.fan.entity.User;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.List;

@Repository
public class UserDaoImpl implements UserDao {
    @Resource(name="temp")
    private SqlSessionTemplate sqlSessionTemplate;

    /**
     * 查找所有用戶(hù)信息
     * */
    @Override
    public List<User> findAll() {
        return sqlSessionTemplate.selectList("com.fan.dao.UserDao.findAll");
    }
}

5、添加UserService接口類(lèi)和UserServicelmpl實(shí)現(xiàn)類(lèi)

package com.fan.service;

import com.fan.entity.User;

import java.util.List;

public interface UserService {
    public List<User> findAll();
}
package com.fan.service.impl;

import com.fan.dao.UserDao;
import com.fan.entity.User;
import com.fan.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Resource(name="userDaoImpl")
    private UserDao userDao;

    @Override
    public List<User> findAll() {

        return  userDao.findAll();
    }
}

6熬的、添加Controller類(lèi)

package com.fan.controller;

import com.fan.entity.User;
import com.fan.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {
    @Resource(name="userServiceImpl")
    private UserService userService;


    /**
     * 查找所有用戶(hù)信息
     * */
    @RequestMapping("/findall")
    public String test(ModelMap map){
        List<User> users = userService.findAll();
        map.addAttribute("u",users);
        return "show";
    }
}

7痊硕、添加index.jsp和show.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h1>首頁(yè)</h1>
<h2><a href="/findall">查詢(xún)所有用戶(hù)</a></h2>
</body>
</html>
<%--
  Created by IntelliJ IDEA.
  User: Mr Wei
  Date: 2020/*/*
  Time: 16:24
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>show</title>
</head>
<body>
<h1>show</h1>
<c:forEach items="${u}" var="u1">
    ${u1.id}--${u1.name}--${u1.sex}<br>
</c:forEach>
</body>
</html>

8、配置UserDaoMapper.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 表示命名空間押框,通常定義的格式是接口的完整路徑-->
<mapper namespace="com.fan.dao.UserDao">
    <!--定義sql語(yǔ)句,sql語(yǔ)句結(jié)束后不要加分號(hào)-->
    <!-- id是被調(diào)用的方法名   mapper文件中讀取參數(shù)的格式:#{屬性名} -->
    <select id="findAll" resultType="com.fan.entity.User">
        select * from users
    </select>
</mapper>

9岔绸、配置applicationContext.xml文件(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:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       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/mvc
                           http://www.springframework.org/schema/mvc/spring-mvc.xsd
                           http://www.springframework.org/schema/tx
                           http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--1、創(chuàng)建數(shù)據(jù)源强戴,使用spring連接數(shù)據(jù)庫(kù)-->
    <bean id="db" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/test"></property>
        <property name="username" value="root"></property>
        <property name="password" value="root"></property>
    </bean>

    <!--2亭螟、掃描注解包-->
    <context:component-scan base-package="com"></context:component-scan>

    <!--3、創(chuàng)建視圖解析器-->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/"></property>
        <property name="suffix" value=".jsp"></property>
    </bean>

    <!--4骑歹、加載注解驅(qū)動(dòng)-->
    <mvc:annotation-driven></mvc:annotation-driven>

    <!--5预烙、創(chuàng)建sqlSession工廠(chǎng)-->
    <bean id="fac" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="db"></property>
        <property name="mapperLocations" value="classpath:mapper/*.xml"></property>
      <!-- <property name="configLocation" value="classpath:mybatis-config.xml"></property>-->
    </bean>

    <!--6、使用dao層實(shí)現(xiàn)類(lèi)時(shí)道媚,需要得到sqlSessionTemplate-->
    <!--mybatis是用SqlSession操作數(shù)據(jù)庫(kù)扁掸,Spring用jdbcTemplate操作數(shù)據(jù)庫(kù)翘县,現(xiàn)在用的是sqlSessionTemplate-->
    <bean id="temp" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="fac"></constructor-arg>
    </bean>

    <!--7、配置事務(wù)-->
    <bean id="tx" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="db"></property>
    </bean>
    <tx:advice id="ad" transaction-manager="tx"></tx:advice>

    <!--10谴分、配置靜態(tài)資源-->
    <mvc:default-servlet-handler></mvc:default-servlet-handler>
</beans>

10锈麸、啟動(dòng)tomcat進(jìn)行測(cè)試

注:我們可以將applicationContext.xml中第6小點(diǎn)重新配置,然后就可以把UserDaoImpl實(shí)現(xiàn)類(lèi)刪除牺蹄。

    <!--6忘伞、省略實(shí)現(xiàn)類(lèi)  省略dao層實(shí)現(xiàn)類(lèi)(使用MapperScannerConfigurer替代SqlSessionTemplate):-->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.fan.dao"></property>
        <property name="sqlSessionFactoryBeanName" value="fac"></property>
    </bean>
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市沙兰,隨后出現(xiàn)的幾起案子氓奈,更是在濱河造成了極大的恐慌,老刑警劉巖鼎天,帶你破解...
    沈念sama閱讀 216,591評(píng)論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件舀奶,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡斋射,警方通過(guò)查閱死者的電腦和手機(jī)育勺,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,448評(píng)論 3 392
  • 文/潘曉璐 我一進(jìn)店門(mén),熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)罗岖,“玉大人涧至,你說(shuō)我怎么就攤上這事⊙轿牛” “怎么了化借?”我有些...
    開(kāi)封第一講書(shū)人閱讀 162,823評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀(guān)的道長(zhǎng)捡多。 經(jīng)常有香客問(wèn)我蓖康,道長(zhǎng),這世上最難降的妖魔是什么垒手? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,204評(píng)論 1 292
  • 正文 為了忘掉前任蒜焊,我火速辦了婚禮,結(jié)果婚禮上科贬,老公的妹妹穿的比我還像新娘泳梆。我一直安慰自己,他們只是感情好榜掌,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,228評(píng)論 6 388
  • 文/花漫 我一把揭開(kāi)白布优妙。 她就那樣靜靜地躺著,像睡著了一般憎账。 火紅的嫁衣襯著肌膚如雪套硼。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 51,190評(píng)論 1 299
  • 那天胞皱,我揣著相機(jī)與錄音邪意,去河邊找鬼九妈。 笑死,一個(gè)胖子當(dāng)著我的面吹牛雾鬼,可吹牛的內(nèi)容都是我干的萌朱。 我是一名探鬼主播,決...
    沈念sama閱讀 40,078評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼策菜,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼晶疼!你這毒婦竟也來(lái)了?” 一聲冷哼從身側(cè)響起又憨,我...
    開(kāi)封第一講書(shū)人閱讀 38,923評(píng)論 0 274
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤冒晰,失蹤者是張志新(化名)和其女友劉穎,沒(méi)想到半個(gè)月后竟块,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,334評(píng)論 1 310
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡耐齐,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,550評(píng)論 2 333
  • 正文 我和宋清朗相戀三年浪秘,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片埠况。...
    茶點(diǎn)故事閱讀 39,727評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡耸携,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出辕翰,到底是詐尸還是另有隱情夺衍,我是刑警寧澤,帶...
    沈念sama閱讀 35,428評(píng)論 5 343
  • 正文 年R本政府宣布喜命,位于F島的核電站沟沙,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏壁榕。R本人自食惡果不足惜矛紫,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,022評(píng)論 3 326
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望牌里。 院中可真熱鬧颊咬,春花似錦、人聲如沸牡辽。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,672評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)态辛。三九已至麸澜,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間因妙,已是汗流浹背痰憎。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 32,826評(píng)論 1 269
  • 我被黑心中介騙來(lái)泰國(guó)打工票髓, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人铣耘。 一個(gè)月前我還...
    沈念sama閱讀 47,734評(píng)論 2 368
  • 正文 我出身青樓洽沟,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親蜗细。 傳聞我的和親對(duì)象是個(gè)殘疾皇子裆操,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,619評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容