Web:Maven整合SSM框架

拼搏现实的明天。 2022-02-01 15:49 410阅读 0赞

Web:使用maven整合SSM框架

  • 概述:
    • 1.首先来看项目的目录结构
    • 2.然后在java包下创建子包
    • 3.接着完善resources包:
    • 4.Web下的目录结构
    • 5.配置maven依赖 **pom.xml**(运行环境是 Tomcat8.5,Spring4.0,jdk1.8)
    • 6.配置Spring-*.xml
    • 7.数据库中创建相应的表
    • 8.创建entity类
    • 9.创建Dao接口(数据交互)
    • 10.在resources包的mapper子包下创建 **AppointmentMapper.xml**和**BookMapper.xml**
    • 11.Service层相关。
    • 12.Controller层相关实现

概述:

学完了SSM之后,跟着GitHub上的一篇博客做了下整合,可能因为是eclipse的或者版本的问题,期间出现了不少问题。然后记录了一下自己在idea上面搭建好的SSM整合方案。
原博客地址:https://blog.csdn.net/qq598535550/article/details/51703190

1.首先来看项目的目录结构

一、创建项目的大致文件结构
在这里插入图片描述
在这里插入图片描述
这里注意几点:
1、如果使用maven的骨架创建webapp,需要自己在main目录下新建java包和resources包,并且需要把java包和resources包都标记为源文件目录。(java包下放一些源代码文件,resources包下放一些配置文件)
在这里插入图片描述

2.然后在java包下创建子包

(其中dto和exception其实可以不要)这里每个包的用途应该很显然吧。
controller包:放的是一些SpringMVC下的控制类。@Controller修饰的类
dao包:放的基本是一些与数据打交道的类,与数据库、缓存相关的。
dto包:主要是一些处理web层与service层数据传输的类。
entity包:这个就是你的实体类bean啊什么的,都放在这里。
exception包:这个就是放一些自定义的错误处理的类。
service包:这差不多就是Spring相关的一些类吧,主要是用来写我们的业务逻辑。@Service修饰的类。
在这里插入图片描述

3.接着完善resources包:

mapper包:mybatis中的一些sql映射的实现(xxxMapper.xml)。

spring包:主要是spring和springmvc的配置文件,这里为了方便管理分成了三个,建议写两个(spring.xml和springMvc.xml)。
spring-dao.xml:主要处理数据源(就是整合mybatis的配置)相关的配置,主要三部分:数据库连接池,注册SqlSessionFactory,扫描所有mapper文件。

spring-service.xml:扫描所有@Service注解,注册TransactionManager,开启基于注解的声明式事务。
spring-web.xml:这里配置web相关的,配置支持静态资源加载;开启注解模式;配置视图解析器;扫描所有@Controller注解。
jdbc.properties是数据库连接的配置文件,log4j是日志的配置文件,然后还需配置mybatis-conf.xml就是mybatis的全局配置文件。
在这里插入图片描述

4.Web下的目录结构

这里resources下的一些包主要是规范,这里没怎么用到,WEB-INF下配置一个jsp文件夹用来存放jsp文件,WEB-INF目录下的文件由于安全性是无法直接访问到的。
在这里插入图片描述

