用maven对ssm进行整合

Posted Pansin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用maven对ssm进行整合相关的知识,希望对你有一定的参考价值。

网上有很多ssm整合的教程,这里给像我这样的初学的新手提供一个整合方法,同时也记录一下自己的学习进度。

同时推荐观看相关视频  https://www.bilibili.com/video/av53621216/ 推荐老师讲的spring和springMVC这两个框架的整合贼棒!(1p—6p),spring和mabatis的话在找找资料看看,感觉我这个写的太乱了(/苦恼)

数据库相关:因为是测试整合的,所以数据库设计特别简单:

数据库名:blog;表名:user;

表中数据:

1.创建maven项目

2.初始项目结构:

3.在创建的初始目录结构中:src/main/java这个是放置java文件,而src/main/resources放置配置文件。

在pom.xml中引入jar包坐标:

  1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2   <modelVersion>4.0.0</modelVersion>
  3   <groupId>com.hhxy</groupId>
  4   <artifactId>blog</artifactId>
  5   <version>0.0.1-SNAPSHOT</version>
  6   <packaging>war</packaging>
  7   
  8   
  9   <properties>
 10         <spring.version>5.0.2.RELEASE</spring.version>
 11         <slf4j.version>1.6.6</slf4j.version>
 12         <log4j.version>1.2.12</log4j.version>
 13         <mysql.version>5.1.6</mysql.version>
 14         <mybatis.version>3.4.5</mybatis.version>
 15     </properties>
 16 
 17     <dependencies>
 18 
 19         <!-- spring -->
 20         <dependency>
 21             <groupId>org.aspectj</groupId>
 22             <artifactId>aspectjweaver</artifactId>
 23             <version>1.6.8</version>
 24         </dependency>
 25 
 26         <dependency>
 27             <groupId>org.springframework</groupId>
 28             <artifactId>spring-aop</artifactId>
 29             <version>${spring.version}</version>
 30         </dependency>
 31 
 32         <dependency>
 33             <groupId>org.springframework</groupId>
 34             <artifactId>spring-context</artifactId>
 35             <version>${spring.version}</version>
 36         </dependency>
 37 
 38 
 39         <dependency>
 40             <groupId>org.springframework</groupId>
 41             <artifactId>spring-web</artifactId>
 42             <version>${spring.version}</version>
 43         </dependency>
 44 
 45         <dependency>
 46             <groupId>org.springframework</groupId>
 47             <artifactId>spring-webmvc</artifactId>
 48             <version>${spring.version}</version>
 49         </dependency>
 50 
 51         <dependency>
 52             <groupId>org.springframework</groupId>
 53             <artifactId>spring-test</artifactId>
 54             <version>${spring.version}</version>
 55         </dependency>
 56 
 57         <dependency>
 58             <groupId>org.springframework</groupId>
 59             <artifactId>spring-tx</artifactId>
 60             <version>${spring.version}</version>
 61         </dependency>
 62 
 63         <dependency>
 64             <groupId>org.springframework</groupId>
 65             <artifactId>spring-jdbc</artifactId>
 66             <version>${spring.version}</version>
 67         </dependency>
 68 
 69         <dependency>
 70             <groupId>junit</groupId>
 71             <artifactId>junit</artifactId>
 72             <version>4.12</version>
 73             <scope>compile</scope>
 74         </dependency>
 75 
 76         <dependency>
 77             <groupId>mysql</groupId>
 78             <artifactId>mysql-connector-java</artifactId>
 79             <version>${mysql.version}</version>
 80         </dependency>
 81 
 82         <dependency>
 83             <groupId>javax.servlet</groupId>
 84             <artifactId>servlet-api</artifactId>
 85             <version>2.5</version>
 86             <scope>provided</scope>
 87         </dependency>
 88 
 89         <dependency>
 90             <groupId>javax.servlet.jsp</groupId>
 91             <artifactId>jsp-api</artifactId>
 92             <version>2.0</version>
 93             <scope>provided</scope>
 94         </dependency>
 95 
 96         <dependency>
 97             <groupId>jstl</groupId>
 98             <artifactId>jstl</artifactId>
 99             <version>1.2</version>
