栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

spring-boot集成mysql,mybatis-plus

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

spring-boot集成mysql,mybatis-plus

1.pom.xml 引入依赖

 
        
            mysql
            mysql-connector-java
            8.0.27
        
        
            com.baomidou
            mybatis-plus-boot-starter
            3.4.3.4
        
   
            com.alibaba
            druid-spring-boot-starter
            1.2.8
        

2.配置文件新增数据库信息

spring:
  datasource:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://xxx.xxx.xx.xx:xxxx/database?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai
      username: xxxx
      password: xxxxxxxx

3.mybaits-plus需要的格式
1.controller

package com.github.binarywang.demo.wx.mp.controller;

import com.github.binarywang.demo.wx.mp.entity.ContactUser;
import com.github.binarywang.demo.wx.mp.service.IUserInfoService;
import com.github.binarywang.demo.wx.mp.utils.CommonUtils;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;


@AllArgsConstructor
@RestController
@RequestMapping(value ="/wx/test/")
public class TestController {
    private final IUserInfoService userInfoService;
    @GetMapping("/userInfo")
    public void userInfo() {
        ContactUser userInfo = new ContactUser();
         // TODO 获取不到微信号,暂时使用昵称代替
        userInfo.setCreateTime(new Date(Long.parseLong(String.valueOf(System.currentTimeMillis()))));
        userInfo.setGender(CommonUtils.getGender(null));
        userInfoService.save(userInfo);
     }
}

2.entity

package com.github.binarywang.demo.wx.mp.entity;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import javafx.beans.DefaultProperty;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
import org.springframework.format.annotation.DateTimeFormat;
import java.util.Date;

 

@Data
@TableName("xx表")
public class ContactUser {

    @TableId(type = IdType.ASSIGN_ID)
    private String id;
    
    private String name;
    
    private String gender;
    
    private String wechatNumber;
    
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    private Date createTime;
    
    private Date updateTime;
    
    private String source_way = "1";
    
    private String status = "1";

}

3.mapper

package com.github.binarywang.demo.wx.mp.mapper;

import com.baomidou.mybatisplus.core.mapper.baseMapper;
import com.github.binarywang.demo.wx.mp.entity.ContactUser;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface UserInfoMapper extends baseMapper {
}

4.service

package com.github.binarywang.demo.wx.mp.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.github.binarywang.demo.wx.mp.entity.ContactUser;


public interface IUserInfoService extends IService {
}

5.serviceImpl

package com.github.binarywang.demo.wx.mp.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.binarywang.demo.wx.mp.entity.ContactUser;
import com.github.binarywang.demo.wx.mp.mapper.UserInfoMapper;
import com.github.binarywang.demo.wx.mp.service.IUserInfoService;
import org.springframework.stereotype.Service;


@Service
public class UserInfoServiceImpl extends ServiceImpl implements IUserInfoService {
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/343028.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号