用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中有注释)。

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