5.配置maven依赖 pom.xml(运行环境是 Tomcat8.5,Spring4.0,jdk1.8)

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  3. <modelVersion>4.0.0</modelVersion>
  4. <groupId>com.soecode.ssm</groupId>
  5. <artifactId>ssm</artifactId>
  6. <packaging>war</packaging>
  7. <version>0.0.1-SNAPSHOT</version>
  8. <name>ssm Maven Webapp</name>
  9. <url>http://github.com/liyifeng1994/ssm</url>
  10. <dependencies>
  11. <!-- 单元测试 -->
  12. <dependency>
  13. <groupId>junit</groupId>
  14. <artifactId>junit</artifactId>
  15. <version>4.11</version>
  16. </dependency>
  17. <!-- 1.日志 -->
  18. <!-- 实现slf4j接口并整合 -->
  19. <dependency>
  20. <groupId>ch.qos.logback</groupId>
  21. <artifactId>logback-classic</artifactId>
  22. <version>1.1.1</version>
  23. </dependency>
  24. <!-- 2.数据库 -->
  25. <dependency>
  26. <groupId>mysql</groupId>
  27. <artifactId>mysql-connector-java</artifactId>
  28. <version>8.0.12</version>
  29. <scope>runtime</scope>
  30. </dependency>
  31. <dependency>
  32. <groupId>com.mchange</groupId>
  33. <artifactId>c3p0</artifactId>
  34. <version>0.9.5.2</version>
  35. </dependency>
  36. <!-- DAO: MyBatis -->
  37. <dependency>
  38. <groupId>org.mybatis</groupId>
  39. <artifactId>mybatis</artifactId>
  40. <version>3.5.1</version>
  41. </dependency>
  42. <dependency>
  43. <groupId>org.mybatis</groupId>
  44. <artifactId>mybatis-spring</artifactId>
  45. <version>1.3.0</version>
  46. </dependency>
  47. <!-- 3.Servlet controller -->
  48. <dependency>
  49. <groupId>taglibs</groupId>
  50. <artifactId>standard</artifactId>
  51. <version>1.1.2</version>
  52. </dependency>
  53. <dependency>
  54. <groupId>jstl</groupId>
  55. <artifactId>jstl</artifactId>
  56. <version>1.2</version>
  57. </dependency>
  58. <dependency>
  59. <groupId>com.fasterxml.jackson.core</groupId>
  60. <artifactId>jackson-annotations</artifactId>
  61. <version>2.9.8</version>
  62. </dependency>
  63. <dependency>
  64. <groupId>com.fasterxml.jackson.core</groupId>
  65. <artifactId>jackson-core</artifactId>
  66. <version>2.9.8</version>
  67. </dependency>
  68. <dependency>
  69. <groupId>com.fasterxml.jackson.core</groupId>
  70. <artifactId>jackson-databind</artifactId>
  71. <version>2.9.8</version>
  72. </dependency>
  73. <dependency>
  74. <groupId>javax.servlet</groupId>
  75. <artifactId>javax.servlet-api</artifactId>
  76. <version>3.1.0</version>
  77. </dependency>
  78. <!-- 4.Spring -->
  79. <!-- 1)Spring核心 -->
  80. <dependency>
  81. <groupId>org.springframework</groupId>
  82. <artifactId>spring-core</artifactId>
  83. <version>5.1.6.RELEASE</version>
  84. </dependency>
  85. <dependency>
  86. <groupId>org.springframework</groupId>
  87. <artifactId>spring-beans</artifactId>
  88. <version>5.1.6.RELEASE</version>
  89. </dependency>
  90. <dependency>
  91. <groupId>org.springframework</groupId>
  92. <artifactId>spring-context</artifactId>
  93. <version>5.1.6.RELEASE</version>
  94. </dependency>
  95. <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
  96. <dependency>
  97. <groupId>org.aspectj</groupId>
  98. <artifactId>aspectjweaver</artifactId>
  99. <version>1.9.2</version>
  100. </dependency>
  101. <!-- 2)Spring DAO层 -->
  102. <dependency>
  103. <groupId>org.springframework</groupId>
  104. <artifactId>spring-jdbc</artifactId>
  105. <version>5.1.6.RELEASE</version>
  106. </dependency>
  107. <dependency>
  108. <groupId>org.springframework</groupId>
  109. <artifactId>spring-tx</artifactId>
  110. <version>5.1.6.RELEASE</version>
  111. </dependency>
  112. <!-- 3)Spring controller -->
  113. <dependency>
  114. <groupId>org.springframework</groupId>
  115. <artifactId>spring-web</artifactId>
  116. <version>5.1.6.RELEASE</version>
  117. </dependency>
  118. <dependency>
  119. <groupId>org.springframework</groupId>
  120. <artifactId>spring-webmvc</artifactId>
  121. <version>5.1.6.RELEASE</version>
  122. </dependency>
  123. <!-- 4)Spring test -->
  124. <dependency>
  125. <groupId>org.springframework</groupId>
  126. <artifactId>spring-test</artifactId>
  127. <version>5.1.6.RELEASE</version>
  128. </dependency>
  129. <!-- redis客户端:Jedis -->
  130. <dependency>
  131. <groupId>redis.clients</groupId>
  132. <artifactId>jedis</artifactId>
  133. <version>2.7.3</version>
  134. </dependency>
  135. <dependency>
  136. <groupId>com.dyuproject.protostuff</groupId>
  137. <artifactId>protostuff-core</artifactId>
  138. <version>1.0.8</version>
  139. </dependency>
  140. <dependency>
  141. <groupId>com.dyuproject.protostuff</groupId>
  142. <artifactId>protostuff-runtime</artifactId>
  143. <version>1.0.8</version>
  144. </dependency>
  145. <!-- Map工具类 -->
  146. <dependency>
  147. <groupId>commons-collections</groupId>
  148. <artifactId>commons-collections</artifactId>
  149. <version>3.2</version>
  150. </dependency>
  151. </dependencies>
  152. <build>
  153. <finalName>ssm</finalName>
  154. </build>
  155. </project>