100         </dependency>
101 
102         <!-- log start -->
103         <dependency>
104             <groupId>log4j</groupId>
105             <artifactId>log4j</artifactId>
106             <version>${log4j.version}</version>
107         </dependency>
108 
109         <dependency>
110             <groupId>org.slf4j</groupId>
111             <artifactId>slf4j-api</artifactId>
112             <version>${slf4j.version}</version>
113         </dependency>
114 
115         <dependency>
116             <groupId>org.slf4j</groupId>
117             <artifactId>slf4j-log4j12</artifactId>
118             <version>${slf4j.version}</version>
119         </dependency>
120         <!-- log end -->
121         <dependency>
122             <groupId>org.mybatis</groupId>
123             <artifactId>mybatis</artifactId>
124             <version>${mybatis.version}</version>
125         </dependency>
126 
127         <dependency>
128             <groupId>org.mybatis</groupId>
129             <artifactId>mybatis-spring</artifactId>
130             <version>1.3.0</version>
131         </dependency>
132 
133         <dependency>
134             <groupId>c3p0</groupId>
135             <artifactId>c3p0</artifactId>
136             <version>0.9.1.2</version>
137             <type>jar</type>
138             <scope>compile</scope>
139         </dependency>
140 
141     </dependencies>
142 <build>
143         <finalName>ssm</finalName>
144         <pluginManagement>
145             <plugins>
146                 <plugin>
147                     <groupId>org.apache.maven.plugins</groupId>
148                     <artifactId>maven-compiler-plugin</artifactId>
149                     <version>3.2</version>
150                     <configuration>
151                         <source>1.8</source>
152                         <target>1.8</target>
153                         <encoding>UTF-8</encoding>
154                         <showWarnings>true</showWarnings>
155                     </configuration>
156                 </plugin>
157             </plugins>
158         </pluginManagement>
159     </build>
160     
161 </project>

 

 

4.创建好项目后,在src/main/java中创建包:如图所示:

5.根据数据库设计实体类,同时在控制层、mapper层、service层创建相应的类。如图:

6.开始的准备步骤准备完毕,接下来开始整合。

ssm整合是spring+springMVC+mybatis这三个框架进行整合,是要拿spring框架对springMVC和mybatis进行整合。

7.下面对三个框架进行整合:

创建applicationContext.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     <!-- =============开启注解扫描Spring中只需要扫描service和mapper============ -->
17     <context:component-scan base-package="com.hhxy.plx" >
18         <!--配置哪些注解不扫描exclude  是不处理的-->
19         <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
20     </context:component-scan>
21     
22     <!-- ==========================读取jdbc工具类==================================== -->
23     <context:property-placeholder location="classpath:jdbc.properties" />
24     <bean id="pooledDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
25         <property name="driverClass" value="${jdbc.driver}"></property>
26         <property name="jdbcUrl" value="${jdbc.url}"></property>
27         <property name="user" value="${jdbc.username}"></property>
28         <property name="password" value="${jdbc.password}"></property>
29     </bean>
30     
31     
32     <!--==============================配置和MyBatis的整合====================== -->
33     <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
34         <!-- 指定mybatis全局配置文件的位置 -->
35         <property name="configLocation" value="classpath:mybatis-config.xml"></property>
36         <property name="dataSource" ref="pooledDataSource"></property>
37         <!-- 指定mybatis,mapper文件的位置 -->
38         <property name="mapperLocations" value="classpath*:com/hhxy/plx/mapper/*.xml"></property>
39     </bean>
40     
41     
42     
43     <!-- ======================配置扫描器,将mybatis接口的实现加入到ioc容器中=========-->
44     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
45         <!--扫描所有mapper接口的实现,加入到ioc容器中 -->
46         <property name="basePackage" value="com.hhxy.plx.mapper"></property>
47     </bean>
52     
53     </beans>

8.创建springmvc.xml

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3        xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context"
 4        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5        xsi:schemaLocation="
 6         http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/mvc
 9         http://www.springframework.org/schema/mvc/spring-mvc.xsd
