SSM框架(Spring+SpringMVC+MyBatis)是目前Java WEB开发使用较多的框架,搭建起来起来比较麻烦,之前也曾搭建成功过。这次通过结合SSM开发的相关学习视频,再一次系统性的将整个SSM框架的搭建过程记录下来,以此来方便日后的开发。本文的前提条件,是Maven已经成功安装完毕!

搭建环境:IDEA  2017.1

Maven 3.3.9

Jdk  1.7

Tomcat 7.0

Mysql 5.7

1、建立Maven Project

(1)新建一个Maven项目

(2)输入GroupId和ArtifactId,可随意输入,GroupId可认为是项目的包名,ArtifactId为项目的名称

(3) 找到Maven安装的Home,关于Maven使用的镜像仓库是在Setting.xml中可修改的,这里注意下选择的Setting.xml的存放位置

(4)完成

(5)查看IDEA Console中Maven项目是否建立成功[INFO] ----------------------------------------------------------------------------

[INFO] Using following parameters for creating project from

Archetype: maven-archetype-webapp:RELEASE

[INFO] ----------------------------------------------------------------------------

[INFO] Parameter: groupId, Value: com.test

[INFO] Parameter: artifactId, Value: test

[INFO] Parameter: version, Value: 1.0-SNAPSHOT

[INFO] Parameter: package, Value: com.test

[INFO] Parameter: packageInPathFormat, Value: com/test

[INFO] Parameter: package, Value: com.test

[INFO] Parameter: version, Value: 1.0-SNAPSHOT

[INFO] Parameter: groupId, Value: com.test

[INFO] Parameter: artifactId, Value: test

[INFO] Project created from Archetype in dir:

C:\Users\Administrator\AppData\Local\Temp\archetypetmp\test

[INFO] ------------------------------------------------------------------------

[INFO] BUILD SUCCESS

[INFO] ------------------------------------------------------------------------

[INFO] Total time: 13.736 s

[INFO] Finished at: 2018-12-13T11:13:40+08:00

[INFO] Final Memory: 14M/150M

[INFO] ------------------------------------------------------------------------

[INFO] Maven execution finished

(6)建立好的Maven项目树状结构图

2、整合SSM框架

(1)在Pom.xml中添加Spring、SpringMVC、MyBatis相关jar

UTF-8

1.7

1.7

3.2.0.RELEASE

3.2.7

org.springframework

spring-core

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-context

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-jdbc

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-test

${spring.version}

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.2.2

javax.servlet

javax.servlet-api

3.0.1

provided

javax.servlet.jsp

jsp-api

2.2

provided

javax.servlet

jstl

1.2

taglibs

standard

1.1.2

com.fasterxml.jackson.core

jackson-databind

2.9.4

mysql

mysql-connector-java

5.1.39

commons-dbcp

commons-dbcp

1.2.2

commons-pool

commons-pool

1.3

ch.qos.logback

logback-classic

1.1.1

junit

junit

4.11

test

(2)在main/,新建java、resources文件夹,分别设为Sources Root和Resources Root,java主要是用来存放业务逻辑代码,resources主要是存放相关配置文件。

(3)在resources/,新建db.properties和log4j.properties,主要用于存放数据库的连接配置信息和日志配置信息。

db.properties:jdbc.driver=com.mysql.jdbc.Driver  //使用的是mysql jdbc驱动

jdbc.url=jdbc:mysql://localhost:3306/database  //url、port、database

jdbc.username=root              //账号

jdbc.password=123456           //密码

log4j.properties:#Global logging

configuration\uff0c\u5efa\u8bae\u5f00\u53d1\u73af\u5883\u4e2d\u8981\u7528debug

log4j.rootLogger=DEBUG, stdout

# Console output...

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

(4) 在mybatis/,创建mybatis整合需要的配置文件sqlMapConfig.xml

注:由于使用spring和mybatis的整合包进行mapper扫描,这里不需要进行mapper配置了。但必须遵循:mapper.xml和mapper.java文件同名且在一个目录。

(5) 在spring/,创建spring整合需要的配置文件applicationContext.xml<?xml  version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:aop="http://www.springframework.org/schema/aop"

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/aop

http://www.springframework.org/schema/aop/spring-aop.xsd

http://www.springframework.org/schema/tx

http://www.springframework.org/schema/tx/spring-tx.xsd">

destroy-method="close">

DataSourceTransactionManager">

pointcut="execution(* cn.ssm.test.service.impl.*.*(..))"/>

