当前位置:   article > 正文

简单易懂:SSM入门级项目整合实例教程+附项目源码_ssm项目源码

ssm项目源码


欢迎===关注===点赞===评论,共同学习,共同进步!

你的点赞、关注、评论、是我创作的动力!

-------希望我的文章对你有所帮助--------

目录

一、前言

二、推荐开发及运行环境

三、项目基本结构

四、创建一个普通JAVAEE-WEB项目

 五、搭建数据库

 六、pom.xml引入依赖

七、创建实体类

 八、创建三层架构对应模块的类和接口

九、Spring和Mybatis的整合

1、spring的配置文件:

2、Spring和Mybatis的整合配置

3、整合测试

4、测试结果

九、Spring和SpringMVC整合

1、Spring的配置

2、SpringMVC的配置

3、SSM框架整合测试

十、注意事项及其BUG反馈

1、控制台中文乱码解决办法:

 2、Mysql数据库中文乱码解决办法:

 3、服务器日志中文乱码解决办法:

4、出现no appropriate protocal 异常报错

 5、出现java.lang.NoClassDefFoundError:javax/severlet/报错

十一、总结



一、前言

  最快的提升自己的编程水平以及对某个方向的理解和运用,必然是需要动手实践,在不断的Debug中自我折磨,然后放弃!!!(玩笑)

  本篇文章只是进行基础的SSM框架(Sping+SpringMVC+Mybatis)的整合的教程,以及其中可能遇到的问题的简单解决办法!因为刚开始起手一个项目,自然需要一个过渡阶段,否则前期将会异常艰难。

  废话不多说,开始吧!

全程干货不累赘,开始发车!!

Final Test Result:

二、推荐开发及运行环境

1、系统:Windows10

2、IDE:Interilj ideal 2021.3