10         http://www.springframework.org/schema/context
11         http://www.springframework.org/schema/context/spring-context.xsd">
12 <!-- 开启注解扫描 只扫描Controller -->
13     <context:component-scan base-package="com.hhxy.plx">
14         <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller" />
15     </context:component-scan>
16 <!--配置的视图解析器对象-->
17     <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
18         <property name="prefix" value="/WEB-INF/pages/"/>
19         <property name="suffix" value=".jsp"/>
20     </bean>
21 
22     <!--过滤静态资源-->
23     <mvc:resources location="/css/" mapping="/css/**" />
24     <mvc:resources location="/images/" mapping="/images/**" />
25     <mvc:resources location="/js/" mapping="/js/**" />
26 
27     <!--开启SpringMVC注解的支持-->
28     <mvc:annotation-driven/>
29 
30 
31 
32 
33 
34 </beans>

9.创建mybatis-config.xml  这个可以不用写东西,但是它一定要有。

1 <?xml version="1.0" encoding="UTF-8"?>
2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
3 "http://mybatis.org/dtd/mybatis-3-config.dtd">
4 <configuration>
5    
6   
7 </configuration>

10.创建log4j.properties

1 jdbc.driver=com.mysql.jdbc.Driver
2 jdbc.url=jdbc:mysql://localhost:3306/blog?characterEncoding=utf-8
3 jdbc.username=root
4 jdbc.password=root

11.web.xml相关配置:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <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" id="WebApp_ID" version="3.0">
 3   <display-name>blog</display-name>
 4   <welcome-file-list>
 5     <welcome-file>index.html</welcome-file>
 6     <welcome-file>index.htm</welcome-file>
 7     <welcome-file>index.jsp</welcome-file>
 8     <welcome-file>default.html</welcome-file>
 9     <welcome-file>default.htm</welcome-file>
