Spring Boot实现SSM整合
6.1 创建工程,导入静态资源
整体目录结构:
编写HTML页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h2>根据ID查询员工数据</h2>
<input type="button" value="点我" onclick="fn01()"/>
<script src="/js/axios-0.18.0.js"></script>
<script>
function fn01(){
axios.get("http://localhost:8080/emp/findById?empId=1").then(function(response){
console.log(response.data);
});
}
</script>
</body>
</html>
6.2 引入依赖
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<!--所有的springboot工程需要继承的父工程-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.10.RELEASE</version>
</parent>
<dependencies>
<!--web开发的相关依赖 场景启动器依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.3.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.25</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
<!-- SpringBoot应用打包插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!--如果运行出现:
Failed to execute goal org.apache.maven.plugins:maven-resourcesplugin:3.2.0:resource
错误就给下面注释解开-->
<!--<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<!--修改版本-->
<version>3.1.0</version>
</plugin>-->
</plugins>
</build>
</project>
6.3 编写配置文件
#配置服务器端口
server:
port: 8080
#数据库信息配置
spring:
datasource:
class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: 123456
type: com.alibaba.druid.pool.DruidDataSource
application:
name: /
#配置映射配置文件
mybatis:
configuration:
map-underscore-to-camel-case: true #驼峰映射
mapper-locations: classpath:mappers/*.xml
type-aliases-package: com.ll.pojo
上面的代码复制到yml里面 基本配置结构就是这样子:
注意事项:不要去修改黄色代码的位置不要在前面或则后面乱打空格 直接复制过去就好了,因为yml对等级区分非常敏感,如果修改黄色代码的位置很容易启动不成功,而且在配置多的情况下还极难发现;
6.4 创建启动类
/**
* @SpringBootApplication 标识是SpringBoot的启动类
* 在引导类上添加 @MapperScan 注解
*/
@SpringBootApplication
@EnableTransactionManagement//开启事务支持
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
6.5 创建实体类
Emp实体
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Emp {
private Integer empId;
private String empName;
private Double empSalary;
}
状态信息实体:
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Result {
private int code;//响应状态码
private String msg;//响应消息
private Object data;//响应数据
}
6.6 编写Mapper
编写Mapper接口:
@Mapper
public interface EmpMapper {
public Emp findEmpById(Integer empId);
}
映射配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.lzw.mapper.EmpMapper">
<!--根据ID查询员工信息-->
<select id="findEmpById" resultType="Emp">
select * from t_emp where emp_id = #{empId}
</select>
</mapper>
6.7 编写Service
Service接口:
public interface EmpService {
public Emp findEmpById(Integer empId);
}
Service实现类:
@Service
@Transactional
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpMapper empMapper;
@Override
public Emp findEmpById(Integer empId) {
return empMapper.findEmpById(empId);
}
}
6.8 编写Controller
表现层代码;
@RestController
//@RestController=@Controller+@ResponseBody
@RequestMapping("/emp")
public class EmpController {
@Autowired
private EmpService empService;
//URL+请求方式
//根据ID查询
@GetMapping("/{empId}")
public Result
findById(@PathVariable("empId") Integer empId){
Emp emp = empService.findEmpById(empId);
Result result = new Result(20000,"查询成功",emp);
return result;
}
}
测一下结果: