🐌个人主页: 🐌 叶落闲庭
💨我的专栏:💨
c语言
数据结构
javaweb
石可破也,而不可夺坚;丹可磨也,而不可夺赤。
SSM整合
- 一、创建工程
- 1.1创建Maven工程
- 1.2工程命名
- 1.3检查&配置本地Maven路径
- 二、SSM整合
- 2.1导入相关依赖
- 2.2添加tomcat插件(可选)
- 2.3添加配置类
- 2.3.1添加Spring配置类
- 2.3.2添加Jdbc配置类
- 2.3.3添加Mybatis配置类
- 2.3.4创建整合类(整合Spring和SpringMVC)
- 2.3.5创建SpringMVC配置类
- 总结
一、创建工程
1.1创建Maven工程
1.2工程命名
1.3检查&配置本地Maven路径
二、SSM整合
2.1导入相关依赖
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.2.10.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>3.5.13</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.2.16</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.15.0</version>
</dependency>
</dependencies>
2.2添加tomcat插件(可选)
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!--设置端口号及访问路径-->
<port>80</port>
<path>/</path>
</configuration>
</plugin></plugins>
</build>
2.3添加配置类
- 项目包结构:
2.3.1添加Spring配置类
- 创建包结构
com.practice.config
,创建SpringConfig
类
package com.practice.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.PropertySource;
/**
* @Author YJ
* @Date 2023/8/13 16:50
* Description:Spring配置类
*/
@Configuration//表明配置类
@ComponentScan({"com.practice.service"})//扫描包路径
@PropertySource("jdbc.properties")//获取资源路径
@Import({JdbcConfig.class,MybatisConfig.class})//spring与mybatis整合
public class SpringConfig {
}
2.3.2添加Jdbc配置类
- 创建jdbc属性文件并添加如下代码表示连接数据库信息
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456
- 在
com.practice.config
下创建JdbcConfig
配置类
package com.practice.config;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* @Author YJ
* @Date 2023/8/13 16:51
* Description:Jdbc配置类
*/
public class JdbcConfig {
/*jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=123456*/
//通过注解,将jdbc属性文件中的属性获取到
@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 DataSource dataSource(){
DruidDataSource dataSource = new DruidDataSource();
dataSource.setDriverClassName(driver);
dataSource.setUrl(url);
dataSource.setUsername(username);
dataSource.setPassword(password);
return dataSource;
}
}
2.3.3添加Mybatis配置类
package com.practice.config;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
/**
* @Author YJ
* @Date 2023/8/13 16:52
* Description:Mybatis配置类
*/
public class MybatisConfig {
@Bean
public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
factoryBean.setTypeAliasesPackage("com.practice.domain");
return factoryBean;
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer(){
MapperScannerConfigurer msc = new MapperScannerConfigurer();
msc.setBasePackage("com.practice.dao");
return msc;
}
}
2.3.4创建整合类(整合Spring和SpringMVC)
package com.practice.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
/**
* @Author YJ
* @Date 2023/8/13 17:12
* Description:整合Spring和SpringMVC
*/
public class ServletConfig extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{SpringConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{SpringMVCConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
2.3.5创建SpringMVC配置类
package com.practice.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
/**
* @Author YJ
* @Date 2023/8/13 17:13
* Description:SpringMVC配置类
*/
@Configuration
@ComponentScan("com.practice.controller")
@EnableWebMvc
public class SpringMVCConfig {
}
总结
这篇文章主要是关于Spring的SSM整合的相关内容,基本步骤就已经总结完毕,欢迎各位小伙伴点赞+关注!!!