IDEA SSM框架整合配置及步骤详解

时间:2021-11-05 22:59:37

参考

狂神说SpringMVC05:整合SSM框架

https://mp.weixin.qq.com/s?__biz=Mzg2NTAzMTExNg==&mid=2247484004&idx=1&sn=cef9d881d0a8d7db7e8ddc6a380a9a76&scene=19#wechat_redirect

前言

根据自己的环境参考狂神的视频进行了SSM框架整合,用于备忘

SSM框架整合步骤

1. 创建数据库

IDEA SSM框架整合配置及步骤详解

2. IDEA创建maven项目.在pom.xml中设设置java conpiler版本(jdk13)

IDEA SSM框架整合配置及步骤详解

3. 导入依赖 jnuit 数据库驱动 数据库连接 Severlet JSp

4. maven 资源过滤

  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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  5. <modelVersion>4.0.0</modelVersion>
  6. <build>
  7. <resources>
  8. <resource>
  9. <directory>src/main/java</directory>
  10. <includes>
  11. <include>**/*.properties</include>
  12. <include>**/*.xml</include>
  13. </includes>
  14. <filtering>false</filtering>
  15. </resource>
  16. <resource>
  17. <directory>src/main/resources</directory>
  18. <includes>
  19. <include>**/*.properties</include>
  20. <include>**/*.xml</include>
  21. </includes>
  22. <filtering>false</filtering>
  23. </resource>
  24. </resources>
  25. </build>
  26.  
  27. <groupId>org.example</groupId>
  28. <artifactId>ssm</artifactId>
  29. <version>1.0-SNAPSHOT</version>
  30. <properties>
  31. <maven.compiler.source>13</maven.compiler.source>
  32. <maven.compiler.target>13</maven.compiler.target>
  33. </properties>
  34. <dependencies>
  35. <!--Junit-->
  36. <dependency>
  37. <groupId>junit</groupId>
  38. <artifactId>junit</artifactId>
  39. <version>4.12</version>
  40. </dependency>
  41. <!--数据库驱动-->
  42. <dependency>
  43. <groupId>mysql</groupId>
  44. <artifactId>mysql-connector-java</artifactId>
  45. <version>5.1.47</version>
  46. </dependency>
  47. <!-- 数据库连接池 -->
  48. <dependency>
  49. <groupId>com.mchange</groupId>
  50. <artifactId>c3p0</artifactId>
  51. <version>0.9.5.2</version>
  52. </dependency>
  53.  
  54. <!--Servlet - JSP -->
  55. <dependency>
  56. <groupId>javax.servlet</groupId>
  57. <artifactId>servlet-api</artifactId>
  58. <version>2.5</version>
  59. </dependency>
  60. <dependency>
  61. <groupId>javax.servlet.jsp</groupId>
  62. <artifactId>jsp-api</artifactId>
  63. <version>2.2</version>
  64. </dependency>
  65. <dependency>
  66. <groupId>javax.servlet</groupId>
  67. <artifactId>jstl</artifactId>
  68. <version>1.2</version>
  69. </dependency>
  70.  
  71. <!--Mybatis-->
  72. <dependency>
  73. <groupId>org.mybatis</groupId>
  74. <artifactId>mybatis</artifactId>
  75. <version>3.5.2</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.mybatis</groupId>
  79. <artifactId>mybatis-spring</artifactId>
  80. <version>2.0.6</version>
  81. </dependency>
  82.  
  83. <!--Spring-->
  84. <dependency>
  85. <groupId>org.springframework</groupId>
  86. <artifactId>spring-webmvc</artifactId>
  87. <version>5.3.4</version>
  88. </dependency>
  89. <dependency>
  90. <groupId>org.springframework</groupId>
  91. <artifactId>spring-jdbc</artifactId>
  92. <version>5.3.4</version>
  93. </dependency>
  94. </dependencies>
  95.  
  96. </project>

5. 创建两个项目的目录结构以及Spring 和 Mybatis的配置文件

IDEA SSM框架整合配置及步骤详解

  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. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans.xsd">
  6.  
  7. </beans>
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6.  
  7. </configuration>

Mybatis

6. 配置数据库属性

  1. jdbc.driver=com.mysql.jdbc.Driver
  2. jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
  3. jdbc.username=root
  4. jdbc.password=root

