用IDEA和maven搭建的ssm項目示例(Spring,SpringMVC,Mybatis)

前後端都有,包括springMVC使用JQuery,最基礎的增刪改查。項目是配置在tomcat上的,導入後記得進行配置。
先放項目鏈接
https://github.com/zhsun1995/ssm_idea_maven_zhsun
直接下載或者git導入都可以,注意配置maven的國內鏡像,不然依賴下載可能比較慢。
百度網盤鏈接
鏈接:https://pan.baidu.com/s/11pMkZayGq3xNYLCS5hhsnw
提取碼:hjhx
注:項目經過後續修改和下面的教程代碼上可能有所出入,但是整體邏輯沒變。

一、數據庫

MySQL
username:root
password:123
// 如若修改,記得在applictionContext.xml中進行修改

1、創建數據庫

CREATE DATABASE ssm_test;
USE ssm_test;

2、創建兩個表native_place,information

CREATE TABLE native_place(
	id int(11) NOT NULL AUTO_INCREMENT,
	name varchar(30) NOT NULL,
	PRIMARY KEY(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;


CREATE TABLE IF NOT EXISTS information (
	id int(11) NOT NULL AUTO_INCREMENT,
	uid int(11) NOT NULL,
	name varchar(30) NOT NULL,
	placeid int(11),
	salary float,
	PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

3、添加外鍵
information表的placeid關聯native_place表的id。

ALTER TABLE information 
ADD CONSTRAINT FK_naticeplace_information 
FOREIGN KEY(placeid) REFERENCES native_place(id);

4、添加唯一性約束

ALTER TABLE information
ADD CONSTRAINT UNIQUE_information_uid UNIQUE (uid);

5、向表中插入幾組數據

INSERT INTO native_place VALUES(1, '北京');
INSERT INTO native_place VALUES(2, '河北');
INSERT INTO native_place VALUES(3, '山東');
INSERT INTO native_place VALUES(4, '廣東');

INSERT INTO information VALUES(1, 10001, '張三', 1, 5555.55);
INSERT INTO information VALUES(2, 10002, '李四', 3, 5555.55);
INSERT INTO information VALUES(3, 10003, '張三', 1, 6666.66);
INSERT INTO information VALUES(4, 10004, '王五', 4, 7000);

二、idea maven創建項目

1、注意不要選錯了
在這裏插入圖片描述
選擇自己的maven版本
在這裏插入圖片描述
2、創建好了之後沒有java和resources文件夾,自己創建一個。
在這裏插入圖片描述
3、向pom.xml中添加依賴
有很多,放個截圖示意一下,全部的可以直接下載項目複製,注意不要全部複製,只複製dependencies以及所對應properties中所對應的version定義即可。
在這裏插入圖片描述
到這裏準備工作完成。

三、pojo包

Information類對應information表,生成對應的構造方法及setter和getter方法,toString方法。
在這裏插入圖片描述
Place類對應native_place表,生成對應的構造方法及setter和getter方法,toString方法。
在這裏插入圖片描述

四、mapper包

InformationMapper
增刪改查

package com.laosun.mapper;

import com.laosun.pojo.Information;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @author SUN
 * @version V1.0
 * @date 2020.04.24
 */
public interface InformationMapper {
    /**
     * 簡單的SQL直接用註解完成
     */

    /**
     * 增
     * @param information
     * @return
     */
    @Insert("insert into information(uid, name, placeid, salary)" +
            " values (#{uid}, #{name}, #{placeid}, #{salary})")
    public int add(Information information);

    /**
     * 刪
     * @param id
     * @return
     */
    @Delete("delete from information where id = #{id}")
    public void delete(int id);

    /**
     * 改
     * @param information
     * @return
     */
    @Update("update information set uid = #{uid}, name = #{name}, " +
            "placeid = #{placeid}, salary = #{salary} where id = #id")
    public int update(Information information);

    /**
     * 查單
     * @param id
     * @return
     */
    @Select("select * from information where id = #{id}")
    public Information get(int id);

    /**
     * 兩個查多函數利用配置文件寫動態sql語句
     * Informatiom.xml  放於同目錄下
     */
    /**
     * 查多
     * @return
     */
    public List<Information> list();

    /**
     * 按條件查多
     * @param map
     * @return
     */
    public List<Information> list(Map<String, String> map);
}

Information.xml->動態SQL查多

<select id="list" resultType="Information">
    select i.* FROM information i left join native_place p on i.placeid = p.id
    <where>
        <if test="uid != null">
            and i.uid = #{uid}
        </if>
        <if test="place != null">
            and p.name = #{place}
        </if>
    </where>
</select>

PlaceMapper
增刪改查

五、applicationContext.xml

在resources目錄下創建applicationContext.xml
1、聲明註解
2、將mybatis的數據庫配置納入spring
3、掃描SQL語句的配置文件Information.xml
4、掃描Mapper,並將其生命週期納入Spring的管理
xml文件將項目下載下來複制即可。
到這一步就可以單獨測試數據庫部分是否好用了
在這裏插入圖片描述

六、service包及service.impl包

此處完成所需要的應用邏輯。忽略前端,目前可單獨進行測試。

package com.laosun.service;

import com.laosun.pojo.Information;
import java.util.List;
import java.util.Map;

/**
 * @author SUN
 * @version V1.0
 * @date 2020.04.24
 */
public interface InformationService {
    /**
     * 簡單的SQL直接用註解完成
     */

    /**
     * 增
     * @param information
     * @return
     */
    public int add(Information information);

    /**
     * 刪
     * @param id
     * @return
     */
    public void delete(int id);

    /**
     * 改
     * @param information
     * @return
     */
    public int update(Information information);

    /**
     * 查單
     * @param id
     * @return
     */
    public Information get(int id);

    /**
     * 查多
     * @return
     */
    public List<Information> list();

    /**
     * 按條件查多
     * @param map
     * @return
     */
    public List<Information> list(Map<String, String> map);

}
package com.laosun.service.impl;

import com.laosun.mapper.InformationMapper;
import com.laosun.pojo.Information;
import com.laosun.service.InformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @author SUN
 * @version V1.0
 * @date 2020.04.24
 */
@Service
public class InformationServiceImpl implements InformationService {
    @Autowired
    private InformationMapper informationMapper;
    /**
     * 增
     *
     * @param information
     * @return
     */
    @Override
    public int add(Information information) {
        return informationMapper.add(information);
    }

    /**
     * 刪
     *
     * @param id
     * @return
     */
    @Override
    public void delete(int id) {
        informationMapper.delete(id);
    }

    /**
     * 改
     *
     * @param information
     * @return
     */
    @Override
    public int update(Information information) {
        return informationMapper.update(information);
    }

    /**
     * 查單
     *
     * @param id
     * @return
     */
    @Override
    public Information get(int id) {
        return informationMapper.get(id);
    }

    /**
     * 查多
     *
     * @return
     */
    @Override
    public List<Information> list() {
        return informationMapper.list();
    }

    /**
     * 按條件查多
     *
     * @param map
     * @return
     */
    @Override
    public List<Information> list(Map<String, String> map) {
        return informationMapper.list(map);
    }
}

此時要在applicationContext.xml將service納入Spring管理。

<context:component-scan base-package="com.laosun.service" />

七、開始編寫前端界面

因爲主要學習後端開發,前端界面從簡編寫,提供接口和顯示界面即可,這裏只是爲了簡單顯示,後續項目的jsp是修改過的。
index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" import="java.util.*"%>

<html>
<body>
<h2>歡迎界面,可修改成登錄界面,現在直接點登錄跳轉即可</h2>

<form action="showInformation" method="post">

    用戶名:<input type="text" name="username" value=""><br />
    密 碼: <input type="password" name="password" value=""><br />

    <input type="submit" value="登錄">
</form>
</body>
</html>

showInformation.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" import="java.util.*"%>

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

<table align='center' border='1' cellspacing='0'>
    <tr>
        <td>id</td>
        <td>name</td>
        <td>place</td>
        <td>salary</td>
    </tr>
    <information:forEach items="${informations}" var="information" varStatus="st">
        <tr>
            <td>${information.id}</td>
            <td>${information.name}</td>
            <td>${information.placeid}</td>
            <td>${information.salary}</td>

        </tr>
    </information:forEach>
</table>

八、springMVC.xml

在resources下創建springMVC.xml
1、掃描Controller,並將其生命週期納入Spring管理
2、註解驅動,以使得訪問路徑與方法的匹配可以通過註解配置
3、靜態頁面,如html,css,js,images可以訪問,注意需要在web.xml中對應的添加配置。
4、視圖定位到/WEB/INF/jsp
xml文件將項目下載下來複制即可。

九、web.xml

把applicationContext.xml和springMVC.xml加載進來。
xml文件將項目下載下來複制即可。

十、controller包

此處只寫了查詢功能,項目文件中包括最基礎的增刪改查。

package com.laosun.controller;

import com.laosun.pojo.Information;
import com.laosun.service.InformationService;
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;

/**
 * @author SUN
 * @version V1.0
 * @date 2020.04.24
 */
@Controller
public class InformationController {
    @Autowired
    InformationService informationService;

    @RequestMapping("index")
    public ModelAndView toShowInformation(){
        ModelAndView mav = new ModelAndView("showInformation");
        return mav;
    }

    @RequestMapping("showInformation")
    public ModelAndView listInformation(){
        ModelAndView mav = new ModelAndView();
        List<Information> informations = informationService.list();
        // "informations"和showInformation.jsp中的items="${informations}"對應
        mav.addObject("informations", informations);
        mav.setViewName("showInformation");
        return mav;
    }
}

十一、部署到tomcat上

在這裏插入圖片描述在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述
到這裏一個只有查詢功能的ssm項目搭建完成
在這裏插入圖片描述

十二、在此基礎上可以在擴展自己想要的功能

文中所附鏈接是經過修改過後的工程,包含增刪改查功能。

十三、一些配置上踩的坑

1、IDEA中引入Jquery需要載入。
2、springMVC過濾靜態資源,需要在web.xml的servlet分發之前配置。(在web.xml中有註釋)
3、@RequestBody使用之前需要在springMVC中添加配置(在springMVC.xml中有註釋)。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章