码迷,mamicode.com
首页 > 其他好文 > 详细

idea搭建SSM的maven项目(tomcat容器)

时间:2019-01-30 00:18:18      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:mysq   接收参数   完成   单元   注解   finish   OLE   前端控制器   前端   

一.创建maven的web项目

(1)选择项目的骨架

技术分享图片

 

(2)写项目的坐标

技术分享图片

(3)maven的设置

设置maven的本地仓库,以及配置文件的位置,同时点击+号,填入archetypeCatalog和internal,让maven读取本地配置即可。

技术分享图片

然后点击next

(4)创建完成

技术分享图片

点击finish

二.添加目录并导入依赖

(一)添加java目录,main目录下创建java目录,并且将其标记为Sources Root

技术分享图片

 

(2) 在main文件夹下创建resources目录,并将其标记为Resources Root

技术分享图片

(如果有需要,可以在src目录下创建test文件夹,并创建java和resources文件夹,并将它们分别标记为Test Resources Root和Test Resources Root,这里就不创建了)

最后目录结构如下

技术分享图片

(3)pom文件的编写(导入相关依赖)

maven.compiler.source和maven.compiler.target標簽中写jdk的版本

  1 <properties>
  2     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  3     <maven.compiler.source>1.8</maven.compiler.source> 
  4     <maven.compiler.target>1.8</maven.compiler.target>
  5 
  6     <!--定义版本号   ${org.springframework.version}-->
  7     <org.springframework.version>5.0.7.RELEASE</org.springframework.version>
  8     <org.mybatis.version>3.4.6</org.mybatis.version>
  9 
 10   </properties>
 11 
 12   <dependencies>
 13 
 14     <!--单元测试-->
 15     <dependency>
 16       <groupId>junit</groupId>
 17       <artifactId>junit</artifactId>
 18       <version>4.12</version>
 19       <scope>test</scope>
 20     </dependency>
 21     <!--lombok-->
 22     <dependency>
 23       <groupId>org.projectlombok</groupId>
 24       <artifactId>lombok</artifactId>
 25       <version>1.16.6</version>
 26     </dependency>
 27     <!-- servlet-api -->
 28     <dependency>
 29       <groupId>javax.servlet</groupId>
 30       <artifactId>servlet-api</artifactId>
 31       <version>2.5</version>
 32       <scope>provided</scope>
 33     </dependency>
 34     <!--mysql驱动-->
 35     <dependency>
 36       <groupId>mysql</groupId>
 37       <artifactId>mysql-connector-java</artifactId>
 38       <version>5.1.21</version>
 39     </dependency>
 40     <!-- druid -->
 41     <dependency>
 42       <groupId>com.alibaba</groupId>
 43       <artifactId>druid</artifactId>
 44       <version>1.0.14</version>
 45     </dependency>
 46     <!-- common-lang -->
 47     <dependency>
 48       <groupId>commons-lang</groupId>
 49       <artifactId>commons-lang</artifactId>
 50       <version>2.6</version>
 51     </dependency>
 52     <!--spring-test-->
 53     <dependency>
 54       <groupId>org.springframework</groupId>
 55       <artifactId>spring-test</artifactId>
 56       <version>${org.springframework.version}</version>
 57       <scope>test</scope>
 58     </dependency>
 59     <!--spring-core-->
 60     <dependency>
 61       <groupId>org.springframework</groupId>
 62       <artifactId>spring-core</artifactId>
 63       <version>${org.springframework.version}</version>
 64     </dependency>
 65     <!--spring-context-->
 66     <dependency>
 67       <groupId>org.springframework</groupId>
 68       <artifactId>spring-context</artifactId>
 69       <version>${org.springframework.version}</version>
 70     </dependency>
 71     <!--spring-context-support-->
 72     <dependency>
 73       <groupId>org.springframework</groupId>
 74       <artifactId>spring-context-support</artifactId>
 75       <version>${org.springframework.version}</version>
 76     </dependency>
 77     <!--spring-expression-->
 78     <dependency>
 79       <groupId>org.springframework</groupId>
 80       <artifactId>spring-expression</artifactId>
 81       <version>${org.springframework.version}</version>
 82     </dependency>
 83     <!--spring-jdbc-->
 84     <dependency>
 85       <groupId>org.springframework</groupId>
 86       <artifactId>spring-jdbc</artifactId>
 87       <version>${org.springframework.version}</version>
 88     </dependency>
 89     <!--spring-tx-->
 90     <dependency>
 91       <groupId>org.springframework</groupId>
 92       <artifactId>spring-tx</artifactId>
 93       <version>${org.springframework.version}</version>
 94     </dependency>
 95     <!--spring-web-->
 96     <dependency>
 97       <groupId>org.springframework</groupId>
 98       <artifactId>spring-web</artifactId>
 99       <version>${org.springframework.version}</version>