7. 创建Book实体类(使用lombok插件),编写Dao层mapper接口,在mybatis配置中注册mapper

  1. package com.projectname.pojo;
  2.  
  3. import lombok.AllArgsConstructor;
  4. import lombok.Data;
  5. import lombok.NoArgsConstructor;
  6.  
  7. @Data
  8. @AllArgsConstructor
  9. @NoArgsConstructor
  10. public class Book {
  11. private int bookID;
  12. private String bookName;
  13. private int bookCounts;
  14. private String detail;
  15.  
  16. }
  1. package com.projectname.dao;
  2.  
  3. import com.projectname.pojo.Book;
  4.  
  5. import java.util.List;
  6.  
  7. public interface DaoBook {
  8. //增加一个Book
  9. int addBook(Book book);
  10.  
  11. //根据id删除一个Book
  12. int deleteBookById(int id);
  13.  
  14. //更新Book
  15. int updateBook(Book books);
  16.  
  17. //根据id查询,返回一个Book
  18. Book queryBookById(int id);
  19.  
  20. //查询全部Book,返回list集合
  21. List<Book> queryAllBook();
  22. }
  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.  
  6. <mapper namespace="com.kuang.dao.BookMapper">
  7.  
  8. <!--增加一个Book-->
  9. <insert id="addBook" parameterType="Books">
  10. insert into ssmbuild.books(bookName,bookCounts,detail)
  11. values (#{bookName}, #{bookCounts}, #{detail})
  12. </insert>
  13.  
  14. <!--根据id删除一个Book-->
  15. <delete id="deleteBookById" parameterType="int">
  16. delete from ssmbuild.books where bookID=#{bookID}
  17. </delete>
  18.  
  19. <!--更新Book-->
  20. <update id="updateBook" parameterType="Books">
  21. update ssmbuild.books
  22. set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
  23. where bookID = #{bookID}
  24. </update>
  25.  
  26. <!--根据id查询,返回一个Book-->
  27. <select id="queryBookById" resultType="Books">
  28. select * from ssmbuild.books
  29. where bookID = #{bookID}
  30. </select>
  31.  
  32. <!--查询全部Book-->
  33. <select id="queryAllBook" resultType="Books">
  34. SELECT * from ssmbuild.books
  35. </select>
  36.  
  37. </mapper>
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <!DOCTYPE configuration
  3. PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  4. "http://mybatis.org/dtd/mybatis-3-config.dtd">
  5. <configuration>
  6. <mappers>
  7. <mapper resource="com/projectname/dao/BookMapper.xml"/>
  8. </mappers>
  9. </configuration>

8. 编写service层的接口和实现类代码

  1. package com.projectname.service;
  2.  
  3. import com.projectname.pojo.Book;
  4.  
  5. import java.util.List;
  6.  
  7. public interface BookService {
  8. //增加一个Book
  9. int addBook(Book book);
  10. //根据id删除一个Book
  11. int deleteBookById(int id);
  12. //更新Book
  13. int updateBook(Book books);
  14. //根据id查询,返回一个Book
  15. Book queryBookById(int id);
  16. //查询全部Book,返回list集合
  17. List<Book> queryAllBook();
  18. }
  1. package com.projectname.service;
  2.  
  3. import com.projectname.dao.BookMapper;
  4. import com.projectname.pojo.Book;
  5.  
  6. import java.util.List;
  7.  
  8. public class BookServiceImpl implements BookService {
  9.  
  10. //调用dao层的操作,设置一个set接口,方便Spring管理
  11. private BookMapper bookMapper;
  12.  
  13. public void setBookMapper(BookMapper bookMapper) {
  14. this.bookMapper = bookMapper;
  15. }
  16.  
  17. public int addBook(Book book) {
  18. return bookMapper.addBook(book);
  19. }
  20.  
  21. public int deleteBookById(int id) {
  22. return bookMapper.deleteBookById(id);
  23. }
  24.  
  25. public int updateBook(Book books) {
  26. return bookMapper.updateBook(books);
  27. }
  28.  
  29. public Book queryBookById(int id) {
  30. return bookMapper.queryBookById(id);
  31. }
  32.  
  33. public List<Book> queryAllBook() {
  34. return bookMapper.queryAllBook();
  35. }
  36. }

Spring

9. 使用Spring编写Mybatis配置文件: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. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6. <context:property-placeholder location="classpath:database.properties"/>
  7. <!-- 2.数据库连接池 -->
  8. <!--数据库连接池
  9. dbcp 半自动化操作 不能自动连接
  10. c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
  11. -->
  12. <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  13. <!-- 配置连接池属性 -->
  14. <property name="driverClass" value="${jdbc.driver}"/>
  15. <property name="jdbcUrl" value="${jdbc.url}"/>
  16. <property name="user" value="${jdbc.username}"/>
  17. <property name="password" value="${jdbc.password}"/>
  18. <!-- c3p0连接池的私有属性 -->
  19. <property name="maxPoolSize" value="30"/>
  20. <property name="minPoolSize" value="10"/>
  21. <!-- 关闭连接后不自动commit -->
  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. <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
  33. <property name="configLocation" value="classpath:mybatis-configuration.xml"/>
  34. </bean>
  35. <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
  36. <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
  37. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  38. <!-- 注入sqlSessionFactory -->
  39. <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  40. <!-- 给出需要扫描Dao接口包 -->
  41. <property name="basePackage" value="com.projectname.dao"/>
  42. </bean>
  43. </beans>

10. Spring 整合service层

spring-service

  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="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6.  
  7. <!-- 扫描service相关的bean -->
  8. <context:component-scan base-package="com.projectname.service" />
  9.  
  10. <!--BookServiceImpl注入到IOC容器中-->
  11. <bean id="BookServiceImpl" class="com.projectname.service.BookServiceImpl">
  12. <property name="bookMapper" ref="bookMapper"/>
  13. </bean>
  14.  
  15. <!-- 配置事务管理器 -->
  16. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  17. <!-- 注入数据库连接池 -->
  18. <property name="dataSource" ref="dataSource" />
  19. </bean>
  20.  
  21. </beans>

Spring MVC层

11. configuate the 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"> <!-- 配置SpringMVC -->
  6. <!-- 1.开启SpringMVC注解驱动 -->
  7. <servlet>
  8. <servlet-name>DispatcherServlet</servlet-name>
  9. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  10. <init-param>
  11. <param-name>contextConfigLocation</param-name>
  12. <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
  13. <param-value>classpath:applicationContext.xml</param-value>
  14. </init-param>
  15. <load-on-startup>1</load-on-startup>
  16. </servlet>
  17. <servlet-mapping>
  18. <servlet-name>DispatcherServlet</servlet-name>
  19. <url-pattern>/</url-pattern>
  20. </servlet-mapping>
  21.  
  22. <!--encodingFilter-->
  23. <filter>
  24. <filter-name>encodingFilter</filter-name>
  25. <filter-class>
  26. org.springframework.web.filter.CharacterEncodingFilter
  27. </filter-class>
  28. <init-param>
  29. <param-name>encoding</param-name>
  30. <param-value>utf-8</param-value>
  31. </init-param>
  32. </filter>
  33. <filter-mapping>
  34. <filter-name>encodingFilter</filter-name>
  35. <url-pattern>/*</url-pattern>
  36. </filter-mapping>
  37.  
  38. <!--Session过期时间-->
  39. <session-config>
  40. <session-timeout>15</session-timeout>
  41. </session-config>
  42. </web-app>

12. spring-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" 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 https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  6. <!-- 配置SpringMVC -->
  7. <!-- 1.开启SpringMVC注解驱动 -->
  8. <mvc:annotation-driven />
  9. <!-- 2.静态资源默认servlet配置-->
  10. <mvc:default-servlet-handler/>
  11.  
  12. <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
  13. <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  14. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  15. <property name="prefix" value="/WEB-INF/jsp/" />
  16. <property name="suffix" value=".jsp" />
  17. </bean>
  18. <!-- 4.扫描web相关的bean -->
  19. <context:component-scan base-package="com.projectname.controller" />
  20. </beans>

13. configuate 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" 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
  6. http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
  7.  
  8. <import resource="spring-dao.xml"/>
  9. <import resource="spring-service.xml"/>
  10. <import resource="spring-mvc.xml"/>
  11.  
  12. </beans>

一些杂七杂八的配置

14. 添加日志功能

  1. <settings>
  2. <setting name="logImpl" value="STDOUT_LOGGING"/>
  3. </settings>

15. add lib

File -> Project Structure->Artifacts
Type -> Bundle

new directory lib

IDEA SSM框架整合配置及步骤详解

到此这篇关于IDEA SSM框架整合配置及步骤详解的文章就介绍到这了,更多相关IDEA SSM框架整合配置内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/rglkt/article/details/115053321