6.配置Spring-*.xml

jdbc.properties
这里有个地方要注意,命名不能直接使用username什么的,要用jdbc.username,直接使用username可能会与系统变量产生冲突。

  1. jdbc.driver=com.mysql.cj.jdbc.Driver
  2. jdbc.url=jdbc:mysql://127.0.0.1:3306/XXX?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true
  3. jdbc.username=root
  4. jdbc.password={your password}

spring-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. xmlns:mvc="http://mybatis.org/schema/mybatis-spring" xmlns:aop="http://www.springframework.org/schema/aop"
  6. 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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
  7. <!-- 配置整合mybatis过程 -->
  8. <!-- 1、注册外部的数据库相关参数设置文件 -->
  9. <context:property-placeholder location="classpath:jdbc.properties"/>
  10. <!-- 2、配置数据库连接池 -->
  11. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  12. <!-- 配置数据库连接属性 -->
  13. <property name="driverClass" value="${jdbc.driver}"/>
  14. <property name="jdbcUrl" value="${jdbc.url}"/>
  15. <property name="user" value="${jdbc.username}"/>
  16. <property name="password" value="${jdbc.password}"/>
  17. <!-- 配置C3P0连接池的私有属性 -->
  18. <property name="maxPoolSize" value="30"/>
  19. <property name="minPoolSize" value="10"/>
  20. <property name="initialPoolSize" value="10"/>
  21. <!-- 关闭连接池后不自动提交 -->
  22. <property name="autoCommitOnClose" value="false"/>
  23. <!-- 设置连接超时时间 -->
  24. <property name="checkoutTimeout" value="10000"/>
  25. <!-- 设置连接超时重试次数-->
  26. <property name="acquireRetryAttempts" value="2"/>
  27. </bean>
  28. <!-- 3、配置SqlSessionFactory对象 -->
  29. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  30. <!-- 配置数据源(数据库连接池) -->
  31. <property name="dataSource" ref="dataSource"/>
  32. <!-- 配置所有 mapper 文件的地址 -->
  33. <property name="mapperLocations" value="classpath:mapper/*Mapper.xml"/>
  34. <!-- 设置要起别名(类小写)的包 -->
  35. <property name="typeAliasesPackage" value="com.lnquan.entity"/>
  36. <!-- 配置 mybatis 全局配置文件的路径-->
  37. <property name="configLocation" value="classpath:mybatis-conf.xml"/>
  38. </bean>
  39. <!-- !!! 一定要配置这个属性,表示使用CGLib进行代理。不然无法使用基于类的代理-->
  40. <aop:config proxy-target-class="true"/>
  41. <!-- 4、配置扫描 dao 接口的包,扫描所有的mapper接口的实现,使他们能够程序加载时自动注入到spring容器中-->
  42. <mvc:scan base-package="com.lnquan.dao"/>
  43. </beans>

spring-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. xmlns:mvc="http://mybatis.org/schema/mybatis-spring" xmlns:tx="http://www.springframework.org/schema/tx"
  6. 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://mybatis.org/schema/mybatis-spring http://mybatis.org/schema/mybatis-spring.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
  7. <!-- 扫描service包下所有使用@Service注解的类型-->
  8. <context:component-scan base-package="com.lnquan.service"/>
  9. <!-- 配置事务管理器 -->
  10. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  11. <property name="dataSource" ref="dataSource"/>
  12. </bean>
  13. <!-- 开启基于注解的声明式事务 -->
  14. <tx:annotation-driven transaction-manager="transactionManager"/>
  15. </beans>