100     </dependency>
101     <!--spring-aop-->
102     <dependency>
103       <groupId>org.springframework</groupId>
104       <artifactId>spring-aop</artifactId>
105       <version>${org.springframework.version}</version>
106     </dependency>
107     <!--spring-webmvc-->
108     <dependency>
109       <groupId>org.springframework</groupId>
110       <artifactId>spring-webmvc</artifactId>
111       <version>${org.springframework.version}</version>
112     </dependency>
113 
114     <!-- aspectj -->
115     <dependency>
116       <groupId>org.aspectj</groupId>
117       <artifactId>aspectjrt</artifactId>
118       <version>1.7.4</version>
119     </dependency>
120     <!--aspectj weaver-->
121     <dependency>
122       <groupId>org.aspectj</groupId>
123       <artifactId>aspectjweaver</artifactId>
124       <version>1.7.4</version>
125     </dependency>
126 
127     <!-- cglib -->
128     <dependency>
129       <groupId>cglib</groupId>
130       <artifactId>cglib</artifactId>
131       <version>3.1</version>
132     </dependency>
133 
134     <!-- mybatis -->
135     <dependency>
136       <groupId>org.mybatis</groupId>
137       <artifactId>mybatis</artifactId>
138       <version>${org.mybatis.version}</version>
139     </dependency>
140     <!--mybatis-spring-->
141     <dependency>
142       <groupId>org.mybatis</groupId>
143       <artifactId>mybatis-spring</artifactId>
144       <version>1.3.0</version>
145     </dependency>
146 
147     <!-- jackson-core -->
148     <dependency>
149       <groupId>com.fasterxml.jackson.core</groupId>
150       <artifactId>jackson-core</artifactId>
151       <version>2.9.4</version>
152     </dependency>
153     <!--jackson-databind-->
154     <dependency>
155       <groupId>com.fasterxml.jackson.core</groupId>
156       <artifactId>jackson-databind</artifactId>
157       <version>2.9.4</version>
158     </dependency>
159     <!--jackson-annotations-->
160     <dependency>
161       <groupId>com.fasterxml.jackson.core</groupId>
162       <artifactId>jackson-annotations</artifactId>
163       <version>2.9.4</version>
164     </dependency>
165 
166     <!--slf4j-api-->
167     <dependency>
168       <groupId>org.slf4j</groupId>
169       <artifactId>slf4j-api</artifactId>
170       <version>1.7.6</version>
171     </dependency>
172     <!--slf4j-log4j12-->
173     <dependency>
174       <groupId>org.slf4j</groupId>
175       <artifactId>slf4j-log4j12</artifactId>
176       <version>1.7.6</version>
177     </dependency>
178     <!--log4j-->
179     <dependency>
180       <groupId>log4j</groupId>
181       <artifactId>log4j</artifactId>
182       <version>1.2.17</version>
183     </dependency>
184     <!--commons-fileupload-->
185     <dependency>
186       <groupId>commons-fileupload</groupId>
187       <artifactId>commons-fileupload</artifactId>
188       <version>1.3.1</version>
189     </dependency>
190     <!-- jstl -->
191     <dependency>
192       <groupId>jstl</groupId>
193       <artifactId>jstl</artifactId>
194       <version>1.2</version>
195     </dependency>
196     <!-- standard -->
197     <dependency>
198       <groupId>taglibs</groupId>
199       <artifactId>standard</artifactId>
200       <version>1.1.2</version>
201     </dependency>
202     <!--pagehelper-->
203     <dependency>
204       <groupId>com.github.pagehelper</groupId>
205       <artifactId>pagehelper</artifactId>
206       <version>4.1.4</version>
207     </dependency>
208 
209 
210   </dependencies>

(4) 配置文件的编写

创建基本的文件夹,并添加文件,如图

技术分享图片

mybatis配置的编写,整合spring,创建application-mybatis.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:aop="http://www.springframework.org/schema/aop"
 6        xmlns:tx="http://www.springframework.org/schema/tx"
 7        xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context.xsd
11     http://www.springframework.org/schema/aop
12     http://www.springframework.org/schema/aop/spring-aop.xsd
13      http://www.springframework.org/schema/tx
14         http://www.springframework.org/schema/tx/spring-tx.xsd
15 ">
16 
17     <!--spring-Mybatis整合-->
18     <!--加载数据库属性文件-->
19     <context:property-placeholder location="classpath:db.properties"/>
20     <!--连接池-->
21     <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
22         <property name="driverClassName" value="${jdbc.driver}" />
23         <!--属性文件当中的名称不能和name名称一样-->
24         <property name="url" value="${jdbc.url}" />
25         <property name="username" value="${jdbc.username}" />
26         <property name="password" value="${jdbc.password}" />
27     </bean>
28     <!-- 配置事务管理器 -->
29     <bean id="transactionManager"
30           class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
31         <!-- 数据源 -->
32         <property name="dataSource" ref="dataSource" />
33     </bean>
34     <!-- 开启注解事务 -->
35     <tx:annotation-driven transaction-manager="transactionManager"/>
36 
37     <!-- Mybatis的工厂 -->
38     <bean id="sqlSessionFactoryBean" class="org.mybatis.spring.SqlSessionFactoryBean">
39         <property name="dataSource" ref="dataSource"/>
40         <!-- 核心配置文件的位置 -->
41         <property name="configLocation" value="classpath:sqlMapConfig.xml"/>
42         <!--配置mapper映射文件的路径-->
43         <!--<property name="mapperLocations" value="classpath:com.qfssc.mapper/*Mapper.xml"/>-->
44     </bean>
45 
46     <!-- 配置Mapper接口扫描 -->
47     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
48         <!-- 配置Mapper扫描包 -->
49         <property name="basePackage" value="com.qfssc.mapper" />
50     </bean>
51 
52 </beans>

