SSM框架整合

SSM框架整合

  • 环境: Maven3.92 、 Mysql8.0、Tomcat7.0
  • 数据库
/*
 Navicat Premium Data Transfer
 Source Server : Graude
 Source Server Type : MySQL
 Source Server Version : 80035 (8.0.35)
 Source Host : localhost:3306
 Source Schema : ssmbuild
 Target Server Type : MySQL
 Target Server Version : 80035 (8.0.35)
 File Encoding : 65001
 Date: 01/04/2024 16:15:39
*/
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for books
-- ----------------------------
DROP TABLE IF EXISTS `books`;
CREATE TABLE `books` (
 `bookID` int NOT NULL AUTO_INCREMENT,
 `bookName` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
 `bookCounts` int NULL DEFAULT NULL,
 `detail` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NULL DEFAULT NULL,
 PRIMARY KEY (`bookID`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci ROW_FORMAT = Dynamic;
-- ----------------------------
-- Records of books
-- ----------------------------
INSERT INTO `books` VALUES (1, 'Java', 1, '从入门到入土');
INSERT INTO `books` VALUES (2, 'MySQL', 2, '从删库到跑路');
SET FOREIGN_KEY_CHECKS = 1;
  • 导入环境依赖
<!--导入依赖:mysql,junit,连接池,servlet,jsp,spring,mybatis,mybatis-spring-->
 <dependencies>
 <!--mysql-->
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>8.0.33</version>
 </dependency>
 <!--junit-->
 <dependency>
 <groupId>junit</groupId>
 <artifactId>junit</artifactId>
 <version>4.13.1</version>
 <scope>test</scope>
 </dependency>
 <!--数据库连接池-->
 <dependency>
 <groupId>com.mchange</groupId>
 <artifactId>c3p0</artifactId>
 <version>0.9.5.2</version>
 </dependency>
 <!--servlet-->
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>servlet-api</artifactId>
 <version>2.5</version>
 </dependency>
 <!--jsp-->
 <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.9</version>
 </dependency>
 <!--mybatis-spring-->
 <dependency>
 <groupId>org.mybatis</groupId>
 <artifactId>mybatis-spring</artifactId>
 <version>2.0.5</version>
 </dependency>
 <!--spring-->
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>5.3.27</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-jdbc</artifactId>
 <version>5.3.8</version>
 </dependency>
 </dependencies>
  • 静态资源导出问题
<!--静态资源导出问题-->
 <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>

一、配置Mybatis

  1. 配置mybatis-config映射文件
<?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>
 <!--配置数据源,现在交给Spring做-->
 <!--配置别名-->
 <typeAliases>
 <package name="com.wfy.pojo"/>
 </typeAliases>
 <mappers>
 <mapper class="com.wfy.dao.BookMapper"/>
 </mappers>
</configuration>
  1. properties文件

使用的是mysql8.0以上的版本,url要配置时区

配置完成后,测试数据库连接

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssmbuild?useSSL=true&useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
jdbc.username=root
jdbc.password=root
  1. 配置实体类(pojo)
package com.wfy.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data //使用该注解需要导入lombok依赖,偷懒用的
@AllArgsConstructor//有参构造
@NoArgsConstructor//无参构造
public class Books {
 private int bookID;
 private String bookName;
 private int bookCounts;
 private String detail;
}
  1. dao层(数据访问层)
  • BookMapper
package com.wfy.dao;
import com.wfy.pojo.Books;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface BookMapper {
 //增
 int addBook(Books books);
 //删
 int deleteBook(@Param("bookID") int bookID);
 //改
 int updateBook(Books books);
 //查(一本)
 Books queryBookById(@Param("bookID")int bookID);
 //查(全部)
 List<Books> queryAllBook();
}
  • BookMapper.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.wfy.dao.BookMapper">
 <!--增-->
 <insert id="addBook" parameterType="Books">
 insert into ssmbuild.books( bookName, bookCounts, detail)
 values(#{bookName},#{bookCounts},#{detail})
 </insert>
 <!--删-->
 <delete id="deleteBook" parameterType="int" >
 delete from ssmbuild.books where bookID=#{bookID}
 </delete>
 <!--改-->
 <update id="updateBook" parameterType="Books">
 update ssmbuild.books
 set bookName=#{bookName},bookCounts=#{bookCounts},detail=#{detail}
 where bookID=#{bookID}
 </update>
 <!--查-->
 <select id="queryBookById" resultType="Books">
 select * from ssmbuild.books
 where bookID=#{bookID}
 </select>
 <!--查所有-->
 <select id="queryAllBook" resultType="Books">
 select * from ssmbuild.books
 </select>
</mapper>
  1. service层(业务层)

利用service层去调用dao层,最大的作用就是解耦

即一个service层可以调用多个dao层

  • 接口Bookservice
package com.wfy.service;
import com.wfy.pojo.Books;
import java.util.List;
public interface BookService {
 //增
 int addBook(Books books);
 //删
 int deleteBook( int bookID);
 //改
 int updateBook(Books books);
 //查(一本)
 Books queryBookById(int bookID);
 //查(全部)
 List<Books> queryAllBook();
}
  • BookseviceImpl
package com.wfy.service;
import com.wfy.dao.BookMapper;
import com.wfy.pojo.Books;
import java.util.List;
public class BookServiceImpl implements BookService{
 //service层调用dao层:组合dao层
 private BookMapper bookMapper;
 public void setBookMapper(BookMapper bookMapper) {
 this.bookMapper = bookMapper;
 }
 @Override
 public int addBook(Books books) {
 return bookMapper.addBook(books);
 }
 @Override
 public int deleteBook(int bookID) {
 return bookMapper.deleteBook(bookID);
 }
 @Override
 public int updateBook(Books books) {
 return bookMapper.updateBook(books);
 }
 @Override
 public Books queryBookById(int bookID) {
 return queryBookById(bookID);
 }
 @Override
 public List<Books> queryAllBook() {
 return bookMapper.queryAllBook();
 }
}

二、配置Spring

2.1、spring-dao层配置

<?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">
 <!--1.关联数据库配置文件-->
 <context:property-placeholder location="classpath:db.properties"/>
 <!--2.连接池-->
 <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.配置dao接口扫描包,动态的实现了Dao接口可以注入到Spring容器中-->
 <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
 <!--注入sqlSessionFactory-->
 <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
 <!--要扫描的dao包-->
 <property name="basePackage" value="com.wfy.dao"/>
 </bean>
</beans>

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

三、配置SpringMVC

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

三层整合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-dao.xml"/>
 <import resource="classpath:spring-service.xml"/>
 <import resource="classpath:spring-mvc.xml"/>
</beans>
作者:ShamUnite原文地址:https://www.cnblogs.com/wfy-studying/p/18108829

%s 个评论

要回复文章请先登录注册