首页 > 编程语言 > IDEA SSM框架整合配置及步骤详解
2021
03-24

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

参考

狂神说SpringMVC05:整合SSM框架

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

前言

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

SSM框架整合步骤

1. 创建数据库

在这里插入图片描述

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

在这里插入图片描述

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

4. maven 资源过滤

<?xml version="1.0" encoding="UTF-8"?>
<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">
 <modelVersion>4.0.0</modelVersion>
 <build>
  <resources>
   <resource>
    <directory>src/main/java</directory>
    <includes>
     <include>**/*.properties</include>
     <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
   </resource>
   <resource>
    <directory>src/main/resources</directory>
    <includes>
     <include>**/*.properties</include>
     <include>**/*.xml</include>
    </includes>
    <filtering>false</filtering>
   </resource>
  </resources>
 </build>

 <groupId>org.example</groupId>
 <artifactId>ssm</artifactId>
 <version>1.0-SNAPSHOT</version>
 <properties>
  <maven.compiler.source>13</maven.compiler.source>
  <maven.compiler.target>13</maven.compiler.target>
 </properties>
 <dependencies>
  <!--Junit-->
  <dependency>
   <groupId>junit</groupId>
   <artifactId>junit</artifactId>
   <version>4.12</version>
  </dependency>
  <!--数据库驱动-->
  <dependency>
   <groupId>mysql</groupId>
   <artifactId>mysql-connector-java</artifactId>
   <version>5.1.47</version>
  </dependency>
  <!-- 数据库连接池 -->
  <dependency>
   <groupId>com.mchange</groupId>
   <artifactId>c3p0</artifactId>
   <version>0.9.5.2</version>
  </dependency>

  <!--Servlet - JSP -->
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>servlet-api</artifactId>
   <version>2.5</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet.jsp</groupId>
   <artifactId>jsp-api</artifactId>
   <version>2.2</version>
  </dependency>
  <dependency>
   <groupId>javax.servlet</groupId>
   <artifactId>jstl</artifactId>
   <version>1.2</version>
  </dependency>

  <!--Mybatis-->
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis</artifactId>
   <version>3.5.2</version>
  </dependency>
  <dependency>
   <groupId>org.mybatis</groupId>
   <artifactId>mybatis-spring</artifactId>
   <version>2.0.6</version>
  </dependency>

  <!--Spring-->
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-webmvc</artifactId>
   <version>5.3.4</version>
  </dependency>
  <dependency>
   <groupId>org.springframework</groupId>
   <artifactId>spring-jdbc</artifactId>
   <version>5.3.4</version>
  </dependency>
 </dependencies>

</project>

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

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ZMaaC1AV-1616322330715)(C:\Users\83599\AppData\Roaming\Typora\typora-user-images\image-20210320184623106.png)]

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
  http://www.springframework.org/schema/beans/spring-beans.xsd">

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

</configuration>

Mybatis

6. 配置数据库属性

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

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

package com.projectname.pojo;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Book {
 private int bookID;
 private String bookName;
 private int bookCounts;
 private String detail;

}
package com.projectname.dao;

import com.projectname.pojo.Book;

import java.util.List;

public interface DaoBook {
 //增加一个Book
 int addBook(Book book);

 //根据id删除一个Book
 int deleteBookById(int id);

 //更新Book
 int updateBook(Book books);

 //根据id查询,返回一个Book
 Book queryBookById(int id);

 //查询全部Book,返回list集合
 List<Book> queryAllBook();
}
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="com.kuang.dao.BookMapper">

 <!--增加一个Book-->
 <insert id="addBook" parameterType="Books">
  insert into ssmbuild.books(bookName,bookCounts,detail)
  values (#{bookName}, #{bookCounts}, #{detail})
 </insert>

 <!--根据id删除一个Book-->
 <delete id="deleteBookById" parameterType="int">
  delete from ssmbuild.books where bookID=#{bookID}
 </delete>

 <!--更新Book-->
 <update id="updateBook" parameterType="Books">
  update ssmbuild.books
  set bookName = #{bookName},bookCounts = #{bookCounts},detail = #{detail}
  where bookID = #{bookID}
 </update>

 <!--根据id查询,返回一个Book-->
 <select id="queryBookById" resultType="Books">
  select * from ssmbuild.books
  where bookID = #{bookID}
 </select>

 <!--查询全部Book-->
 <select id="queryAllBook" resultType="Books">
  SELECT * from ssmbuild.books
 </select>

</mapper>
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
  PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<mappers>
<mapper resource="com/projectname/dao/BookMapper.xml"/>
</mappers>
</configuration>

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

package com.projectname.service;

import com.projectname.pojo.Book;

import java.util.List;

public interface BookService {
 //增加一个Book
 int addBook(Book book);
 //根据id删除一个Book
 int deleteBookById(int id);
 //更新Book
 int updateBook(Book books);
 //根据id查询,返回一个Book
 Book queryBookById(int id);
 //查询全部Book,返回list集合
 List<Book> queryAllBook();
}
package com.projectname.service;

import com.projectname.dao.BookMapper;
import com.projectname.pojo.Book;

import java.util.List;

public class BookServiceImpl implements BookService {

 //调用dao层的操作,设置一个set接口,方便Spring管理
 private BookMapper bookMapper;

 public void setBookMapper(BookMapper bookMapper) {
  this.bookMapper = bookMapper;
 }

 public int addBook(Book book) {
  return bookMapper.addBook(book);
 }

 public int deleteBookById(int id) {
  return bookMapper.deleteBookById(id);
 }

 public int updateBook(Book books) {
  return bookMapper.updateBook(books);
 }

 public Book queryBookById(int id) {
  return bookMapper.queryBookById(id);
 }

 public List<Book> queryAllBook() {
  return bookMapper.queryAllBook();
 }
}

Spring

9. 使用Spring编写Mybatis配置文件:spring-dao.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"
  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">
 <context:property-placeholder location="classpath:database.properties"/>
 <!-- 2.数据库连接池 -->
 <!--数据库连接池
  dbcp 半自动化操作 不能自动连接
  c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面)
 -->
 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  <!-- 配置连接池属性 -->
  <property name="driverClass" value="${jdbc.driver}"/>
  <property name="jdbcUrl" value="${jdbc.url}"/>
  <property name="user" value="${jdbc.username}"/>
  <property name="password" value="${jdbc.password}"/>
  <!-- c3p0连接池的私有属性 -->
  <property name="maxPoolSize" value="30"/>
  <property name="minPoolSize" value="10"/>
  <!-- 关闭连接后不自动commit -->
  <property name="autoCommitOnClose" value="false"/>
  <!-- 获取连接超时时间 -->
  <property name="checkoutTimeout" value="10000"/>
  <!-- 当获取连接失败重试次数 -->
  <property name="acquireRetryAttempts" value="2"/>
 </bean>
 <!-- 3.配置SqlSessionFactory对象 -->
 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 注入数据库连接池 -->
  <property name="dataSource" ref="dataSource"/>
  <!-- 配置MyBaties全局配置文件:mybatis-config.xml -->
  <property name="configLocation" value="classpath:mybatis-configuration.xml"/>
 </bean>
 <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 -->
 <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <!-- 注入sqlSessionFactory -->
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
  <!-- 给出需要扫描Dao接口包 -->
  <property name="basePackage" value="com.projectname.dao"/>
 </bean>
</beans>

10. Spring 整合service层

spring-service

<?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"
  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">

 <!-- 扫描service相关的bean -->
 <context:component-scan base-package="com.projectname.service" />

 <!--BookServiceImpl注入到IOC容器中-->
 <bean id="BookServiceImpl" class="com.projectname.service.BookServiceImpl">
  <property name="bookMapper" ref="bookMapper"/>
 </bean>

 <!-- 配置事务管理器 -->
 <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  <!-- 注入数据库连接池 -->
  <property name="dataSource" ref="dataSource" />
 </bean>

</beans>

Spring MVC层

11. configuate the web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
   version="4.0"> <!-- 配置SpringMVC -->
 <!-- 1.开启SpringMVC注解驱动 -->
 <servlet>
  <servlet-name>DispatcherServlet</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!-->
   <param-value>classpath:applicationContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
 </servlet>
 <servlet-mapping>
  <servlet-name>DispatcherServlet</servlet-name>
  <url-pattern>/</url-pattern>
 </servlet-mapping>

 <!--encodingFilter-->
 <filter>
  <filter-name>encodingFilter</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>encodingFilter</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>

 <!--Session过期时间-->
 <session-config>
  <session-timeout>15</session-timeout>
 </session-config>
</web-app>

12. spring-mvc.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:mvc="http://www.springframework.org/schema/mvc"
  xmlns:context="http://www.springframework.org/schema/context"
  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">
 <!-- 配置SpringMVC -->
 <!-- 1.开启SpringMVC注解驱动 -->
 <mvc:annotation-driven />
 <!-- 2.静态资源默认servlet配置-->
 <mvc:default-servlet-handler/>

 <!-- 3.配置jsp 显示ViewResolver视图解析器 -->
 <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  <property name="prefix" value="/WEB-INF/jsp/" />
  <property name="suffix" value=".jsp" />
 </bean>
 <!-- 4.扫描web相关的bean -->
 <context:component-scan base-package="com.projectname.controller" />
</beans>

13. configuate 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:mvc="http://www.springframework.org/schema/mvc"
 xmlns:context="http://www.springframework.org/schema/context"
 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">

 <import resource="spring-dao.xml"/>
 <import resource="spring-service.xml"/>
 <import resource="spring-mvc.xml"/>

</beans>

一些杂七杂八的配置

14. 添加日志功能

<settings>
 <setting name="logImpl" value="STDOUT_LOGGING"/>
</settings>

15. add lib

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

new directory lib

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

编程技巧