springboot整合mybatis的简单过程
程序员文章站
2022-03-11 08:53:16
前言什么是springboot?Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。项目的框架......
前言
什么是springboot?
Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。
项目的框架
环境搭建
-
添加依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <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>1.3.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.46</version> <scope>runtime</scope> </dependency>
-
配置数据的数据源——在application.yml中
server: port: 8888 spring: datasource: username: root password: root url: jdbc:mysql://localhost:3306/test driver-class-name: com.mysql.jdbc.Driver
-
配置映射文件的扫描
mybatis: mapper-locations: classpath:mapping/*Mapper.xml type-aliases-package: com.example.department.entity
-
扫描映射接口——启动类上
@SpringBootApplication @MapperScan("com.example.department.dao") public class DepartmentApplication { public static void main(String[] args) { SpringApplication.run(DepartmentApplication.class, args); } }
注意:若java包下包含配置文件(如mapper.xml),则需要在pom.xml中配置扫描java下的配置文件路径。
代码实现
//定义实体类
public class Department {
private Integer id;
private String departmentName;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getDepartmentName() {
return departmentName;
}
public void setDepartmentName(String departmentName) {
this.departmentName = departmentName;
}
//定义dao层
@Repository
public interface DepartmentMapper {
public List<Department> getAll();
}
//定义mapper.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.department.dao.DepartmentMapper">
<select id="getAll" resultType="com.example.department.entity.Department">
select * from department
</select>
//定义service层
@Service
public class DepartmentService {
@Autowired
private DepartmentMapper departmentMapper;
public List<Department> getAll(){
return departmentMapper.getAll();
}
}
//定义controller层
@Controller
public class DepartmentController {
@Autowired
private DepartmentService departmentService;
@RequestMapping("/getAll")
@ResponseBody
public String getAll(){
List<Department> all = departmentService.getAll();
for (Department d:all) {
System.out.println(d);
}
return "success";
}
}
部署springboot
测试
通过localhost:8888/getAll 访问controller层的方法。
本文地址:https://blog.csdn.net/qq_40693603/article/details/107368095
推荐阅读
-
SQL Server 2005 创建简单的存储过程--总结分析
-
spring boot整合mybatis+mybatis-plus的示例代码
-
springboot整合ehcache 实现支付超时限制的方法
-
springboot整合rabbitmq的示例代码
-
详解SpringBoot结合swagger2快速生成简单的接口文档
-
mybatis处理枚举类的简单方法
-
Mybatis分页插件PageHelper的配置和简单使用方法(推荐)
-
Spring mvc整合tiles框架的简单入门教程(maven)
-
Spring + Spring Boot + MyBatis + MongoDB的整合教程
-
SpringBoot+Mybatis项目使用Redis做Mybatis的二级缓存的方法