4 SpringBoot 整合 Mybatis

SpringBoot 整合 Mybatis

  上一节已经介绍了第一个 SpringBoot 应用程序,接下来介绍何如整合 Mybatis ,和数据库进行打交道。

1 引入依赖

  • 我们用 alibaba druid 数据库连接池进行连接
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.1.10</version>
</dependency>
  • 引入 MySQL
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.35</version>
</dependency>
  • 引入 Mybatis
<dependency>
	<groupId>org.mybatis.spring.boot</groupId>
	<artifactId>mybatis-spring-boot-starter</artifactId>
	<version>1.3.2</version>
</dependency>

2 配置文件

  • application.yml 中配置数据库连接
spring:
  application:
    name: hello-springboot
  datasource:
    druid:
      # 若为 MySQL 8.0x,则为 com.mysql.cj.jdbc.Driver
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&useSSL=false
      username: root
      password: host
      # 定义初始连接数
      initial-size: 1
      # 最小空闲数
      min-idle: 1
      # 最大连接数
      max-active: 20
      # 检测数据库连接是否可用
      test-on-borrow: true
  • 配置 Mybatis
mybatis:
  # mapper.xml 中的 resultType 或 resultMap 返回的实体类会自动在此包下找
  type-aliases-package: com.pky.hello.springboot.commons.domain
  # 指定 mapper.xml 的路径
  mapper-locations: classpath:mapper/*.xml

3 创建 domain 实体类

  创建与数据库表中各字段一一对应的实体类 User。

package com.pky.hello.springboot.commons.domain;

import java.io.Serializable;

public class User implements Serializable{

    /**
     * id,唯一标识,主键
     */
    private Integer id;
    /**
     * 用户名,唯一索引
     */
    private String username;
    /**
     * 密码
     */
    private String password;

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

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

注意:属性若是基本数据类型则改为其包装类,避免前端不传指过来而导致类型 null基本数据类型不匹配

4 创建三层架构

4.1 mapper 层

  即数据访问层,和数据库打交道。

  • mapper 接口:在 com.pky.hello.springboot 包下创建包 commons.mapper,并在 mapper 包下创建 UserMapper 接口。
package com.pky.hello.springboot.commons.mapper;

import com.pky.hello.springboot.commons.domain.User;
import org.springframework.stereotype.Repository;

@Repository
public interface UserMapper {

    /**
     * 通过用户名获取用户信息
     * @param username
     * @return
     */
    User getByLoginId(String username);
}
  • mapper.xml: 在 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.pky.hello.springboot.commons.mapper.UserMapper">
    <sql id="UserColumns">
        a.id,
        a.username,
        a.password
    </sql>

    <!--查询登陆用户信息-->
    <select id="getByLoginId" resultType="User">
        SELECT
        <include refid="UserColumns" />
        FROM
        user AS a
        WHERE a.username = #{username}
    </select>

</mapper>

4.2 service 层

  即业务逻辑层,做数据的处理。

  • 在上述的 commons 包下创建 service 包,并在 service 包下创建 LoginService 接口
package com.pky.hello.springboot.commons.service;

import com.pky.hello.springboot.commons.domain.User;

public interface LoginService {

    /**
     * 通过用户名获取用户信息
     * @param user
     * @return
     */
    User getByLoginId(User user);
}
  • 在 service 包下创建 imp 包,并在 impl 包中创建 上述接口的实现 LoginServiceImpl
package com.pky.hello.springboot.commons.service.impl;

import com.pky.hello.springboot.commons.domain.User;
import com.pky.hello.springboot.commons.mapper.UserMapper;
import com.pky.hello.springboot.commons.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 登录业务逻辑
 */
@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    UserMapper userMapper;

    /**
     * 通过用户名获取用户信息
     * @param user
     * @return
     */
    @Override
    public User getByLoginId(User user) {
        User user1 = userMapper.getByLoginId(user.getUsername());

        if(user1 != null) {
            return user1;
        }
        return null;
    }
}

4.3 Controller 层

package com.pky.hello.springboot.controller;

import com.pky.hello.springboot.commons.domain.User;
import com.pky.hello.springboot.commons.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 登录控制器
 */
@RestController
@RequestMapping(value = "login")
public class LoginController {

    @Autowired
    LoginService loginService;

    /**
     * 登录
     * @param user
     * @return
     */
    @GetMapping(value = "user")
    public User login(User user) {
        User user1 = loginService.getByLoginId(user);
        return user1;
    }
}

5 测试

  浏览器上输入 http://localhost:8082/v1/hello_springboot/login/user?username=admin

image

                    图5.1

  如图 5.1 所示,一套完整的 SpringBoot 整合 Mybatis 项目则完成。

  接下来分两种情况进行介绍:

  • 一是使用 templates 模板引擎做一个页面和业务代码的混合项目;
  • 二是通过前端项目调用后端项目 API,得到 JSON 数据后显示到页面上(前后分离

猜你喜欢

转载自blog.csdn.net/qq_34369588/article/details/94740127
今日推荐