spring-web.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" xmlns:mvc="http://www.springframework.org/schema/mvc"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
  6. <!--
  7. 1、开启 annotation-driven(SpringMVC注解模式)
  8. 自动注册
  9. DefaultAnnotationHandlerMapping,AnnotationMethodHandlerAdapter等
  10. -->
  11. <!-- 2、使SpringMVC支持静态资源加载 -->
  12. <mvc:default-servlet-handler/>
  13. <mvc:annotation-driven/>
  14. <!-- 3、配置视图解析器 -->
  15. <bean id="internalResourceViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  16. <property name="prefix" value="/WEB-INF/jsp/"/>
  17. <property name="suffix" value=".jsp"/>
  18. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
  19. </bean>
  20. <!-- 4、扫描web(就是扫描所有的controller)相关的bean -->
  21. <context:component-scan base-package="com.lnquan.controller"/>
  22. <context:component-scan base-package="com.lnquan.dao"/>
  23. <context:component-scan base-package="com.lnquan.service.impl"/>
  24. </beans>

7.数据库中创建相应的表

book表、appointment表
在这里插入图片描述
在这里插入图片描述
数据表中各字段类型:
在这里插入图片描述

8.创建entity类

  1. entity包下添加以下两个类(自行添加有参、无参构造器,getset方法,toString方法。这里就省略了)

Book:

  1. public class Book {
  2. private int bookId;
  3. private String name;
  4. private int number;
  5. }

Appointment:

  1. public class Appointment {
  2. private int bookId;
  3. private int studentId;
  4. private Date appointTime;
  5. private Book book;
  6. }

9.创建Dao接口(数据交互)

主要是配置对book类和appointment类的一些操作。Dao层的实现主要是在mybatis中实现,但是需要我们在mapper配置文件中写相关的sql语句。

bookDao:

  1. @Component
  2. public interface BookDao {
  3. //通过数的Id进行查询,返回Book
  4. Book queryById(int id);
  5. //查询所有的书
  6. List<Book> queryAll();
  7. //减少指定Id的书的库存,一次减少一本
  8. int reduceNumber(int bookId);
  9. }

appointmentDao:

  1. @Component
  2. public interface AppointmentDao {
  3. //插入预约,返回预约号
  4. int insertAppointment(@Param("bookId") int bookId,@Param("stuId") int stuId);
  5. //根据主键查询学生的预约情况,携带预约的书籍
  6. Appointment queryByKeyWithBook(@Param("bookId") int bookId,@Param("stuId") int stuId);
  7. }

10.在resources包的mapper子包下创建 AppointmentMapper.xmlBookMapper.xml

进行如下配置。

BookMapper.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.lnquan.dao.BookDao">
  4. <!--
  5. Book queryById(int id);
  6. List<Book> queryAll();
  7. int reduceNumber(int bookId);
  8. -->
  9. <select id="queryById" resultType="book">
  10. SELECT * FROM book WHERE book_id=#{id}
  11. </select>
  12. <select id="queryAll" resultType="book">
  13. SELECT * FROM book ORDER BY book_id
  14. </select>
  15. <update id="reduceNumber">
  16. UPDATE book SET number=number-1 WHERE book_id=#{bookId} AND number>0
  17. </update>
  18. </mapper>

