通过IEAD+Maven快速搭建SSM项目的过程(Spring + Spring MVC + Mybatis)

网友投稿 263 2023-02-10

通过IEAD+Maven快速搭建SSM项目的过程(Spring + Spring MVC + Mybatis)

项目效果预览:

通过访问控制层的test/dologin方法进入index.jsp,展示jsp中的“hello,ssm”内容

一、新建项目:

1.打开IDEA,新建一个Maven项目

2.点击next,新建一个为ssmDemo的Maven项目(自己定义项目GroupId和ArtifactId)

3.配置本地Maven路径

4.点击Next,确认项目信息,配置项目名称和项目路径,点击Finish按钮即可创建项目

5.项目创建完成,目录结构如下:

注:如项目目录中未包含src,则项目还在初始化,注意网络连接,稍等片刻即可

二、补充项目结构:

1.在main文件夹下面创建java和resources文件夹

2.使java文件夹为项目资源文件夹(Sources root)

3.使resources文件夹为Resources root

4.在src/java目录下面创建com包,在com包创建controller、dao、po、service、service.impl包

三、完善pom.xml(添加项目依赖--完整pom.xml代码)

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.demo

ssmDemo02

1.0-SNAPSHOT

war

ssmDemo02 Maven Webapp

http://example.com

UTF-8

1.7

1.7

5.0.2.RELEASE

1.6.6

1.2.12

1.2.3

5.1.6

3.4.5

5.0.1.RELEASE

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.3.0

org.springframework

spring-contextdnUne

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-test

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-orm

${spring.version}

org.aspectj

aspectjweaver

1.6.8

org.springframework

spring-jdbc

${spring.version}

junit

junit

4.12

test

mysql

mysql-connector-java

${mysql.version}

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet.jsp

jsp-api

2.0

provided

jstl

jstl

1.2

log4j

log4j

${log4j.version}

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

com.alibaba

druid

1.0.9

ssmDemo02

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.demo

ssmDemo02

1.0-SNAPSHOT

war

ssmDemo02 Maven Webapp

http://example.com

UTF-8

1.7

1.7

5.0.2.RELEASE

1.6.6

1.2.12

1.2.3

5.1.6

3.4.5

5.0.1.RELEASE

org.mybatis

mybatis

${mybatis.version}

org.mybatis

mybatis-spring

1.3.0

org.springframework

spring-contextdnUne

${spring.version}

org.springframework

spring-context-support

${spring.version}

org.springframework

spring-aop

${spring.version}

org.springframework

spring-core

${spring.version}

org.springframework

spring-web

${spring.version}

org.springframework

spring-webmvc

${spring.version}

org.springframework

spring-beans

${spring.version}

org.springframework

spring-test

${spring.version}

org.springframework

spring-tx

${spring.version}

org.springframework

spring-orm

${spring.version}

org.aspectj

aspectjweaver

1.6.8

org.springframework

spring-jdbc

${spring.version}

junit

junit

4.12

test

mysql

mysql-connector-java

${mysql.version}

javax.servlet

javax.servlet-api

3.1.0

provided

javax.servlet.jsp

jsp-api

2.0

provided

jstl

jstl

1.2

log4j

log4j

${log4j.version}

org.slf4j

slf4j-api

${slf4j.version}

org.slf4j

slf4j-log4j12

${slf4j.version}

com.alibaba

druid

1.0.9

ssmDemo02

maven-clean-plugin

3.1.0

maven-resources-plugin

3.0.2

maven-compiler-plugin

3.8.0

maven-surefire-plugin

2.22.1

maven-war-plugin

3.2.2

maven-install-plugin

2.5.2

maven-deploy-plugin

2.8.2

效果图:

注1:代码复制完成后,选择右下角的Import Changes

注2:如Import Changes后External还未出现jar包,则选中项目,右键,选择maven,Generate Sources...

四、为项目导入jar包

1.选择项目右键 -- open Module Setting

2.选择Artifact

3.选择项目名,选择outPut LayOut,选择Available Elements -- 选择项目名称 -- 右键 -- put into...

效果图(WEB-INF中添加了lib目录):

五、完善target文件夹

1.选择右侧侧栏的maven

2.选择Lifecycle -- 选择clean(双击)

3.选择Lifecycle -- 选择install(双击)

4.效果图

六、测试项目

1.添加tomcat(run --> editConfigurations --> 点击+ --> 选择tomcat-- local)

2.配置Deployment(点击Deployment -- 选择右下角+ -- 选择Artifact --选择Artifa)

3.配置访问路径:/ssmDemotest

4.启动tomcat,访问:http://localhost:8080/ssmDemotest

七、编写ssm框架所需xml

1.在src/resources文件夹下面添加mybatis包、spring包

2.在src/resources文件夹下面添加log4j.properties、db.properties

3.在mybatis包下添加:mybatis-config.xml

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

4.在spring包下添加:spring-dao.xml

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

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

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

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

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

5.在spring包下添加:spring-service.xml

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

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

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

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

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://springframework.org/schema/beans/spring-beans-4.0.xsd

http://springframework.org/schema/context http://springframework.org/schema/context/spring-context-4.0.xsd

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

http://springframework.org/schema/tx http://springframework.org/schema/tx/spring-tx-4.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util-4.0.xsd">

6.在spring包下添加:spring-mvc.xml

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

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

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

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframework.org/schema/p"

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

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

xsi:schemaLocation="http://springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd

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

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

7. 完善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">

Archetype Created Web Application

contextConfigLocation

classpath:spring/spring-dao.xml

org.springframework.web.context.ContextLoaderListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/spring-mvc.xml

1

springmvc

/

default

*.js

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

Archetype Created Web Application

contextConfigLocation

classpath:spring/spring-dao.xml

org.springframework.web.context.ContextLoaderListener

springmvc

org.springframework.web.servlet.DispatcherServlet

contextConfigLocation

classpath:spring/spring-mvc.xml

1

springmvc

/

default

*.js

八、启动tomcat,测试xml文件是否正确

1.如启动日志没有错误信息,则xml添加无误

2.如网址:http://localhost:8080/ssmDemotest 正常展示,则xml添加无误

注:由于大部分代码均为复制,可以重启IDEA

九、controller层

1.在controller包新建类:TestController

2.在类顶部(导包语句下面)添加注解:@Controller,@RequestMapping(value = "test")

3.编写方法:

@RequestMapping("dologin")

public String dologin() {

System.out.println("====test====");

return "index";

}

十、JSP

1.在web-inf下创建目录:jsp

2.在jsp文件下创建:index.jsp

3.在index.jsp下添加:hello,ssm

十一、测试

http://localhost:8080/ssmDemotest/test/dologin

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:虚拟化平台api接口(开源虚拟化平台)
下一篇:SpringBoot中干掉Whitelabel Error Page返回自定义内容的实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~