Spring MVC 框架学习---- SSM 框架整合
Posted RAIN 7
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring MVC 框架学习---- SSM 框架整合相关的知识,希望对你有一定的参考价值。
文章目录
Spring MVC 框架学习(八)---- SSM 框架整合
一、准备工作导入依赖
导入 spring mybatis mvc 等依赖,同时过滤 xml文件和 properties 文件
<?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>
<groupId>org.example</groupId>
<artifactId>2022-6-29</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<!--lombok 插件 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok-maven-plugin</artifactId>
<version>1.18.12.0</version>
<scope>provided</scope>
</dependency>
<!-- spring 核心依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<!-- 测试依赖-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- mysql8.0 驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<!-- aop 织入包-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.9.1</version>
</dependency>
<!-- mybatis依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.6</version>
</dependency>
<!-- spring-mybatis 依赖 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.7</version>
</dependency>
<!-- srping-jdbc 提供 spring 的数据源dataSource -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.21</version>
</dependency>
<!-- 事务相关的依赖包 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.3.21</version>
</dependency>
<!-- spring-mvc 的底层有servlet实现,必须导入 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>
<!--下面都是 mvc 有返回json数据的要求的话,必须导入的json相关依赖包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.3</version>
</dependency>
</dependencies>
<!-- 在运行的时候使得 xml文件和 properties文件得以留下 -->
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>
总体配置文件架构
建表语句,测试 mybatis 是否成功连接
create database if not exists ssm;
use ssm;
drop table if exists users;
create table users(
id int primary key auto_increment,
username varchar(20),
password varchar(20)
);
总的applicationContext.xml 文件
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:cache="http://www.springframework.org/schema/cache"
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
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
<!--导入spring-dao.xml配置文件-->
<import resource="classpath:spring-dao.xml"/>
<!-- 导入spring-mvc 配置文件-->
<!-- 导入spring-service 配置文件-->
<!-- 自动扫描包路径下的注解-->
<context:component-scan base-package="com.*"/>
<!-- 开启aop支持-->
<aop:aspectj-autoproxy/>
</beans>
二、Spring 整合 Mybatis
db.properties
jdbc.driverClassName=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/ssm?useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT
jdbc.username=root
jdbc.password=123456
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.bit.pojo"/>
</typeAliases>
</configuration>
spring-dao.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
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
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 引入外部的 properties 文件-->
<context:property-placeholder location="classpath:db.properties" />
<!-- 拿到spring的数据源,以后可以是c3p0、德鲁伊等数据源-->
<bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
<property name="driverClassName" value="$jdbc.driverClassName"/>
<property name="username" value="$jdbc.username"/>
<property name="url" value="$jdbc.url"/>
<property name="password" value="$jdbc.password"/>
</bean>
<!-- 拿到sqlSessionFactory-->
<bean class="org.mybatis.spring.SqlSessionFactoryBean" id="sqlSessionFactory">
<property name="dataSource" ref="dataSource"/>
<property name="configLocation" value="classpath:mybatis-config.xml"/>
<property name="mapperLocations" value="classpath:com/bit/mapper/UserMapper.xml"/>
</bean>
<!-- 在配置dao接口扫描包,动态实现了dao接口注入到 spring容器中-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<!-- 注入sqlSessionFactory-->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
<property name="basePackage" value="com.bit.mapper"/>
<!-- 经过上面两步,相当于给接口加入了SqlSession,也相当于个接口创建一个实现类然后getMapper再去使用mybatis-->
</bean>
</beans>
建一个pojo包,创建一个实体类
package com.bit.pojo;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User
private int id;
private String username;
private String password;
建一个mapper 包,创建一个mapper接口以及 mapper.xml 文件
UserMapper 接口
package com.bit.mapper;
import com.bit.pojo.User;
import org.apache.ibatis.annotations.Param;
import java.util.List;
public interface UserMapper
int insert(User user);
int delete(@Param("id") int id);
int update(@Param("id") int id,User user);
List<User> selectAll();
User selectById(int id);
UserMapper.xml 文件
<?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.bit.mapper.UserMapper">
<insert id="insert" parameterType="user">
insert into users values(#id,#username,#password)
</insert>
<delete id="delete">
delete from users where id=#id
</delete>
<update id="update" parameterType="user">
update users set username=#username,password=#password where id=#id
</update>
<select id="selectAll" resultType="user" >
select * from users;
</select>
<select id="selectById" resultType="user" >
select * from users where id=#id;
</select>
</mapper>
在service 层创建接口,对dao层进行封装
创建一个service包,写一个UserService接口 和 UserServiceImpl 实现类
UserService 接口
package com.bit.service;
import com.bit.pojo.User;
import java.util.List;
public interface UserService
int addUser(User user);
int delteteUser(int id);
int updateUser(int id,User user);
List<User> selectAllUser();
User selectOne(int id);
UserServiceImpl 实现类
package com.bit.service;
import com.bit.mapper.UserMapper;
import com.bit.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService
@Autowired
private UserMapper userMapper;
public int addUser(User user)
return userMapper.insert(user);
public int delteteUser(int id)
return userMapper.delete(id);
public int updateUser(int id, User user)
return userMapper.update(id,user);
public List<User> selectAllUser()
return userMapper.selectAll();
SSM框架和SSH框架的区别