SSM框架整合
创建工程
新建一个web工程,其下的文件组织结构如下:
1 2 3 4 5 6 7 8 9 10 11 12
| project ├─.idea ├─.mvn ├─src │ │ └─com │ │ └─yourname │ ├─resources │ └─webapp │ └─WEB-INF └─test ├─java └─resources
|
然后在pom.xml
导入依赖坐标:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>4.0.1</version> <scope>provided</scope> </dependency> <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.3.26</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.3.26</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis</artifactId> <version>3.5.11</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>2.1.0</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</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.2</version> <scope>test</scope> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.13.5</version> </dependency>
|
SSM整合
Spring配置
在com.yourname.config
包下创建SpringConfig
类:
1 2 3 4 5 6
| @Configuration @ComponentScan({"com.yourname.service"}) @PropertySource("jdbc.properties") @Import({JdbcConfig.class, MybatisConfig.class}) public class SpringConfig { }
|
MyBatis配置
在com.yourname.config
包下创建MybatisConfig
类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| package com.yourname.config;
import org.mybatis.spring.SqlSessionFactoryBean; import org.mybatis.spring.mapper.MapperScannerConfigurer; import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
public class MybatisConfig { @Bean public SqlSessionFactoryBean sqlSessionFactory(DataSource dataSource) { SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean(); factoryBean.setDataSource(dataSource); factoryBean.setTypeAliasesPackage("com.yourname.domain"); return factoryBean; }
@Bean public MapperScannerConfigurer mapperScannerConfigurer() { MapperScannerConfigurer scan = new MapperScannerConfigurer(); scan.setBasePackage("com.yourname.dao"); return scan; } }
|
在resources
文件夹下创建配置文件jdbc.properties
:
1 2 3 4
| jdbc.driver=com.mysql.cj.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/your_database_name jdbc.username=root jdbc.password=password
|
在com.yourname.config
包下创建JdbcConfig
类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.takune.config;
import com.alibaba.druid.pool.DruidDataSource; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean;
import javax.sql.DataSource;
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 DataSource dataSource() { DruidDataSource dataSource = new DruidDataSource(); dataSource.setDriverClassName(driver); dataSource.setUrl(url); dataSource.setUsername(username); dataSource.setPassword(password); return dataSource; } }
|
SpringMVC配置
在com.yourname.config
包下创建ServletConfig
类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| package com.takune.config;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
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[]{"/"}; } }
|
在com.yourname.config
包下创建SpringMvcConfig
类:
1 2 3 4 5 6 7 8 9 10 11
| package com.takune.config;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration @ComponentScan({"com.takune.controller"}) @EnableWebMvc public class SpringMvcConfig { }
|
功能模块
表与实体类
使用如下指令创建book
表:
1 2 3 4 5 6
| create table book( id int not null auto_increment primary key , name varchar(20) not null , type varchar(20) not null , description varchar(100) not null );
|
在com.yourname.config
包下创建实体类Book
:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.takune.domain;
public class Book { private Integer id; private String name; private String type; private String description;
public Integer getId() { return id; }
public void setId(Integer id) { this.id = id; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public String getDescription() { return description; }
public void setDescription(String description) { this.description = description; } }
|
dao(接口+自动代理)
service(接口+实现类)
业务层接口测试(整合Junit)
controller
表现层接口测试(Postman)
创建SpringBoot项目
从零开始的SpringBoot项目
接下来我们将用IEDA从零开始创建一个新的SpringBoot项目。
首先点击新建项目,选择Spring Initializr, 给你的项目取一个合适的名字, 使用maven对项目进行管理, 选择合适的jdk版本, 如下图所示:
然后选择合适的SpringBoot版本和依赖包(这里我选了Spring Web和MySQL Driver, 可以根据实际需要进行选择):
到此为止我们就创建了一个SpringBoot项目!
相关依赖的配置
这一部分我们会介绍SpringBoot项目中的一些依赖的配置, 以便我自己后来参考🤯🤯🤯
druid
在pom.xml
中引入druid的坐标, 然后安装依赖:
1 2 3 4 5
| <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.15</version> </dependency>
|
在application.yml
中填写配置信息:
1 2 3 4 5 6 7 8
| spring: ... datasource: druid: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/blog username: root password: password
|