3、服务器:Tomacat-8(用高版本对应的Severlet-4.0会启动报错,注意!!

4、数据库:MySQL5.7

5、浏览器:FireFox

6、JavaKit:JDK18

三、项目基本结构

四、创建一个普通JAVAEE-WEB项目

1、点击File,new,project

 2、创建一个JavaEnterpri项目,选择对应JDK,命名为SSM,选择JavaEE8,然后Finish

 五、搭建数据库

1、创建SSM数据库和tb_book数据表

  1. create database SSM;
  2. create table tb_book(
  3. id int(11) primary key,
  4. name varchar(20) not null,
  5. press varchar(20) not null,
  6. author varchar(20) not null);

 2、插入数据

insert into tb_book(id,name,press,author) values(1,"Moving Earth","人民出版社","刘慈欣");

 六、pom.xml引入依赖

代码如下:(复制粘贴即可)

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <groupId>com.example</groupId>
  7. <artifactId>SSM</artifactId>
  8. <version>1.0-SNAPSHOT</version>
  9. <name>SSM</name>
  10. <packaging>war</packaging>
  11. <properties>
  12. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  13. <maven.compiler.target>1.8</maven.compiler.target>
  14. <maven.compiler.source>1.8</maven.compiler.source>
  15. <junit.version>5.8.1</junit.version>
  16. </properties>
  17. <dependencies>
  18. <dependency>
  19. <groupId>org.springframework</groupId>
  20. <artifactId>spring-context</artifactId>
  21. <version>5.2.8.RELEASE</version>
  22. </dependency>
  23. <!--Spring事务管理-->
  24. <dependency>
  25. <groupId>org.springframework</groupId>
  26. <artifactId>spring-tx</artifactId>
  27. <version>5.2.8.RELEASE</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework</groupId>
  31. <artifactId>spring-jdbc</artifactId>
  32. <version>5.2.8.RELEASE</version>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework</groupId>
  36. <artifactId>spring-test</artifactId>
  37. <version>5.2.8.RELEASE</version>
  38. </dependency>
  39. <!--Spring MVC的相关依赖-->
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-webmvc</artifactId>
  43. <version>5.2.8.RELEASE</version>
  44. </dependency>
  45. <!--MyBatis相关依赖-->
  46. <dependency>
  47. <groupId>org.mybatis</groupId>
  48. <artifactId>mybatis</artifactId>
  49. <version>3.5.2</version>
  50. </dependency>
  51. <!--MyBatis与Spring整合相关依赖-->
  52. <dependency>
  53. <groupId>org.mybatis</groupId>
  54. <artifactId>mybatis-spring</artifactId>
  55. <version>2.0.1</version>
  56. </dependency>
  57. <!--数据源-->
  58. <dependency>
  59. <groupId>com.alibaba</groupId>
  60. <artifactId>druid</artifactId>
  61. <version>1.1.20</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>javax.servlet</groupId>
  65. <artifactId>javax.servlet-api</artifactId>
  66. <version>4.0.1</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>org.apache.cxf</groupId>
  70. <artifactId>cxf-rt-frontend-jaxrs</artifactId>
  71. <version>3.4.5</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>org.junit.jupiter</groupId>
  75. <artifactId>junit-jupiter-api</artifactId>
  76. <version>${junit.version}</version>
  77. <scope>test</scope>
  78. </dependency>
  79. <dependency>
  80. <groupId>org.junit.jupiter</groupId>
  81. <artifactId>junit-jupiter-engine</artifactId>
  82. <version>${junit.version}</version>
  83. <scope>test</scope>
  84. </dependency>
  85. <dependency>
  86. <groupId>javax.servlet.jsp</groupId>
  87. <artifactId>jsp-api</artifactId>
  88. <version>2.2</version>
  89. <scope>provided</scope>
  90. </dependency>
  91. <!-- 数据库驱动相关依赖-->
  92. <dependency>
  93. <groupId>mysql</groupId>
  94. <artifactId>mysql-connector-java</artifactId>
  95. <version>8.0.11</version>
  96. </dependency>
  97. <dependency>
  98. <groupId>junit</groupId>
  99. <artifactId>junit</artifactId>
  100. <version>4.13</version>
  101. <scope>test</scope>
  102. </dependency>
  103. </dependencies>
  104. <build>
  105. <finalName>SSM</finalName>
  106. <pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
  107. <plugins>
  108. <plugin>
  109. <artifactId>maven-clean-plugin</artifactId>
  110. <version>3.1.0</version>
  111. </plugin>
  112. <!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
  113. <plugin>
  114. <artifactId>maven-resources-plugin</artifactId>
  115. <version>3.0.2</version>
  116. </plugin>
  117. <plugin>
  118. <artifactId>maven-compiler-plugin</artifactId>
  119. <version>3.8.0</version>
  120. </plugin>
  121. <plugin>
  122. <artifactId>maven-surefire-plugin</artifactId>
  123. <version>2.22.1</version>
  124. </plugin>
  125. <plugin>
  126. <artifactId>maven-war-plugin</artifactId>
  127. <version>3.2.2</version>
  128. </plugin>
  129. <plugin>
  130. <artifactId>maven-install-plugin</artifactId>
  131. <version>2.5.2</version>
  132. </plugin>
  133. <plugin>
  134. <artifactId>maven-deploy-plugin</artifactId>
  135. <version>2.8.2</version>
  136. </plugin>
  137. </plugins>
  138. </pluginManagement>
  139. <plugins>
  140. <plugin>
  141. <groupId>org.apache.maven.plugins</groupId>
  142. <artifactId>maven-war-plugin</artifactId>
  143. <version>3.3.2</version>
  144. </plugin>
  145. </plugins>
  146. <!-- 如果不添加此节点mybatis的mapper.xml文件都会被漏掉。 -->
  147. <resources>
  148. <resource>
  149. <directory>src/main/java</directory>
  150. <includes>
  151. <include>**/*.properties</include>
  152. <include>**/*.xml</include>
  153. </includes>
  154. <filtering>false</filtering>
  155. </resource>
  156. <resource>
  157. <directory>src/main/resources</directory>
  158. <includes>
  159. <include>**/*.properties</include>
  160. <include>**/*.xml</include>
  161. </includes>
  162. <filtering>false</filtering>
  163. </resource>
  164. </resources>
  165. </build>
  166. </project>

七、创建实体类

src/main/java下创建com.itheima.domain包,创建Book实体类

1-1Book.java

  1. package com.itheima.domain;
  2. public class Book {
  3. private Integer id;
  4. private String name;
  5. private String press;
  6. private String author;
  7. public Integer getId()
  8. {
  9. return id;
  10. }
  11. public void setId(Integer id){
  12. this.id=id;
  13. }
  14. public void setName(String name) {
  15. this.name = name;
  16. }
  17. public String getName() {
  18. return name;
  19. }
  20. public void setAuthor(String author) {
  21. this.author = author;
  22. }
  23. public String getAuthor() {
  24. return author;
  25. }
  26. public void setPress(String press) {
  27. this.press = press;
  28. }
  29. public String getPress() {
  30. return press;
  31. }
  32. }

 八、创建三层架构对应模块的类和接口

src/main/java下创建com.itheima.dao包,创建BookMapper接口

1-2 BookMapper.java

  1. package com.itheima.dao;
  2. import com.itheima.domain.Book;
  3. public interface BookMapper {
  4. public Book findBookById(Integer id);
  5. }

同目录下创建其映射文件

1-3BookMapper.xml

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <!DOCTYPE mapper
  3. PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  5. <mapper namespace="com.itheima.dao.BookMapper">
  6. <!--根据id查询图书信息 -->
  7. <select id="findBookById" parameterType="int"
  8. resultType="com.itheima.domain.Book">
  9. select * from tb_book where id = #{id}
  10. </select>
  11. </mapper>

     src/main/java下创建com.itheima.service包,创建BookService接口

        1-4 BookService.java

  1. package com.itheima.service;
  2. import com.itheima.domain.Book;
  3. public interface BookService {
  4. Book findBookById(Integer id);
  5. }

src/main/java下创建com.itheima.service.Impl包,创建实现类BookServiceImpl

1-5 BookServiceImpl.java

  1. package com.itheima.service.impl;
  2. import com.itheima.dao.BookMapper;
  3. import com.itheima.domain.Book;
  4. import com.itheima.service.BookService;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.stereotype.Service;
  7. @Service
  8. public class BookServiceImpl implements BookService {
  9. @Autowired
  10. private BookMapper bookMapper;
  11. public Book findBookById (Integer id)
  12. {
  13. return bookMapper.findBookById(id);
  14. }
  15. }

src/main/java下创建com.itheima.controller包,创建BookController处理器类

1-5 BookController.java


  1. package com.itheima.controller;
  2. import com.itheima.domain.Book;
  3. import com.itheima.service.BookService;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.RequestMapping;
  7. import org.springframework.web.servlet.ModelAndView;
  8. @Controller
  9. public class BookController {
  10. @Autowired
  11. private BookService bookService;
  12. @RequestMapping("/book")
  13. public ModelAndView findBookById(Integer id)
  14. {
  15. Book book=bookService.findBookById(id);
  16. ModelAndView modelAndView=new ModelAndView();
  17. modelAndView.setViewName("book.jsp");
  18. modelAndView.addObject("book",book);
  19. return modelAndView;
  20. }
  21. }

九、Spring和Mybatis的整合

1、spring的配置文件:

src/main/resources目录下创建application-service.xml配置文件

2-1 application-service.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. 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/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. ">
  11. <context:component-scan base-package="com.itheima.service"/>
  12. </beans>

2、Spring和Mybatis的整合配置

src/main/resources目录下创建属性源文件jdbc.properties,用于配置MySQL数据库

2-2 jdbc.properties

  1. jdbc.driverClassName=com.mysql.cj.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true\
  3. &characterEncoding=utf-8&serverTimezone=Asia/Shanghai
  4. jdbc.username=root
  5. jdbc.password=123456

src/main/resources目录下创建application-dao.xml配置信息

2-3 application-dao.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. 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/context
  9. http://www.springframework.org/schema/context/spring-context.xsd
  10. ">
  11. <context:property-placeholder location="classpath:jdbc.properties"/>
  12. <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
  13. <property name="driverClassName" value="${jdbc.driverClassName}"/>
  14. <property name="url" value="${jdbc.url}"/>
  15. <property name="username" value="${jdbc.username}"/>
  16. <property name="password" value="${jdbc.password}"/>
  17. </bean>
  18. <bean id="sqlSessionFactory"
  19. class="org.mybatis.spring.SqlSessionFactoryBean">
  20. <property name="dataSource" ref="dataSource"/>
  21. </bean>
  22. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  23. <property name="basePackage" value="com.itheima.dao"/>
  24. </bean>
  25. </beans>

3、整合测试

src/test/java中创建BookServiceTest类

2-4BookServiceTest.java

  1. import com.itheima.domain.Book;
  2. import com.itheima.service.BookService;
  3. import org.junit.Test;
  4. import org.junit.runner.RunWith;
  5. import org.springframework.beans.factory.annotation.Autowired;
  6. import org.springframework.test.context.ContextConfiguration;
  7. import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
  8. @RunWith(SpringJUnit4ClassRunner.class)
  9. @ContextConfiguration(locations = {"classpath:application-service.xml","classpath:application-dao.xml"})
  10. public class BookServiceTest {
  11. @Autowired
  12. private BookService bookService;
  13. @Test
  14. public void findBookById()
  15. {
  16. Book book=bookService.findBookById(2);
  17. System.out.println("id:"+book.getId());
  18. System.out.println("name:"+book.getName());
  19. System.out.println("author:"+book.getAuthor());
  20. System.out.println("press:"+book.getPress());
  21. }
  22. }

4、测试结果

测试成功:

九、Spring和SpringMVC整合

1、Spring的配置

在项目webapp/WEB-INF的web.xml中输入以下代码

3-1 web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
  5. version="4.0">
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:application-*.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>
  12. org.springframework.web.context.ContextLoaderListener
  13. </listener-class>
  14. </listener>
  15. <servlet>
  16. <servlet-name>DispatcherServlet</servlet-name>
  17. <servlet-class>
  18. org.springframework.web.servlet.DispatcherServlet
  19. </servlet-class>
  20. <init-param>
  21. <param-name>contextConfigLocation</param-name>
  22. <param-value>classpath:spring-mvc.xml</param-value>
  23. </init-param>
  24. <load-on-startup>1</load-on-startup>
  25. </servlet>
  26. <servlet-mapping>
  27. <servlet-name>DispatcherServlet</servlet-name>
  28. <url-pattern>/</url-pattern>
  29. </servlet-mapping>
  30. </web-app>

2、SpringMVC的配置

src/main/resources目录下创建Spring-mvc.xml配置信息

 3-2 spring-mvc.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:context="http://www.springframework.org/schema/context"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  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/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. <context:component-scan base-package="com.itheima.controller"/>
  13. <mvc:annotation-driven/>
  14. </beans>

3、SSM框架整合测试

src/main/webapp下创建book.jsp文件

3-4 book.jsp

  1. <%@ page contentType="text/html;charset=UTF-8" language="java" %>
  2. <html>
  3. <head>
  4. <title>图书信息查询</title>
  5. </head>
  6. <body>
  7. <table border="1">
  8. <tr>
  9. <th>图书id</th>
  10. <th>图书名称</th>
  11. <th>出版社</th>
  12. <th>作者</th>
  13. </tr>
  14. <tr>
  15. <td>${book.id}</td>
  16. <td>${book.name}</td>
  17. <td>${book.press}</td>
  18. <td>${book.author}</td>
  19. </tr>
  20. </table>
  21. </body>
  22. </html>

启动Tomcat8,在浏览器输入

http://localhost:8082/SSM_war_exploded/book?id=1

 其中8082为端口号,我的填的是8082,默认是8080,根据自己的配置改就行!

 结果:

十、注意事项及其BUG反馈

1、控制台中文乱码解决办法:

Settings->Editor->File Encodings->三个部分如图都改为GBK即可

 2、Mysql数据库中文乱码解决办法:

修改数据库字符集 :

alter database SSM default convert to character set utf8;

 3、服务器日志中文乱码解决办法:

找到对应的Tomcat-8存储地址,在conf中找到

logging.properties文件,将全部GBk替换成UTF-8即可

 

4、出现no appropriate protocal 异常报错

找到对应位置jdk,把java.security中的

dk.tls.disabledAlgorithms=后的SSLv3,TLSv1,TLSv1.1删除,

如图大概在724行,删除后如图:

 

 5、出现java.lang.NoClassDefFoundError:javax/severlet/报错

是由于Tomcat高版本不支持4.0版本的Sevrlet导致,更换低版本Tomcat即可

十一、总结

  总体来说,当开始着手一个WEB项目时,总会由于各种原因导致各种BUG,需要花费大量的时间去排查和调试,才能一步步把最后的项目运行出来,因此需要考虑的方面有很多,重要的是必须看懂编译器报错的原因,从而分析出可行的解决办法。

 只有不断的鞭策自己去实践和学习,积累Debug的能力和经验,才能慢慢获得进步!

发文不易,恳请大佬们高抬贵手!


点赞:随手点赞是种美德,是大佬们对于本人创作的认可!


评论:往来无白丁,是你我交流的的开始!


收藏:愿君多采撷,是大佬们对在下的赞赏!

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号