springboot整合mybatis的简单过程
程序员文章站
2022-06-14 21:53:29
前言什么是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
上一篇: java双色球抽奖
推荐阅读
-
面试-springboot+Mybatis实现简单的登录功能
-
springboot整合mail实现邮箱的发送功能
-
springboot整合mybatis连接mysql数据库出现SQLException异常
-
从零开始搭建springboot+springcloud+mybatis本地项目全过程
-
springboot整合mybatis
-
在 Golang 中实现一个简单的Http中间件过程详解
-
SpringBoot整合log4j2日志的实现
-
SpringBoot整合mybatis进行快速开发
-
使用爬虫框架htmlunit整合springboot出现的一个不兼容问题
-
SpringBoot2.X 整合RedisTemplate 简单实现消息队列