AppointmentMapper.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.lnquan.dao.AppointmentDao">
  4. <!--
  5. //插入预约,返回预约号
  6. int insertAppointment(@Param("bookId") int bookId,@Param("stuId") int stuId);
  7. //根据主键查询学生的预约情况,携带预约的书籍
  8. Appointment queryByKeyWithBook(@Param("bookId") int bookId,@Param("stuId") int stuId);
  9. -->
  10. <insert id="insertAppointment" useGeneratedKeys="true" keyColumn="book_id">
  11. INSERT INTO appointment(book_id, student_id) VALUES (#{bookId},#{stuId})
  12. </insert>
  13. <resultMap id="apo" type="appointment">
  14. <id property="studentId" column="stuID"/>
  15. <result property="bookId" column="bookId"/>
  16. <result property="appointTime" column="time"/>
  17. <!--
  18. 这里因为在appointment类中携带book,数据库返回结果时需要使用联合标签(association)对book进行封装
  19. -->
  20. <association property="book" javaType="book">
  21. <id property="bookId" column="bookId"/>
  22. <result property="name" column="name"/>
  23. <result property="number" column="number"/>
  24. </association>
  25. </resultMap>
  26. <!--使用自定义的resultMap-->
  27. <select id="queryByKeyWithBook" resultMap="apo">
  28. SELECT a.student_id stuId,a.appoint_time time,a.book_id bookId,b.name name,b.number number
  29. FROM appointment a JOIN book b ON a.book_id=b.book_id
  30. WHERE a.student_id=#{stuId} AND a.book_id=#{bookId}
  31. </select>
  32. </mapper>

11.Service层相关。

  1. service包下创建BookService接口和AppointmentService接口。

BookService:

  1. public interface BookService {
  2. Book selectOneById(int bookId);
  3. List<Book> selectAll();
  4. int reduceNumber(int bookId);
  5. }

AppointmentService:

  1. public interface AppointmentService {
  2. int addAppointment(int bookId,int stuId);
  3. Appointment selectAppointment(int bookId,int stuId);
  4. }

再在service包下创建一个impl子包,用来放Serviece接口的相关实现类。impl包下创建AppointmentServiceImpl 和 BookServiceImpl。

AppointmentServiceImpl :

  1. @Service("AppointmentServiceImpl")
  2. public class AppointmentServiceImpl implements AppointmentService {
  3. @Autowired
  4. private AppointmentDao appointmentDao;
  5. public int addAppointment(int bookId, int stuId) {
  6. return appointmentDao.insertAppointment(bookId,stuId);
  7. }
  8. public Appointment selectAppointment(int bookId, int stuId) {
  9. return appointmentDao.queryByKeyWithBook(bookId,stuId);
  10. }
  11. }

BookServiceImpl:

  1. @Service("BookServiceImpl")
  2. public class BookServiceImpl implements BookService {
  3. @Autowired
  4. private BookDao bookDao;
  5. public Book selectOneById(int bookId) {
  6. return bookDao.queryById(bookId);
  7. }
  8. public List<Book> selectAll() {
  9. return bookDao.queryAll();
  10. }
  11. public int reduceNumber(int bookId) {
  12. return bookDao.reduceNumber(bookId);
  13. }
  14. }

12.Controller层相关实现

controller包下创建AppointmentControllerBookController两个控制类。

BookController:

  1. @Controller
  2. public class BookController {
  3. @Autowired
  4. @Qualifier("BookServiceImpl")
  5. private BookServiceImpl bookService;
  6. @Autowired
  7. @Qualifier("AppointmentServiceImpl")
  8. private AppointmentServiceImpl appointmentServie;
  9. @RequestMapping("/selectOne")
  10. public String selectOne(@RequestParam("bookId") int bookId, Map<String , List<Book>> map){
  11. Book book = bookService.selectOneById(bookId);
  12. List<Book> books = new ArrayList<Book>();
  13. books.add(book);
  14. map.put("books",books);
  15. return "list";
  16. }
  17. @RequestMapping("/selectAll")
  18. public String selectAll(Map<String ,List<Book>> map){
  19. List<Book> books = bookService.selectAll();
  20. map.put("books",books);
  21. return "list";
  22. }
  23. }

AppointmentController:

  1. @Controller
  2. public class AppointmentController {
  3. @Autowired
  4. @Qualifier("BookServiceImpl")
  5. private BookServiceImpl bookService;
  6. @Autowired
  7. @Qualifier("AppointmentServiceImpl")
  8. private AppointmentServiceImpl appointmentService;
  9. @ResponseBody
  10. @RequestMapping("addAppoint")
  11. public Book addAppoint(@RequestParam("bookId") int bookId,
  12. @RequestParam("stuId") int stuId){
  13. bookService.reduceNumber(bookId);
  14. appointmentService.addAppointment(bookId,stuId);
  15. return bookService.selectOneById(bookId);
  16. }
  17. }

到这里基本后台的就写完了,可以自己试着写以下前端相关的。

发表评论

表情:
评论列表 (有 0 条评论,410人围观)

还没有评论,来说两句吧...

相关阅读

    相关 SSM框架整合

    1.创建maven项目,准备实体表 idea创建一个maven项目,选用骨架webapp。创建后边验证要用的数据库和实体表。 create database s

    相关 SSM框架整合

    最近接触了不少的项目,从SSM到SSH都接触到了,然而对于这几个项目的架构还是一知半解,我自己花费了一些时间,整理下,写了一个SSM的框架的demo,后期我会给出SSH的一个d

    相关 SSM框架整合

    1、Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头。文件必须存在。 applicationContext-d

    相关 ssm框架整合

    整合思路 1、Dao层: Mybatis的配置文件:SqlMapConfig.xml 不需要配置任何内容,需要有文件头。文件必须存在。 application