Web 基础 之 Eclipse 中 SpringBoot 整合 SSM 环境 配置文件版和 注解版 两种版本方法使用的简单整理
目录
Web 基础 之 Eclipse 中 SpringBoot 整合 SSM 环境 配置文件版和 注解版 两种版本方法使用的简单整理
一、简单介绍
二、相关环境
三、SpringBoot 整合 SSM 环境 ,使用配置文件的方法
四、SpringBoot 整合 SSM 环境 ,使用注解的方法
一、简单介绍
Web 开发的一些知识整理,方便后期遇到类似的问题,能够及时查阅使用。
本节介绍,Web 开发 中Eclipse 中 SpringBoot 整合 SSM 环境 配置文件版和 注解版 两种版本方法使用的简单整理,其实主要区别整理在于调用数据库的方法上,配置文件的需要引入 c3p0;两个整合环境版本放在一起,可以直观感受两者区别,根据需要选择合适版本使用即可,如果有不足之处,欢迎指出,或者你有更好的方法,欢迎留言。
二、相关环境
1、Windows 10
2、Eclipse 2021-06 (4.20.0)
3、apache-maven-3.8.3
4、MySQL 8.0.23 + MySQL Workbench 8.0 CE
三、SpringBoot 整合 SSM 环境 ,使用配置文件的方法
1、打开网址,创建一个 maven 工程,添加 Spring Web 、Mybatis framework、MySQL Driver
网址:https://start.spring.io/
(注意:spring boot 版本根据需要选取,java 根据自己实际安装版本选择,其他也根据自己需要设置添加即可 )
2、设置好了,下载工程到本地,然后解压
3、打开 Eclipse ,File - import 导入 maven 工程
4、 等待一会儿,就好导入,并显示
(没有的话,可能还在加载,或者切换下工程状态,或者重启下 Eclipse)
5、打开 MySQL Workbench ,新建一个数据库和表,并且添加些数据,便于简单测试使用
6、在工程 src/main/java 下添加一些 package ,用来简单分类管理一些 脚本
7、在 entity package 下添加 User 数据模型,与之前 MySQL 创建的表的数据项目对上
package com.example.maven_srpingboot_ssm_config_file_test.entity;
public class User {
public Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", username=" + username + ", password=" + password + "]";
}
}
8、在 dao package 下创建 接口脚本 UserDao,这里是可以对应连接数据库,进行操作的接口脚本(这里为了测试,只添加简单的查询接口)
package com.example.maven_srpingboot_ssm_config_file_test.dao;
import java.util.List;
import com.example.maven_srpingboot_ssm_config_file_test.entity.User;
public interface UserDao {
// 查 select * from user
List searchAllUsers();
}
9、在工程下src/main/resource 下添加一个 mapper 文件夹,添加 xml 文件 UserMapper 和 MybatisConfig
(UserMapper 用来数据库语句的映射(这里简单配置查询数据的语句),MybatisConfig 配置一些参数)
UserMapper.xml (注意 与 dao 脚本对应上)
MybatisConfig.xml
10、在 pom.xml 中引入 c3p0
4.0.0 org.springframework.boot spring-boot-starter-parent2.5.6 com.example maven_srpingboot_ssm_config_file_test0.0.1-SNAPSHOT maven_srpingboot_ssm_config_file_test Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-weborg.mybatis.spring.boot mybatis-spring-boot-starter2.2.0 mysql mysql-connector-javaruntime org.springframework.boot spring-boot-starter-testtest com.mchange c3p00.9.5.2 org.springframework.boot spring-boot-maven-plugin
11、在 config package 添加一个脚本,配置数据库等相关信息,关键是与一些数据构建各个关系,关键脚本(注意:一些数据库相关替换成你的数据库相关)
package com.example.maven_srpingboot_ssm_config_file_test.config;
import java.beans.PropertyVetoException;
import java.io.IOException;
import javax.sql.DataSource;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import com.mchange.v2.c3p0.ComboPooledDataSource;
@Configuration
// mapper 的扫描路径
@MapperScan("com.example.maven_srpingboot_ssm_config_file_test.dao")
public class SQLConfig {
@Autowired
private DataSource dataSource; // 名称 与 @Bean(name="dataSource") 对上
// dataSource
@Bean(name="dataSource")
public ComboPooledDataSource createDataSource() throws PropertyVetoException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
// driver
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
// url
dataSource.setJdbcUrl("jdbc:mysql://::1:3306/maven_ssm?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true");
// username
dataSource.setUser("root");
// password
dataSource.setPassword("root123");
dataSource.setAutoCommitonClose(false);
return dataSource;
}
// SessionFactory
@Bean(name="sqlSessionFactory")
public SqlSessionFactoryBean createSqlSessionFactoryBean() throws IOException {
SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
// 加载主配置文件 mybatis-config.xml
bean.setConfigLocation(new ClassPathResource("MybatisConfig.xml"));
// Mapper 扫描路径
PathMatchingResourcePatternResolver reslover = new PathMatchingResourcePatternResolver();
String packageSearchPath = "classpath*:/mapper/**.xml";
bean.setMapperLocations(reslover.getResources(packageSearchPath));
// 配置实体包
bean.setTypeAliasesPackage("com.example.maven_srpingboot_ssm_config_file_test.entity");
// dataSource
bean.setDataSource(dataSource);
return bean;
}
}
12、在 controller package 添加一个测试脚本 TestController,用来测试配置文件方法,调用数据库是否配置成功,这里简单查询数据库信息
package com.example.maven_srpingboot_ssm_config_file_test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.maven_srpingboot_ssm_config_file_test.dao.UserDao;
import com.example.maven_srpingboot_ssm_config_file_test.entity.User;
@RestController
public class TestController {
@Autowired
private UserDao mUserDao;
@RequestMapping("/test")
public String testSearchAllUsers() {
List users = mUserDao.searchAllUsers();
for(User user : users) {
System.out.println(user.toString());
}
return "testSearchAllUsers";
}
}
13、如图,选择启动脚本,右键 Run As, 运行 maven 工程
14、在网页输入 localhost:8080/test,控制台 Console 打印出数据库的信息
15、到此 SpringBoot 整合 SSM 环境 ,使用配置文件的方法基本完毕
四、SpringBoot 整合 SSM 环境 ,使用注解的方法
1、打开网址,创建一个 maven 工程,添加 Spring Web 、Mybatis framework、MySQL Driver
网址:https://start.spring.io/
(注意:spring boot 版本根据需要选取,java 根据自己实际安装版本选择,其他也根据自己需要设置添加即可 )
2、设置好了,下载工程到本地,然后解压
3、打开 Eclipse ,File - import 导入 maven 工程
4、 等待一会儿,就好导入,并显示
(没有的话,可能还在加载,或者切换下工程状态,或者重启下 Eclipse)
5、打开 MySQL Workbench ,新建一个数据库和表,并且添加些数据,便于简单测试使用
(使用上个案例的数据)
6、在工程 src/main/resources 中的application.properites 添加数据库信息
(注意把文件改为 UTF-8 ,数据库关键信息替换成你数据库信息)
# 数据库 url (指定具体数据库) spring.datasource.url=jdbc:mysql://::1:3306/maven_ssm?serverTimezone=UTC&characterEncoding=utf-8&useSSL=true # 数据库登录用户名 spring.datasource.username=root # 数据库登录密码 spring.datasource.password=root123 # 数据库驱动类名 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
7、在工程 src/main/java 下添加一些 package ,用来简单分类管理一些 脚本
8、在 entity package 下添加 User 脚本
(注意:User 参数与 数据库表对应上)
9、在 dao package 下添加 UserMapper 接口文件,用来映射到数据库的查询语言
(这里测试只添加查询数据库信息)
package com.example.maven_srpingboot_ssm_comment_test.dao;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import com.example.maven_srpingboot_ssm_comment_test.entity.User;
@Mapper
public interface UserMapper {
@Select("select * from user")
List searchAllUsers();
}
10、在 controller package 添加一个测试脚本 TestController,用来测试配置文件方法,调用数据库是否配置成功,这里简单查询数据库信息
package com.example.maven_srpingboot_ssm_comment_test.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.example.maven_srpingboot_ssm_comment_test.dao.UserMapper;
import com.example.maven_srpingboot_ssm_comment_test.entity.User;
@RestController
public class TestController {
@Autowired
private UserMapper mUserMapper;
@RequestMapping("/test")
public String testSearchAllUsers() {
List users = mUserMapper.searchAllUsers();
for(User user : users) {
System.out.println(user.toString());
}
return "testSearchAllUsers";
}
}
11、如图,选择启动脚本,右键 Run As, 运行 maven 工程
12、在网页输入 localhost:8080/test,控制台 Console 打印出数据库的信息
13、到此 SpringBoot 整合 SSM 环境 ,使用注解的方法实现基本完毕



