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 ServiceImplimplements IUserInfoService { }



