当前位置: 移动技术网 > IT编程>开发语言>Java > springboot整合mybatis的简单过程

springboot整合mybatis的简单过程

2020年07月17日  | 移动技术网IT编程  | 我要评论

前言

什么是springboot?
Spring Boot 是由 Pivotal 团队提供的全新框架。Spring Boot 是所有基于 Spring Framework 5.0 开发的项目的起点。Spring Boot 的设计是为了让你尽可能快的跑起来 Spring 应用程序并且尽可能减少你的配置文件。

项目的框架

在这里插入图片描述

环境搭建

  1. 添加依赖

     <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>
    
  2. 配置数据的数据源——在application.yml中

     server:
      port: 8888
    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://localhost:3306/test
        driver-class-name: com.mysql.jdbc.Driver
    
  3. 配置映射文件的扫描

     mybatis:
      mapper-locations: classpath:mapping/*Mapper.xml
      type-aliases-package: com.example.department.entity
    
  4. 扫描映射接口——启动类上

    @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

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网