(SpringBoot)(一)SpringBoot集成Mybatis

SpringBoot集成Mybatis操作流程

1、快捷创建 SpringBoot 工程

【1】:File—New—Project
(SpringBoot)(一)SpringBoot集成Mybatis_第1张图片
【2】:填写相关配置项

(SpringBoot)(一)SpringBoot集成Mybatis_第2张图片

【3】:选择Web–Spring Web

(SpringBoot)(一)SpringBoot集成Mybatis_第3张图片

【4】填写项目名称以及项目存放的位置

(SpringBoot)(一)SpringBoot集成Mybatis_第4张图片
2、添加Mybatis的起步依赖

在pom.xml配置文件中添加Mybatis的起步依赖

<!--添加Mybatis的起步依赖-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.2</version>
        </dependency>

3、添加数据库驱动坐标

在pom.xml配置文件中添加数据库驱动坐标

<!--添加mysql的驱动连接-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.33</version>
        </dependency>

4、添加数据库连接信息

在 resources 文件夹下面的 application.properties 文件里添加数据库连接信息

#DB Configuration:
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/springbootmybatis?serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=sa

5.创建数据库并加入数据(我的数据库如下)

表结构如下:
(SpringBoot)(一)SpringBoot集成Mybatis_第5张图片
数据项如下:
(SpringBoot)(一)SpringBoot集成Mybatis_第6张图片

6.创建数据库表的实体bean

(SpringBoot)(一)SpringBoot集成Mybatis_第7张图片

/**
 * 数据库User表实体对象
 */
public class User {

    private Integer id;
    private String username;
    private String password;
    private String name;

    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;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "id=" + id +
                ", username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", name='" + name + '\'' +
                '}';
    }

    public User(Integer id, String username, String password, String name) {
        this.id = id;
        this.username = username;
        this.password = password;
        this.name = name;
    }
}

7、编写对应的Mapper和对应的Controller和对应的Service及其实现类
(SpringBoot)(一)SpringBoot集成Mybatis_第8张图片
8、配置Mybatis的对应的配置文件

在 resources 文件夹下创建 mapper 目录,在 mapper 目录下创建 UserMapper.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.netposa.springbootmybatis.mapper.UserMapper">
    <select id="getAllUser" resultType="com.netposa.springbootmybatis.domain.User">
        select * from user;
    </select>
</mapper>

9、配置Mybatis的信息

在application.properties中添加mybatis的信息

#spring集成Mybatis环境
#pojo别名扫描包
mybatis.type-aliases-package=com.netposa.springbootmybatis
#加载Mybatis映射文件
mybatis.mapper-locations=classpath:mapper/*Mapper.xml

10、编写控制层

import com.netposa.springbootmybatis.domain.User;
import com.netposa.springbootmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("/getAllUser")
    @ResponseBody
    public List<User> getAllUser(){
        List<User> list = userService.getAllUser();
        return list;
    }
}

11、编写Service层

import com.netposa.springbootmybatis.domain.User;
import com.netposa.springbootmybatis.mapper.UserMapper;
import com.netposa.springbootmybatis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getAllUser() {
        List<User> list = userMapper.getAllUser();
        return list;
    }
}

12、运行程序启动类,查看结果

使用IDEA自带的测试插件RestServices测试结果如下:

(SpringBoot)(一)SpringBoot集成Mybatis_第9张图片

你可能感兴趣的:(java学习之路)