springboot整合mybatis遇到得坑

SpringBoot整合Mybatis超详细流程

(亲测解决)Unsatisfied dependency expressed through bean property ‘sqlSessionFactory’

java.sql.SQLException: The server time zone value 的解决办法

springboot整合mybatis踩过的坑

SpringBoot整合Mybatis

springboot整合mybatis(详解)

MyBatis-Plus(二)设置实体类对应的表名、字段名

spring.datasource.url=jdbc:mysql://localhost:3306/user_manager?serverTimezone=UTC
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
      <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>

一、设置关联的表名
1、默认情况下,如果数据库表是使用标准的下划线命名,并且能对应上实体类的类名,我们就不需要特别去手动匹配。比如有张 user_info 表,那么会自动匹配下面这个实体类:

@Data
public class UserInfo {
    private Integer id;
    private String userName;
    private String passWord;
}

在这里插入图片描述

使用TK框架中selectByPrimaryKey(Object key),需要注意要在entity里注明哪个字段是主键,否则会不知道哪个是PrimaryKey会随机一个字段就报错。

通用mapper的查询,selectByPrimaryKey、select、selectByExample等

package com.leyou.controller;

import com.leyou.pojo.TbUser;
import com.leyou.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class HelloController {

    private final static Logger logger = LoggerFactory.getLogger(HelloController.class);

   @Autowired
    private UserService userService;

    @GetMapping("hello")
    public String helloo(){
        logger.trace("我是trace");
        logger.debug("我是debug");
        logger.info("我是info");
        logger.warn("我是warn");
        logger.error("我是error");
        return "hello,springboot!";
    }
   @GetMapping("/hello1")
    public TbUser hello() {
        TbUser user = this.userService.queryById(1L);
        return user;
    }
    @GetMapping("/hello2")
    public List<TbUser> hello2() {
        TbUser tbUser = new TbUser();
        tbUser.setId(4L);
        List<TbUser> listUser = this.userService.queryByObj(tbUser);
        return listUser;
    }
}

package com.leyou.pojo;

import javax.persistence.Id;
import java.util.Date;

public class TbUser {
    @Id
    private Long id;

    // 用户名
    private String userName;

    // 密码
    private String password;

    // 姓名
    private String name;

    // 年龄
    private Integer age;

    // 性别,1男性,2女性
    private Integer sex;

    // 出生日期
    private Date birthday;

    // 创建时间
    private Date created;

    // 更新时间
    private Date updated;

    public Long getId() {
        return id;
    }

    public void setId(Long 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;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public Integer getSex() {
        return sex;
    }

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

    public Date getBirthday() {
        return birthday;
    }

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

    public Date getCreated() {
        return created;
    }

    public void setCreated(Date created) {
        this.created = created;
    }

    public Date getUpdated() {
        return updated;
    }

    public void setUpdated(Date updated) {
        this.updated = updated;
    }

    @Override
    public String toString() {
        return "User [id=" + id + ", userName=" + userName + ", password=" + password + ", name=" + name
                + ", age=" + age + ", sex=" + sex + ", birthday=" + birthday + ", created=" + created
                + ", updated=" + updated + "]";
    }
}

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值