ssm框架整合

it2025-11-08  17

根据b站 狂神说java整理: https://www.bilibili.com/video/BV1aE41167Tu?p=17

文章目录

7、ssm整合7.1、基本环境7.2、编写代码之mybatis层7.3、编写代码之spring层7.4、编写代码之springmvc层7.5、将配置文件加入applicationContext.xml文件中整合在一起7.6、简单总结

7、ssm整合

7.1、基本环境

1、创建数据库ssm和创建book表

create database ssm; use ssm; create table books( bookID int(10) not null auto_increment comment '书id', bookName varchar(100) not null comment '书名', bookCounts int(11) not null comment '数量', detail varchar(200) not null comment '描述', key bookID (bookID) ) engine=innodb default charset=utf8 insert into books(bookID,bookName,bookCounts,detail)values (1,'Java',1,'java入门'), (2,'MySQL',10,'mysql入门'), (3,'Linux',5,'linux入门');

2、创建maven项目,导入pom依赖

主要是一些需要的依赖和静态资源导出问题

<!--依赖:junit,数据库驱动,连接池,servlet,jsp,mybatis,mybatis-spring,spring--> <dependencies> <!--junit--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13</version> <scope>test</scope> </dependency> <!--数据库驱动--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.19</version> </dependency> <!--数据库连接池,c3p0--> <dependency> <groupId>com.mchange</groupId> <artifactId>c3p0</artifactId> <version>0.9.5.5</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.1-b03</version> </dependency> <!--mybatis--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.4</version> </dependency> <!--mybatis-spring--> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.0.4</version> </dependency> <!--spring--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!--spring-jdbc--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>5.2.5.RELEASE</version> </dependency> <!--Lombok--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.12</version> </dependency> </dependencies> <!--静态资源导出问题--> <build> <resources> <resource> <directory>src/main/java</directory> <excludes> <exclude>**/*.properties</exclude> <exclude>**/*.xml</exclude> </excludes> <filtering>false</filtering> </resource> <resource> <directory>src/main/resources</directory> <includes> <include>**/*.properties</include> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>

3、在resources包下建立核心配置文件

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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> </beans>

mybatis.xml配置文件

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

db.properties

jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai jdbc.username=root jdbc.password=root

4、在java包下,先写com.xxx。在建立一些包结构

controller、mapper、pojo、service

如下图:

7.2、编写代码之mybatis层

说明:如上图,ssm整合主要是编写resources包下的6个xml配置文件和配置web下的web.xml文件。下编写的books类,BookMapper类,BooksMapper.xml,BooksService接口,BooksServiceImpl类,这5个是为了演示增删改查,不是整合ssm。

1、在pojo包下边编写books类

package com.lu.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data //get、set方法 @AllArgsConstructor //有参构造 @NoArgsConstructor //无参构造 public class Books { private int bookID; private String bookName; private int bookCounts; private String detail; }

2、在mapper包下编写BookMapper

BookMapper类

package com.lu.mapper; import com.lu.pojo.Books; import org.apache.ibatis.annotations.Param; import java.util.List; public interface BooksMapper { //增加图书 int addBook(Books book); //删除图书 int deleteBook(@Param("boodId") int id); //更新图书 int updateBook(Books book); //查询图书queryBookById Books queryBookById(@Param("bookid") int id); //查询全部的书 List<Books> queryAllBook(); }

在resources包下边建一个mapper包,在这个mapper包下新建文件:

BooksMapper.xml

<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lu.mapper.BooksMapper"> <insert id="addBook" parameterType="com.lu.pojo.Books"> insert into books (bookName, bookCounts, detail) values(#{bookName},#{bookCounts},#{detail}); </insert> <delete id="deleteBookById" parameterType="int"> delete from books where bookID = #{bookId}; </delete> <update id="updataBook" parameterType="com.lu.pojo.Books"> update books set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail} where bookID=#{bookID}; </update> <select id="queryBookById" resultType="com.lu.pojo.Books"> select * from books where bookID=#{bookId}; </select> <select id="queryAllBook" resultType="com.lu.pojo.Books"> select * from books; </select> </mapper>

写完mapper.xml要赶紧把它在配置文件mybatis-config.xml中绑定。

完整的mybatis-config.xml

<?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> <typeAliases> <package name="com.lu.pojo"/> </typeAliases> <!--绑定BooksMapper--> <mappers> <mapper resource="mapper/BooksMappers.xml"/> </mappers> </configuration>

3、在service包下新建BooksService接口和BooksServiceImpl类

BooksService接口:这个接口的方法和mapper中的一样,只是没有那两个注解

package com.lu.service; import com.lu.pojo.Books; import java.util.List; public interface BooksService { //增加图书 int addBook(Books book); //删除图书 int deleteBook(int id); //更新图书 int updateBook(Books book); //查询图书 Books queryBookById(int id); //查询全部的书 List<Books> queryAllBook(); }

BooksServiceImpl类

package com.lu.service; import com.lu.mapper.BooksMapper; import com.lu.pojo.Books; import org.springframework.stereotype.Service; import java.util.List; public class BooksServiceImpl implements BooksService{ //service调mapper层, 组合mapper private BooksMapper booksMapper; public void setBooksMapper(BooksMapper booksMapper) { this.booksMapper = booksMapper; } public int addBook(Books books) { return booksMapper.addBook(books); } public int deleteBook(int id) { return booksMapper.deleteBook(id); } public int updateBook(Books books) { return booksMapper.updateBook(books); } public Books queryBookById(int id) { return booksMapper.queryBookById(id); } public List<Books> queryAllBook() { return booksMapper.queryAllBook(); } }

7.3、编写代码之spring层

spring整合mybatis,使用c3p0连接池。

编写spring整合mybatis的核心配置文件spring-mapper.xml。

spring-mapper.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"> <!--1. 关联数据库配置文件--> <context:property-placeholder location="classpath:db.properties"/> <!--2. 连接池: dbcp:半自动化操作,不能自动连接 c3p0:自动化操作(自动化的加载配置文件,并且可以自动设置到对象中) druid;hikari --> <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"/> <!--绑定mybatis配置文件--> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!--4. 配置mapper接口的包,动态的实现了mapper接口可以注入到spring容器中去--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!--注入sqlSessionFactory--> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!--要扫描的mapper包--> <property name="basePackage" value="com.lu.mapper"/> </bean> </beans>

spring整合service层,编写spring-service.xml。因为service层有一个重要的:事务

spring-service.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"> <!--1.扫描servic下的包--> <context:component-scan base-package="com.lu.service"></context:component-scan> <!--2.将我们所有的业务类,注入到spring,可以通过配置,或者注解实现--> <bean id="booksServiceImpl" class="com.lu.service.BooksServiceImpl"> <property name="booksMapper" ref="booksMapper"/> </bean> <!--3.声明式事务配置--> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!--注入数据源--> <property name="dataSource" ref="dataSource"/> </bean> <!--4.aop事务支持--> </beans>

7.4、编写代码之springmvc层

因为创建的不是web项目,要增加web支持。

步骤:右击项目–>选择Add Framework Support–>选择Web Application–>ok

编写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"> <!--1.注解驱动--> <mvc:annotation-driven/> <!--2.静态资源过滤--> <mvc:default-servlet-handler/> <!--3.扫描包--> <context:component-scan base-package="com.lu.controller"/> <!--4.视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp"/> <property name="suffix" value=".jsp"/> </bean> </beans>

配置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"> <!--DispatchServlet--> <servlet> <servlet-name>springmvc</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>springmvc</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--乱码过滤--> <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>

7.5、将配置文件加入applicationContext.xml文件中整合在一起

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" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="classpath:spring-service.xml"/> <import resource="classpath:spring-mapper.xml"/> <import resource="classpath:spring-mvc.xml"/> </beans>

7.6、简单总结

主要是说resources资源包下的6个文件和配置的web.xml(个人理解,欢迎评论纠错)。

ssm框架是Spring+SpringMVC+MyBatis,这三个都有自己的核心配置文件。

在这个ssm整合里面的6个配置文件: mybatis层:mybatis-config.xml,主要是绑定mapper包的接口, db.properties文件,是把驱动、url,密码等单独提出来,进行配置。

spring层:spring-mapper.xml文件,使用连接池连接数据库并放到bean容器中,sqlSessionFactory也交给bean容器管理。 spring-service.xml文件,是把service层的类交给bean容器管理,并且还可以配置事务。

springmvc层:spring-mvc.xml文件,是静态资源过滤、扫描包、配置试图解析器

applicationContext.xml文件是将3个主要的关联在一起,如7.5中的3个import标签。

tomcat运行后出现错误

22-Oct-2020 12:25:09.842 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal One or more Filters failed to start. Full details will be found in the appropriate container log file 22-Oct-2020 12:25:09.842 严重 [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.startInternal Context [/ssm_war_exploded] startup failed due to previous errors

解决如下图

最新回复(0)