SSM(SpringMVC+spring+mybatis)整合的步骤以及相关依赖
目录(一)导入SSM框架相关的依赖
①创建一个新的web工程(idea2023版)
②思考三者之间的联系,回忆依赖
③在pom.xml文件中引入依赖坐标
(二)使用注解开辟,编写Spring的设置类(代替applicationContext.xml文件)
(三)编写jdbc.properties文件
(四)编写JdbcConfig设置类
(五)编写Mybatis的设置类
(六)编写SpringMVC相关的设置类
①编写Servlet的设置类,使用注解开辟Servlet
②编写SpringMVC设置类
(一)导入SSM框架相关的依赖
①创建一个新的web工程(idea2023版)
https://i-blog.csdnimg.cn/direct/68cf6f4b366c4a72b1600019f396e65a.png
https://i-blog.csdnimg.cn/direct/2f8f078a4a2d4b39ba567f85f71cb42f.png
设置maven
https://i-blog.csdnimg.cn/direct/38715dd6baae43eabe218b8dcf037143.png
②思考三者之间的联系,回忆依赖
须要的依赖是与Spring、SpringMVC、Mybatis(包含JDBC和数据库毗连相关的依赖)相关,这样我们就可以做一个遐想,思路更加清晰
[*]spring-webmvc 5.2.10.RELEASE
[*]spring-jdbc 5.2.10.RELEASE
[*]spring-test 5.2.10.RELEASE
[*]mybatis 3.5.6
[*]mybatis-spring 1.3.0
[*]mysql-connection-java 5.1.47
[*]junit 4.12
[*]javax-servlet-api 3.1.0
[*]jackson-databind2.9.0
[*]druid 1.1.16
③在pom.xml文件中引入依赖坐标
<dependencies>
<!-- Spring Web MVC 依赖,用于构建Web应用程序 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- Spring JDBC 依赖,用于简化JDBC操作 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.3.14</version>
</dependency>
<!-- Alibaba Druid 依赖,用于数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.6</version>
</dependency>
<!-- Spring Test 依赖,用于单元测试 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.3.14</version>
<scope>test</scope>
</dependency>
<!-- MyBatis 依赖,用于ORM操作 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.8</version>
</dependency>
<!-- MyBatis-Spring 依赖,用于整合MyBatis和Spring -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>2.0.6</version>
</dependency>
<!-- MySQL 连接器依赖,用于连接MySQL数据库 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.28</version>
</dependency>
<!-- Servlet API 依赖,用于Servlet编程,由Servlet容器提供 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<!-- Jackson Databind 依赖,用于JSON处理 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.0</version>
</dependency>
<!-- MyBatis-Spring整合 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.2.0</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!--代码生成器-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
</dependency>
</dependencies>
<build>
<plugins>
<!--配置tomcat插件-->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.1</version>
<configuration>
<port>80</port>
<path>/</path>
</configuration>
</plugin>
</plugins>
</build> lombok可用于生成实体类的get和set方法,这样在类的上方打上@Setter和@Getter注解就可以为属性实现get和set方法,注意:万万不可导错包,应为lombok.getter
(二)使用注解开辟,编写Spring的设置类(代替applicationContext.xml文件)
[*]设置自动扫描的包路径(@ComponentScan注解)
[*]指定须要加载的JDBC以及Mybatis的设置文件(@Import注解)
[*]指定须要扫描的jdbc.properties文件,获取数据库毗连的参数(@PropertySource注解)
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
/**
* Spring配置类,用于定义Spring应用上下文的配置
* 该类使用多个注解来实现不同的配置功能
*/
@Configuration
// 指定Spring在启动时自动扫描的包路径,以便进行组件扫描和自动装配
@ComponentScan("com.xxx.service")
// 指定配置文件的位置,此处为JDBC的属性配置文件
@PropertySource("classpath:jdbc.properties")
// 导入其他配置类,以便将MyBatis和JDBC的配置整合到当前配置中
@Import({MybatisConfig.class, JdbcConfig.class})
public class SpringConfig {
}(三)编写jdbc.properties文件
数据库毗连所须要的四个参数:
[*]数据库驱动、URL、用户名、密码
jdbc.url=jdbc:mysql://localhost:3306/mysql?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC
jdbc.driver=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456(四)编写JdbcConfig设置类
[*]读取jdbc.properties文件中的属性,将四个参数(Driver、URL、username、password)的值注入设置类
[*]设置一个数据源并返回
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
public class JdbcConfig {
@Value("${jdbc.driver}")
private String driver;
@Value("${jdbc.url}")
private String url;
@Value("${jdbc.username}")
private String username;
@Value("${jdbc.password}")
private String password;
@Bean
public DruidDataSource dataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setDriverClassName(driver);
druidDataSource.setUrl(url);
druidDataSource.setUsername(username);
druidDataSource.setPassword(password);
return druidDataSource;
}
}(五)编写Mybatis的设置类
[*]使用构造器的方式注入SqlSessionFactoryBean,并设置数据源,给包设置别名,设置扫描mapper映射文件的路径
[*]使用构造器的方式注入MapperScannerConfigurer,设置MyBatis mapper接口所在的包路径
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import javax.sql.DataSource;
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) throws Exception {
SqlSessionFactoryBean sqlSessionFactoryBean = new SqlSessionFactoryBean();
sqlSessionFactoryBean.setDataSource(dataSource);
//扫描mapper映射文件
sqlSessionFactoryBean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
// 开启驼峰命名和下划线命名的自动转换
sqlSessionFactoryBean.getObject().getConfiguration().setMapUnderscoreToCamelCase(true);
sqlSessionFactoryBean.setTypeAliasesPackage("com.xxx.pojo");
return sqlSessionFactoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setBasePackage("com.xxx.mapper");
return mapperScannerConfigurer;
}
}(六)编写SpringMVC相关的设置类
①编写Servlet的设置类,使用注解开辟Servlet
[*]继续一个接口(AbstractDispatcherServletInitializer),并重写内里的三个方法,使我们能够使用注解设置Servlet
在程序启动会先去加载web.xml,但是现在用Servlet设置类替换了web.xml,所以程序会首先加载设置类,程序在加载这个类的过程中,会一同加载Spring的设置类以及SpringMvc的设置类
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* ServletReplaceConfig 类继承自 AbstractAnnotationConfigDispatcherServletInitializer,
* 用于配置 Spring 的 DispatcherServlet 初始化参数.
* 该类主要用于定义根配置类和Servlet配置类,以及映射Servlet到特定的请求路径.
*/
public class ServletReplaceConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
/**
* 获取根配置类.
* 这些配置类通常包含全局的配置信息,比如数据库连接池配置、服务配置等.
*
* @return Class<?>[] 返回一个包含根配置类的数组.
*/
@Override
protected Class<?>[] getRootConfigClasses() {
// 返回包含SpringConfig配置类的数组
return new Class[]{SpringConfig.class};
}
/**
* 获取Servlet配置类.
* 这些配置类通常包含Web相关的配置信息,比如视图解析器配置、静态资源处理等.
*
* @return Class<?>[] 返回一个包含Servlet配置类的数组.
*/
@Override
protected Class<?>[] getServletConfigClasses() {
// 返回包含SpringMvcConfig配置类的数组
return new Class[]{SpringMvcConfig.class};
}
/**
* 获取Servlet的映射路径.
* 这里定义了DispatcherServlet处理的请求路径,"/"表示处理所有路径的请求.
*
* @return String[] 返回一个包含Servlet映射路径的数组.
*/
@Override
protected String[] getServletMappings() {
// 返回包含根路径映射的数组,意味着拦截所有请求
return new String[]{"/"};
}
}②编写SpringMVC设置类
[*]指定Spring组件扫描的包路径,自动检测并注册为bean(@Component)
[*]启用SpringMVC的注解支持(@EnableWebMvc)
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* Spring MVC配置类
* 该类使用@Configuration注解标记为一个配置类,替代XML配置文件
* 通过@ComponentScan注解指定Spring组件扫描的包路径,自动检测并注册为Bean
* 使用@EnableWebMvc注解启用Spring MVC的配置支持
*/
@Configuration
@ComponentScan({"com.minyudie.controller", "com.minyudie.config"})
@EnableWebMvc
public class SpringMvcConfig {
// 类定义结束
}
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]