10     <welcome-file>default.jsp</welcome-file>
11   </welcome-file-list>
12   
13   
14   
15   
16   <!-- Spring 整合  Springweb配置Spring监听器 -->
17   <!--配置Spring的监听器,默认只加载WEB-INF目录下的applicationContext.xml配置文件-->
18   <listener>
19     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
20   </listener>
21   <!--设置配置文件的路径-->
22   <context-param>
23     <param-name>contextConfigLocation</param-name>
24     <param-value>classpath:applicationContext.xml</param-value>
25   </context-param>
26   <context-param>
27     <param-name/>
28     <param-value/>
29   </context-param>
30 
31   <!-- 此部分为springMVC的配置 -->
32   
33   
34   <!--配置前端控制器-->
35   <servlet>
36     <servlet-name>dispatcherServlet</servlet-name>
37     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
38     <!--加载springmvc.xml配置文件-->
39     <init-param>
40       <param-name>contextConfigLocation</param-name>
41       <param-value>classpath:springmvc.xml</param-value>
42     </init-param>
43     <!--启动服务器,创建该servlet-->
44     <load-on-startup>1</load-on-startup>
45   </servlet>
46   <servlet-mapping>
47     <servlet-name>dispatcherServlet</servlet-name>
48     <url-pattern>/</url-pattern>
49   </servlet-mapping>
50 
51   <!--解决中文乱码的过滤器-->
52   <filter>
53     <filter-name>characterEncodingFilter</filter-name>
54     <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
55     <init-param>
56       <param-name>encoding</param-name>
57       <param-value>UTF-8</param-value>
58     </init-param>
59   </filter>
60   <filter-mapping>
61     <filter-name>characterEncodingFilter</filter-name>
62     <url-pattern>/*</url-pattern>
63   </filter-mapping>
64   
65   
66   
67   
68 </web-app>

12.上面就是所有配置文件。

UserController:

 1 package com.hhxy.plx.controller;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.stereotype.Controller;
 5 import org.springframework.web.bind.annotation.RequestMapping;
 6 
 7 import com.hhxy.plx.entity.User;
 8 import com.hhxy.plx.service.UserService;
 9 
10 @Controller
11 @RequestMapping("/user")
12 public class UserController {
13     @Autowired
14     private UserService userService;
15     @RequestMapping("/login")
16     public String findAll() {
17         System.out.println("Controller执行成功!!");
18         userService.findAll();
19         return "login";
20         
21     }
22     @RequestMapping("/save")
23     public String save(User user) {
24     userService.saveUser(user);
25         return "login";
26         
27     }
28 
29 }

User实体类:

 1 package com.hhxy.plx.entity;
 2 
 3 public class User {
 4     private int id;
 5     private String username;
 6     private String password;
 7     public int getId() {
 8         return id;
 9     }
10     public void setId(int id) {
11         this.id = id;
12     }
13     public String getUsername() {
14         return username;
15     }
16     public void setUsername(String username) {
17         this.username = username;
18     }
19     public String getPassword() {
20         return password;
21     }
22     public void setPassword(String password) {
23         this.password = password;
24     }
25     
26     
27     
28 }

UserMapper接口类:

1 package com.hhxy.plx.mapper;
2 
3 import com.hhxy.plx.entity.User;
4 
5 public interface UserMapper {
6 
7     void saveUser(User user);
8 
9 }

UserMapper.xml:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
 3 <mapper namespace="com.hhxy.plx.mapper.UserMapper">
 4   <resultMap id="BaseResultMap" type="com.hhxy.plx.mapper.UserMapper">
 5     <id column="id" jdbcType="INTEGER" property="id" />
 6     <result column="username" jdbcType="VARCHAR" property="username" />
 7     <result column="password" jdbcType="VARCHAR" property="password" />
 8   
 9   </resultMap>
10    <select id="findAll" parameterType="com.hhxy.plx.entity.User" resultMap="BaseResultMap">
11    select * from user
12    </select>
13   <insert id="saveUser" parameterType="com.hhxy.plx.entity.User">
14   
15   insert into user(username,password) values (#{username},#{password})
16   </insert>
17   
18   
19   
20   </mapper>

UserService接口类:

 1 package com.hhxy.plx.service;
 2 
 3 import java.util.List;
 4 
 5 import com.hhxy.plx.entity.User;
 6 
 7 public interface UserService {
 8 
 9     public List<User> findAll();
10 
11     public void saveUser(User user);
12 
13 
14     
15 
16 }

UserServiceImpl实现类:

 1 package com.hhxy.plx.service;
 2 
 3 import java.util.List;
 4 
 5 import org.springframework.beans.factory.annotation.Autowired;
 6 import org.springframework.stereotype.Service;
 7 
 8 import com.hhxy.plx.entity.User;
 9 import com.hhxy.plx.mapper.UserMapper;
10 @Service("userService")
11 public class UserServiceImpl implements UserService {
12 @Autowired
13 private UserMapper userMapper;
14     
15     public List<User> findAll(){
16         System.out.println("ServiceImpl调用成功!!!!");
17         return null;
18         
19     }
20 
21     @Override
22     public void saveUser(User user) {
23         System.out.println("保存用户!!!");
24         userMapper.saveUser(user);
25         
26     }
27     
28     
29 }

index.jsp:

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 <a href="user/login">测试跳转</a>
11 <form action="user/save" method="post">
12 账号:<input type="text" name="username"><br/>
13 密码:<input type="text" name="password"><br/>
14 
15 <input type="submit" value="保存">
16 
17 
18 </form>
19 
20 </body>
21 </html>

login.jsp

 1 <%@ page language="java" contentType="text/html; charset=utf-8"
 2     pageEncoding="utf-8"%>
 3 <!DOCTYPE html>
 4 <html>
 5 <head>
 6 <meta charset="utf-8">
 7 <title>Insert title here</title>
 8 </head>
 9 <body>
10 login页面
11 
12 </body>
13 </html>

测试:

输入要保存的账号密码:跳转保存。

刷新数据库:

以上是关于用maven对ssm进行整合的主要内容,如果未能解决你的问题,请参考以下文章

Maven创建Web项目整合SSM框架

SSM项目整合,适合新手

ssm框架整合,配置文件中的配置内容

005商城项目:ssm框架的整合成功之后的问题:使用maven的tomcat插件时的debug

史上最详细的IDEA优雅整合Maven+SSM框架(详细思路+附带源码)

ssm整合实现图书管理系统