打开idea开始上号啦
pom.xml 文件 , <dependency>放入</dependencies>内。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.23</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<!-- 将对象 转化为JSON格式-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
刷新依赖
resoucrces创建application.yml 注意位置不能错 更不能打错 不然起不到配置的作用。
配置填写信息
server:
port: 9527
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://数据库地址:3306/数据库名称?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: 用户名
password:数据库密码
mybatis-plus:
configuration:
map-underscore-to-camel-case: true
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
global-config:
db-config:
id-type: ASSIGN_ID
type-aliases-package: com.example.ball.entity
数据库连接:(连接数据库是为了使用lombok 方便编写代码)
连接成功后 运行一下启动类
创建文件夹:
创建对应的类
Result类:
package com.example.ball.common;
import lombok.Data;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;
@Data
public class Result<T> implements Serializable {
private Integer code; //编码:1成功,0和其它数字为失败
private String msg; //错误信息
private T data; //数据
private Map map = new HashMap(); //动态数据
public static <T> Result<T> success(T object) {
Result<T> res = new Result<T>();
res.data = object;
res.code = 1;
return res;
}
public static <T> Result<T> error(String msg) {
Result res = new Result();
res.msg = msg;
res.code = 0;
return res;
}
public Result<T> add(String key, Object value) {
this.map.put(key, value);
return this;
}
}
查看数据库字段 对应类型编写实体
entity类
package com.example.ball.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.util.Date;
@Data
@TableName("team")
public class Ball {
private Integer id;
private String LeftName;
private String RightName;
private Integer LeftScore;
private Integer RightScore;
private Date CreateDate;
}
mapper接口:
package com.example.ball.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.ball.entity.Ball;
import org.apache.ibatis.annotations.Mapper;
@Mapper
public interface BallMapper extends BaseMapper<Ball> {
}
service接口:
package com.example.ball.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.ball.entity.Ball;
public interface BallService extends IService<Ball> {
}
impl类:
package com.example.ball.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.ball.entity.Ball;
import com.example.ball.mapper.BallMapper;
import com.example.ball.service.BallService;
import org.springframework.stereotype.Service;
@Service
public class BallServiceImpl extends ServiceImpl<BallMapper, Ball> implements BallService {
}
controller类:
package com.example.ball.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.ball.common.Result;
import com.example.ball.entity.Ball;
import com.example.ball.service.BallService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@CrossOrigin // 跨域问题
@RequestMapping("yz")
@RestController
public class BallController {
@Autowired
private BallService ballService;
@RequestMapping("/get")
public Result<Ball> get(@RequestParam String id){
LambdaQueryWrapper<Ball> queryWrapper= new LambdaQueryWrapper<>();
queryWrapper.eq(Ball::getId,id);
Ball ball = ballService.getById(id);
return com.example.ball.common.Result.success(ball);
}
}
查看结果发送请求 :http://localhost:9527/yz/get?id=2
页面查看是否查询成功。