数据库连接的配置文件db.properties的编写

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis?characterEncoding=utf-8
jdbc.username=root
jdbc.password=1234

 springmvc的相关配置,编写application-mvc.xml:

 1 <?xml version="1.0" encoding="UTF-8" ?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4        xmlns:context="http://www.springframework.org/schema/context"
 5        xmlns:mvc="http://www.springframework.org/schema/mvc"
 6        xsi:schemaLocation="http://www.springframework.org/schema/beans
 7     http://www.springframework.org/schema/beans/spring-beans.xsd
 8     http://www.springframework.org/schema/context
 9     http://www.springframework.org/schema/context/spring-context.xsd
10     http://www.springframework.org/schema/mvc
11     http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
12     ">
13 
14     <mvc:annotation-driven />
15     <!--静态资源处理 -->
16     <mvc:default-servlet-handler/>
17     <!--视图解析器 -->
18     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
19         <property name="prefix" value="/WEB-INF/views/"/>
20         <property name="suffix" value=".jsp" />
21     </bean>
22 
23     <!--配置请求拦截器,拦截所有请求,用到的时候再进行配置拦截器-->
24     <!--<mvc:interceptors>
25         <mvc:interceptor>
26             <mvc:mapping path="/*"/>
27             <bean class="com.qfssc.web.interceptor.RequestInterceptor"></bean>
28         </mvc:interceptor>
29     </mvc:interceptors>-->
30 
31 </beans>

spring核心配置文件applicationContext.xml文件的编写:

<?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: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
">
    <!--注解扫描-->
    <context:component-scan base-package="com.qfssc"/>
    <!--导入mybatis-->
    <import resource="classpath:application-mybatis.xml"/>

    <!--导入springMVC-->
    <import resource="classpath:application-mvc.xml"/>
    
</beans>

编写web.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<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_3_0.xsd" 
 metadata-complete="false" version="3.0">
  <absolute-ordering/>
  <display-name>web</display-name>

  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>

  <!--配置前端控制器-->
  <servlet>
    <servlet-name>SpringMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <!--加载spring主配置文件-->
      <param-value>classpath:applicationContext.xml</param-value>
    </init-param>
    <!--项目一启动就加载框架-->
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>SpringMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

  <!--编码过滤器-->
  <filter>
    <filter-name>CharacterEncodingFilter</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>CharacterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

log4j的配置文件log4j.properties编写,

1 # Global logging configuration
2 log4j.rootLogger=ERROR, stdout
3 # MyBatis logging configuration...
4 log4j.logger.com._520it.mybatis=TRACE
5 # Console output...
6 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
7 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
8 log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

三.配置本地tomcat

点击Add Configuration

技术分享图片

 

按部就班,然后点击ok

技术分享图片

jre,url,和端口号一般默认就行了

技术分享图片

依次点击

技术分享图片

选择第二个选项,这样支持热部署,便于开发,点击ok

技术分享图片

再点击server

技术分享图片

再将箭头指向的位置都选成update classes and resources(支持热部署)

技术分享图片

最后点击apply,点击ok

然后就可以看到tom猫了

技术分享图片

点击三角形,启动tomcat.

技术分享图片

如果以上步骤全部正确,浏览器直接打开,进入index.jsp,看到Hello World!

四.检测SpringMVC的可用性

在index.jsp中写一个表单向服务器发送请求

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<body>
<h2>Hello World!</h2>
<form action="${pageContext.request.contextPath}/student">
    姓名:<input type="text"><br>
    <input type="submit" value="提交">
</form>
</body>
</html>

编写web层,写一个StudentController,转发到success.jsp页面,只是为了测试一下,不接收参数了。。。

package com.qfssc.web;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class StudentController {
    @RequestMapping("/student")
    public String teacher(){
        return "success";
    }
}

在views文件夹下创建success.jsp页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<h1>success</h1>
</body>
</html>

在index.jsp页面里点击提交

技术分享图片

无论是否有参数,都会跳转到success.jsp页面

技术分享图片

说明我们springmvc生效了(mybatis自行测试,这里不做过多介绍了)

 

 

 

 

 

 

 

 

  

 

idea搭建SSM的maven项目(tomcat容器)

标签:mysq   接收参数   完成   单元   注解   finish   OLE   前端控制器   前端   

原文地址:https://www.cnblogs.com/wwjj4811/p/10336253.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!