File->New->Project->Maven->。。。->完成。
目录结构:
DeptController.java
package com.strex.controller;
import com.strex.entity.DeptEntity;
import com.strex.service.DeptService;
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 DeptController {
@Autowired
private DeptService deptService;
@GetMapping("/insert")
public void insert() {
deptService.insert();
}
// 单条件查询
// http://localhost:8080/find?id=1
@GetMapping("/findOne")
public DeptEntity findOne(Integer id) {
return deptService.selectById(id);
}
@GetMapping("/getList")
public List getList(DeptEntity deptEntity) {
return deptService.findList(deptEntity);
}
// http://localhost:8080/insertDept?deptno=23&dname=%E5%A4%96%E4%BA%A4%E9%83%A8&db_source=DB02
@GetMapping("/insertDept")
public String insertDept(DeptEntity deptEntity) {
return deptService.insertDept(deptEntity);
}
@GetMapping("/updateDept")
public String updateDept(DeptEntity deptEntity) {
return deptService.updateDept(deptEntity);
}
}
DeptEntity.java
package com.strex.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
//查询需要知道主键,指定表名
@Data
@TableName("dept")
public class DeptEntity {
//TableId表示主键 type设置自增
@TableId(value = "deptno",type = IdType.AUTO)
private Integer deptno;
private String dname;
private String db_source;
}
DeptMapper.java
package com.strex.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.strex.entity.DeptEntity; import org.apache.ibatis.annotations.Mapper; import org.springframework.stereotype.Repository; //这里只需要继承BaseMapper就可以自动实现增删改查,不需要写sql //Mapper注解为一个实体类 @Mapper @Repository public interface DeptMapper extends BaseMapper{ //单表增删改查不需要写sql // @Insert("INSERT INTO DB01.dept (dname,db_source) VALUES ('[ˈteilə]','n. 裁缝师'), ('[ʌnˈkɔnʃəs]','adj. 不省人事的;失去知觉的')") // void insertOne(); }
DeptService.java
package com.strex.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strex.entity.DeptEntity;
import com.strex.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.util.List;
@Component
public class DeptService {
@Autowired
private DeptMapper deptMapper;
public void insert() {
System.out.println("调用service成功");
// userMapper.insertOne();
}
public DeptEntity selectById(Integer id) {
DeptEntity deptEntity = deptMapper.selectById(id);
return deptEntity;
}
// 多条件查询
public List findList(DeptEntity deptEntity) {
QueryWrapper queryWrapper = new QueryWrapper<>();
String dname = deptEntity.getDname();
if (!StringUtils.isEmpty(dname)) {
// 上面的空判断可以不用,而是通过eq的第一个参数,condition boolen
queryWrapper.eq("dname", dname);
// queryWrapper.ge() 小于
// queryWrapper.orderBy()
// queryWrapper.gt() 大于
// queryWrapper.between() 介于。。。之间
}
// 上面的判断另一种写法
queryWrapper.eq(!StringUtils.isEmpty(deptEntity.getDb_source()), "db_source", deptEntity.getDb_source());
return deptMapper.selectList(queryWrapper);
}
public String insertDept(DeptEntity deptEntity) {
return deptMapper.insert(deptEntity) > 0 ? "success" : "fail";
}
public String updateDept(DeptEntity deptEntity) {
return deptMapper.updateById(deptEntity) > 0 ? "success" : "fail";
}
}
MyApplication.java
package com.strex;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class MyApplication {
public static void main(String[] args) {
//这里就只需要让工程运行起来就行了
SpringApplication.run(MyApplication.class);
}
}
application.yml
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
# spring.datasource.url=jdbc:mysql://107.182.184.210:3306/DB01?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
# spring.datasource.username='strex'
spring:
datasource:
password: 51**
username: 'st**'
url: jdbc:mysql://***IP***:3306/DB01?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&useSSL=false
logging:
level:
###打印mybatis日志,sql语句
com.strex.mapper: debug
3、运行