(6) 在spring/,创建springmvc.xml,配置注解映射器、注解适配器、视图解析器等<?xml  version="1.0" encoding="UTF-8"?>

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xmlns:util="http://www.springframework.org/schema/util"

xmlns:mvc="http://www.springframework.org/schema/mvc"

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/util

http://www.springframework.org/schema/util/spring-util.xsd

http://www.springframework.org/schema/mvc

http://www.springframework.org/schema/mvc/spring-mvc.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context.xsd">

InternalResourceViewResolver">

FormattingConversionServiceFactoryBean">

CustomDateConverter"/>

(7) 创建逆向工程,自动生成po及mapper文件

Po文件主要生成的是数据库表的字段定义,相关字段的Get和Set方法。

mapper文件主要生成的是数据库表中一般字段的查询接口及相关xml文件等。

po对象:public class Items {

private Integer id;

private String name;

private Float price;

private String pic;

private Date createtime;

private String detail;

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 == null ? null : name.trim();

}

public Float getPrice() {

return price;

}

public void setPrice(Float price) {

this.price = price;

}

public String getPic() {

return pic;

}

public void setPic(String pic) {

this.pic = pic == null ? null : pic.trim();

}

public Date getCreatetime() {

return createtime;

}

public void setCreatetime(Date createtime) {

this.createtime = createtime;

}

public String getDetail() {

return detail;

}

public void setDetail(String detail) {

this.detail = detail == null ? null : detail.trim();

}

}

Mapper:

mapper接口类:public interface ItemsMapper {

int countByExample(ItemsExample example);

int deleteByExample(ItemsExample example);

int deleteByPrimaryKey(Integer id);

int insert(Items record);

int insertSelective(Items record);

List selectByExampleWithBLOBs(ItemsExample example);

List selectByExample(ItemsExample example);

Items selectByPrimaryKey(Integer id);

int updateByExampleSelective(@Param("record") Items record,

@Param("example") ItemsExample example);

int updateByExampleWithBLOBs(@Param("record") Items record,

@Param("example") ItemsExample example);

int updateByExample(@Param("record") Items record,

@Param("example") ItemsExample example);

int updateByPrimaryKeySelective(Items record);

int updateByPrimaryKeyWithBLOBs(Items record);

int updateByPrimaryKey(Items record);

}

Mapper xml文件:<?xml  version="1.0" encoding="UTF-8" ?>

mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd" >

extends="BaseResultMap" >

and ${criterion.condition}

and ${criterion.condition} #{criterion.value}

and ${criterion.condition} #{criterion.value}

and #{criterion.secondValue}

and ${criterion.condition}

item="listItem" open="(" close=")" separator=",">

#{listItem}

and ${criterion.condition}

and ${criterion.condition} #{criterion.value}

and ${criterion.condition} #{criterion.value}

and #{criterion.secondValue}

and ${criterion.condition}

item="listItem" open="(" close=")" separator=",">

#{listItem}

id, name, price, pic, createtime

detail

parameterType="cn.ssm.test.po.ItemsExample" >

select

distinct

,

from items

order by ${orderByClause}

parameterType="cn.ssm.test.po.ItemsExample" >

select

distinct

from items

order by ${orderByClause}

parameterType="java.lang.Integer" >

select

,

from items

where id = #{id,jdbcType=INTEGER}

delete from items

where id = #{id,jdbcType=INTEGER}

delete from items

insert into items (id, name, price,

pic, createtime, detail

)

values (#{id,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR},

#{price,jdbcType=REAL},

#{pic,jdbcType=VARCHAR}, #{createtime,jdbcType=TIMESTAMP},

#{detail,jdbcType=LONGVARCHAR}

)

insert into items

id,

name,

price,

pic,

createtime,

detail,

#{id,jdbcType=INTEGER},

#{name,jdbcType=VARCHAR},

#{price,jdbcType=REAL},

#{pic,jdbcType=VARCHAR},

#{createtime,jdbcType=TIMESTAMP},

#{detail,jdbcType=LONGVARCHAR},

resultType="java.lang.Integer" >

select count(*) from items

update items

id = #{record.id,jdbcType=INTEGER},

name = #{record.name,jdbcType=VARCHAR},

price = #{record.price,jdbcType=REAL},

pic = #{record.pic,jdbcType=VARCHAR},

createtime = #{record.createtime,jdbcType=TIMESTAMP},

detail = #{record.detail,jdbcType=LONGVARCHAR},

