使用IDEA快速构建![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3b23a09497c91aa9cf4d6fdc32442341.png#pic_center)
pom.xml
<!-- 引入Mybatis-plus依赖 -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.2.0</version>
</dependency>
<!-- 引入阿里巴巴druid -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.22</version>
</dependency>
yml
server:
port: 8001
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
username: root
password: root
主启动
@SpringBootApplication
@MapperScan("com.example.demo.dao")
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@ToString
@Accessors(chain = true)
@TableName(value = "test")
public class User {
@TableId(value = "ID",type = IdType.UUID)
private String id;
@TableField(value = "NAME")
private String name;
@TableField(value = "AGE")
private String age;
@TableField(value = "DEPT")
private String dept;
}
json封装体
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserResult<T>{
private int code;
private String message;
private T data;
public UserResult(int code, String message){
this(code,message,null);
}
}
dao
public interface UserDao extends BaseMapper<User> {
}
service
public interface UserService {
int create(User user);
User selectById(String id);
}
serviceImpl
@Service
public class UserServiceImpl implements UserService {
@Resource
UserDao userDao;
@Override
public int create(User user) {
return userDao.insert(user);
}
@Override
public User selectById(String id) {
return userDao.selectById(id);
}
}
controller
@RestController
@RequestMapping("/user")
public class UserController {
@Resource
UserService userService;
@PostMapping("/create")
public UserResult create(@RequestBody User user) {
int result = userService.create(user);
if (result>0){
return new UserResult(200,"保存成功",result);
}else {
return new UserResult(444,"保存失败",null);
}
}
@GetMapping("/selectById/{id}")
public UserResult selectById(@PathVariable("id") String id) {
User result = userService.selectById(id);
if (result!=null){
return new UserResult(200,"查询成功",result);
}else {
return new UserResult(444,"查询失败",null);
}
}
@PostMapping("/selectHello")
public String selectHello(){
return "Hello";
}
@GetMapping("/selectHelloWorld")
public String selectHelloWorld(){
return "Hello World";
}
}