This commit is contained in:
developServer 2024-08-03 01:46:28 +00:00
parent 6312c20547
commit 4871c0eee2
11 changed files with 169 additions and 8 deletions

View File

@ -0,0 +1,24 @@
package com.example.demo.controller;
import com.example.demo.entity.News;
import com.example.demo.service.NewsService;
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 NewsController {
private final NewsService newsService;
@Autowired
public NewsController(NewsService newsService) {
this.newsService = newsService;
}
@GetMapping("/news")
public List<News> getAllNews() {
return newsService.getAllNews();
}
}

View File

@ -0,0 +1,24 @@
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.service.UserService;
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 UserController {
private final UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/users")
public List<User> getAllUser() {
return userService.getAllUsers();
}
}

View File

@ -1,17 +1,32 @@
package com.example.demo.models; package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.*;
import lombok.Data; import lombok.Data;
import java.sql.Timestamp; import java.sql.Timestamp;
@Data @Data
@TableName("news")
public class News { public class News {
@TableId(value = "ID", type = IdType.AUTO)
private Integer id; private Integer id;
@TableField(value = "TITLE")
private String title; private String title;
@TableField(value = "EXCERPT")
private String excerpt; private String excerpt;
@TableField(value = "CREATE_TIME", fill = FieldFill.UPDATE)
private Timestamp create_time; private Timestamp create_time;
@TableField(value = "UPDATE_TIME", fill = FieldFill.INSERT)
private Timestamp update_time; private Timestamp update_time;
@TableField(value = "IS_DELETE")
private Boolean is_delete; private Boolean is_delete;
@TableField(value = "EXT")
private String ext; private String ext;
public News() { public News() {

View File

@ -0,0 +1,32 @@
package com.example.demo.entity;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import java.sql.Date;
@Data
@TableName("users")
public class User {
@TableId(value = "id", type = IdType.AUTO)
private Integer id;
@TableField(value = "user_account")
private String user_account;
@TableField(value = "user_name")
private String user_name;
@TableField(value = "password")
private String password;
@TableField(value = "email")
private String email;
@TableField(value = "mobile")
private String mobile;
@TableField(value = "status")
private String status;
@TableField(value = "create_time")
private Date create_time;
@TableField(value = "update_time")
private Date update_time;
}

View File

@ -1,7 +1,7 @@
package com.example.demo.mapper; package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.models.News; import com.example.demo.entity.News;
public interface newsMapper extends BaseMapper<News> { public interface NewsMapper extends BaseMapper<News> {
} }

View File

@ -0,0 +1,8 @@
package com.example.demo.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.User;
import org.mybatis.spring.annotation.MapperScan;
public interface UserMapper extends BaseMapper<User> {
}

View File

@ -0,0 +1,10 @@
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.News;
import java.util.List;
public interface NewsService extends IService<News> {
List<News> getAllNews();
News getNewsById(Integer id);
}

View File

@ -0,0 +1,9 @@
package com.example.demo.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.example.demo.entity.User;
import java.util.List;
public interface UserService extends IService<User> {
List<User> getAllUsers();
}

View File

@ -0,0 +1,22 @@
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.News;
import com.example.demo.mapper.NewsMapper;
import com.example.demo.service.NewsService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class NewsServiceImpl extends ServiceImpl<NewsMapper, News> implements NewsService {
@Override
public List<News> getAllNews() {
return list();
}
@Override
public News getNewsById(Integer id) {
return getById(id);
}
}

View File

@ -0,0 +1,18 @@
package com.example.demo.service.impl;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
@Override
public List<User> getAllUsers() {
return list();
}
}

View File

@ -1,13 +1,12 @@
package com.example.demo; package com.example.demo;
import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest; import com.baomidou.mybatisplus.test.autoconfigure.MybatisPlusTest;
import com.example.demo.mapper.newsMapper; import com.example.demo.mapper.NewsMapper;
import com.example.demo.models.News; import com.example.demo.entity.News;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan; import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase;
import org.springframework.test.annotation.Rollback;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
@ -17,14 +16,14 @@ import org.springframework.transaction.annotation.Transactional;
public class MybatisPlusTests { public class MybatisPlusTests {
@Autowired @Autowired
private newsMapper newsMapper; private NewsMapper newsMapper;
@Test @Test
@Transactional @Transactional
public void insert() { public void insert() {
News user = new News("test333", "test"); News user = new News("test333", "test");
int insert = newsMapper.insert(user); int insert = newsMapper.insert(user);
System.out.println("影响行数" + insert); System.out.println("影响行数" + insert);
System.out.println(user.getId()); System.out.println(user.getId());
} }
} }