update items

set id = #{record.id,jdbcType=INTEGER},

name = #{record.name,jdbcType=VARCHAR},

price = #{record.price,jdbcType=REAL},

pic = #{record.pic,jdbcType=VARCHAR},

createtime = #{record.createtime,jdbcType=TIMESTAMP},

detail = #{record.detail,jdbcType=LONGVARCHAR}

update items

set id = #{record.id,jdbcType=INTEGER},

name = #{record.name,jdbcType=VARCHAR},

price = #{record.price,jdbcType=REAL},

pic = #{record.pic,jdbcType=VARCHAR},

createtime = #{record.createtime,jdbcType=TIMESTAMP}

update items

name = #{name,jdbcType=VARCHAR},

price = #{price,jdbcType=REAL},

pic = #{pic,jdbcType=VARCHAR},

createtime = #{createtime,jdbcType=TIMESTAMP},

detail = #{detail,jdbcType=LONGVARCHAR},

where id = #{id,jdbcType=INTEGER}

update items

set name = #{name,jdbcType=VARCHAR},

price = #{price,jdbcType=REAL},

pic = #{pic,jdbcType=VARCHAR},

createtime = #{createtime,jdbcType=TIMESTAMP},

detail = #{detail,jdbcType=LONGVARCHAR}

where id = #{id,jdbcType=INTEGER}

update items

set name = #{name,jdbcType=VARCHAR},

price = #{price,jdbcType=REAL},

pic = #{pic,jdbcType=VARCHAR},

createtime = #{createtime,jdbcType=TIMESTAMP}

where id = #{id,jdbcType=INTEGER}

(8) 在service文件夹内,通过使用service整合mapper接口,实现相关查询功能@Autowired

private ItemsMapper itemsMapper;

@Override

public List findItemsList(ItemsQueryVo itemsQueryVo)

throws Exception {

//通过ItemsMapperCustom查询数据库

return itemsMapperCustom.findItemsList(itemsQueryVo);

}

(9) 创建controller,使用注解功能@Controller

//为了对url进行分类管理 ,可以在这里定义根路径,最终访问url是根路径+子路径

//比如:商品列表:/items/queryItems.action

@RequestMapping("/items")

public class ItemsController {

@Autowired

private ItemsService itemsService;

// 商品查询

@RequestMapping("/queryItems")

public ModelAndView queryItems(HttpServletRequest request) throws Exception {

//测试forward后request是否可以共享

System.out.println(request.getParameter("id"));

// 调用service查找 数据库,查询商品列表

List itemsList = itemsService.findItemsList(null);

// 返回ModelAndView

ModelAndView modelAndView = new ModelAndView();

// 相当 于request的setAttribut,在jsp页面中通过itemsList取数据

modelAndView.addObject("itemsList", itemsList);

// 指定视图

// 下边的路径,如果在视图解析器中配置jsp路径的前缀和jsp路径的后缀,修改为

// modelAndView.setViewName("/WEB-INF/jsp/items/itemsList.jsp");

// 上边的路径配置可以不在程序中指定jsp路径的前缀和jsp路径的后缀

modelAndView.setViewName("items/itemsList");

return modelAndView;

}

}

(10) 在WEB-INF下添加jsp文件,在jsp文件夹内添加html文件

(11) 修改web.xml的配置,修改访问路径、配置外路径等。

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"

id="WebApp_ID" version="2.5">

test

contextConfigLocation

classpath:spring/applicationContext.xml

org.springframework.web.context.ContextLoaderListener

spring_mybatis

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/springmvc.xml

spring_mybatis

*.action

CharacterEncodingFilter

org.springframework.web.filter.CharacterEncodingFilter

encoding

utf-8

CharacterEncodingFilter

/*

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

3、实现Tomcat热部署

(1)找到Edit configurations,进入到如下界面

(2)点击fix或者点击Deployment,进入如下界面设置相关参数

(3)更改下图中的两项参数

(4)Build项目

(5)build后的结果展示

4、注意事项

(1)需要在pom.xml中的build下添加如下代码,解决编译后的mapper文件内无法编译XXXmapper.xml内容

src/main/java

**/*.xml

