SpringBoot集成Mybatis

1、创建配置文件,并添加数据库连接信息

  application.properties形式添加数据库连接信息

spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/jeff
spring.datasource.username=root
spring.datasource.password=mysql

  application.yml形式添加数据库连接信息

#配置数据库
spring: 
  datasource: 
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/jeff
    username: root
    password: mysql

2、在pom.xml中引入Mybatis的jar包

        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.1</version>
        </dependency>
        
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>

3、创建数据库(jeff),创建用户表(sys_user),并添加数据

4、创建用户实体类(User)

package com.jeff.entity;

import java.util.Date;

import com.fasterxml.jackson.annotation.JsonFormat;

public class User {

    // 主键
    private long id;

    // 登陆名
    private String loginName;

    // 密码
    private String password;

    //
    private String salt;

    // 性别(0:保密;1:男;2:女)
    private int sex;

    // 状态(0:有效;1:无效)
    private int status;

    // 真实姓名
    private String name;

    // 昵称
    private String nickName;

    // 手机号
    private String phone;

    // 邮箱
    private String email;

    // 生日
    @JsonFormat(pattern = "yyyy-MM-dd")
    private Date birthday;

    // 用户头像
    private String headimgUrl;

    // 创建时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    // 创建人
    private String createName;

    // 修改时间
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date modifyTime;

    // 修改人
    private String modifyName;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getLoginName() {
        return loginName;
    }

    public void setLoginName(String loginName) {
        this.loginName = loginName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getSalt() {
        return salt;
    }

    public void setSalt(String salt) {
        this.salt = salt;
    }

    public int getSex() {
        return sex;
    }

    public void setSex(int sex) {
        this.sex = sex;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
    }

    public String getName() {
        return name;
    }

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

    public String getNickName() {
        return nickName;
    }

    public void setNickName(String nickName) {
        this.nickName = nickName;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public String getHeadimgUrl() {
        return headimgUrl;
    }

    public void setHeadimgUrl(String headimgUrl) {
        this.headimgUrl = headimgUrl;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public String getCreateName() {
        return createName;
    }

    public void setCreateName(String createName) {
        this.createName = createName;
    }

    public Date getModifyTime() {
        return modifyTime;
    }

    public void setModifyTime(Date modifyTime) {
        this.modifyTime = modifyTime;
    }

    public String getModifyName() {
        return modifyName;
    }

    public void setModifyName(String modifyName) {
        this.modifyName = modifyName;
    }

}

5、创建UserMapper

package com.jeff.mapper;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import com.jeff.entity.User;

@Mapper
public interface UserMapper {
    
    @Select("select * from sys_user where id=#{id}")
    User getUserById(@Param("id") Long id);

}

6、创建UserService

package com.jeff.service;

import com.jeff.entity.User;

public interface UserService {

    User getUserById(Long id);

}

7、创建UserServiceImpl

package com.jeff.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jeff.entity.User;
import com.jeff.mapper.UserMapper;
import com.jeff.service.UserService;

@Service
public class UserServiceImpl implements UserService {
    
    @Autowired
    private UserMapper mapper;

    @Override
    public User getUserById(Long id) {
        
        return mapper.getUserById(id);
    }

}

8、创建UserController并添加getUserById方法

package com.jeff.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.jeff.entity.User;
import com.jeff.service.UserService;

@RestController
@RequestMapping("user")
public class UserController {
    
    @Autowired
    private UserService service;

    @RequestMapping("getUserById")
    public User getUserById(Long id) {
        
        return service.getUserById(id);
    }

}

7、测试getUserById方法 http://localhost:8080/user/getUserById?id=1

posted @ 2018-11-26 22:31  知识追求者  阅读(176)  评论(0编辑  收藏  举报