**/*.properties

false

(2)热部署的时候,注意不要改变Application context的默认配置,需要修改首页显示的页面,路径可在Server中配置

idea ssm框架 mysql_IDEA 2017 整合SSM框架(使用Maven创建工程)相关推荐

  1. SSM之SpringMVC 03 —— 整合SSM(简单图书系统)

    系列文章 SSM之SpringMVC 01 -- SpringMVC原理及概念.Hello SpringMVC 注解版和配置版 SSM之SpringMVC 02 -- Controller和RestF ...

  2. idea ssm框架 mysql_idea搭建简单ssm框架的最详细教程(新)

    为开发一个测试程序,特搭建一个简单的ssm框架,因为网上看到很多都是比较老旧的教程,很多包都不能用了,eclipes搭建并且其中还附带了很多的其他东西,所以特此记录一下mac中idea搭建过程. 另: ...

  3. SSM框架的优势整合

    SSM框架的优势整合 SSM为开发解决了什么问题? SSM框架:Spring+SpringMVC+Mybatis Spring框架的优势: 提供IOC容器,解决了层与层之间的耦合问题(对象之间的依赖关 ...

  4. ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-整合各大框架

    ssm(springmvc4+spring4+mybatis3)整合实战-个人博客系统-整合各大框架 ssm框架整合开发实战,这一篇我将介绍如何实现各大框架的整合. 上一篇博客,我介绍了web.xml ...

  5. SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一)

    SSM(Spring+SpringMVC+Mybatis)框架环境搭建(整合步骤)(一) 1. 前言 最近在写毕设过程中,重新梳理了一遍SSM框架,特此记录一下. 附上源码:https://gitee ...

  6. springboot2整合mysql5_SpringBoot2整合SSM框架详解

    SpringBoot2整合SSM框架详解 发布时间:2019-01-15 21:33, 浏览次数:1218 , 标签: SpringBoot SSM <>开发环境 * 开发工具:Eclip ...

  7. idea 使用 maven 整合 ssm 框架 实现简单的增、删、改 和 分页查询功能

    详细请参考:   idea 使用 maven 整合 ssm 框架 文章目录 ==效果图== ==准备数据库== ==创建maven项目== ==配置文件== pom.xml jdbc.properti ...

  8. idea 使用maven 整合ssm框架

    创建 maven 项目 刚创建好的 maven 项目结构 整合 SSM 框架后的项目结构 数据库环境 创建 mybatis 数据库,在 mybatis 数据库中创建 teacher 数据库表,然后在 ...

  9. IDEA整合SSM框架之配置日志logback(七)

    IDEA整合SSM框架 配置日志logback 引入logback相关依赖 在resources的根目录下创建logback.xml 在web.xml配置logback.xml 运行项目,查看日志打印 ...

最新文章

  1. 2021年中国AIoT产业全景图谱
  2. python所有变量更新_PYTHON:使用python变量更新MULTIPLE COLUMNS
  3. 最短路径Dijkstra讲解,工具包使用 python
  4. [无向图割点] PKU 1523 SPF
  5. 免费试用 Mobile Me
  6. 服务注册中心 eureka 搭建
  7. pythonspot_python-Spotipy-列表索引超出范围
  8. iOS开发之适配http请求
  9. setCompoundDrawables和setCompoundDrawablesWithIntrinsicBounds区别
  10. ASP.NET的路由系统
  11. java能写dnf辅助么,【Java8新特征】还没搞懂函数式接口?赶忙过来看看吧!_卡盟,dnf脚本...
  12. java winform程序_重拾JAVA之WinForm实战之(四)
  13. Docker下安装RabbitMQ
  14. Linux_clustalW安装及使用(部分)
  15. WIN10 任务栏卡死解决办法
  16. 智能路由器OpenWrt 开发环境 及 编译分析(一)
  17. 微信开发 现金红包、裂变红包、企业付款
  18. OCI runtime create failed: runc create failed: unable to start container process: --docker加载镜像
  19. English总结(一)-- 常用语句篇
  20. YYKit系列之——YYCache使用

热门文章

  1. 计算机网络 原理与实验指导书,《计算机网络原理》实验指导书.doc
  2. 软件测试工程师-HTML
  3. WriteFreely:创建博客,建立社区
  4. linux bcc_使用bcc / BPF在Linux中分析性能的7种工具
  5. docbook_DocBook简介,一种值得学习的灵活标记语言
  6. 最末参与者优化 lpo_优化博客以提高读者参与度的6种方法
  7. es6 async函数的实现原理
  8. 贪心算法,递归算法,动态规划算法比较与总结
  9. 十八.多个SLAM框架(A-LOAM、Lego-loam、LIO-SAM、livox-loam)室外测试效果粗略对比分析
  10. java电脑_电脑上搭建java开发环境