·1. 模块创建
创建一个项目,创建时勾选springweb和mysql driver,
项目创建好后导入mybatis-plus和druid
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.3</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.6</version>
</dependency>
配置好端口等
2. 实体类开发
本处会使用lombok(一个java类库,提供一组注解,简化pojo实体类开发)
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
实体类:
@Data//lombok提供的其中包括get set toString方法等,lombok还提供其他方法具体自行查看
public class Book {
private Integer id;
private String type;
private String name;
private String description;
}
3. 数据层开发
基础功能
技术方案:
- MyBatisPlus
- Druid
配置:
spring:
datasource:
druid:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/jdbcstudy?serverTimezone=GMT
username: root
password: 123456
dao层:
@Repository
@Mapper
public interface BookDao extends BaseMapper<Book> {
}
测试:
@SpringBootTest
class DemoApplicationTests {
@Autowired
private BookDao bookDao;
@Test
void testGetById() {
System.out.println(bookDao.selectById(1));
}
@Test
void testUpDate() {
Book book = new Book();
book.setId(4);
book.setType("测试数据aaa");
book.setDescription("测试数据123");
book.setName("测试数据123");
bookDao.updateById(book);
}
@Test
void testSave() {
Book book = new Book();
book.setType("测试数据123");
book.setDescription("测试数据123");
book.setName("测试数据123");
bookDao.insert(book);
}
@Test
void testDelete() {
bookDao.deleteById(4);
}
@Test
void testGetAll() {
System.out.println(bookDao.selectList(null));
}
}
如果想看到mybait-plus的运行日志不要每次只有结果:
在springboot的配置文件中的mybait-plus配置项中添加:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
分页查询功能
// 分页查询
@Test
void testGetPage() {
// 参数一:当前数据,参数二:每一页大小
IPage iPage = new Page(1,2);
bookDao.selectPage(iPage,null);
}
除此之外还需要一个MybatisPlus分页拦截器:
//表示是一个配置需要加载
@Configuration
public class MPConfig {
// 实例化拦截器
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor(){
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// PaginationInnerInterceptor()是用来分页的拦截器
interceptor.addInnerInterceptor(new PaginationInnerInterceptor());
return interceptor;
}
}
按条件查询
@Test
void testGetBy() {
// 方法一:
String name = "Spring";
QueryWrapper<Book> qw = new QueryWrapper<>();
// 查询name like %spring%的值,第一个参数是条件相当于if
qw.like(name != null,"name",name);
bookDao.selectList(qw);
// 方法二:(推荐)
LambdaQueryWrapper<Book> lqw = new LambdaQueryWrapper<Book>();
lqw.like(name != null,Book::getName,"Spring");
bookDao.selectList(lqw);
}
4. 业务层开发
普通业务层开发
业务层接口:
public interface BookService {
Boolean save(Book book);
Boolean update(Book book);
Boolean delete(Integer id);
Book getByID(Integer id);
List<Book> getAll();
}
业务层实现类:
@Service
public class BookServiceImpl implements BookService {
@Autowired
private BookDao bookDao;
@Override
public Boolean save(Book book) {
// bookDao.insert(book)返回值为影响的行数,
return bookDao.insert(book) >0;
}
@Override
public Boolean update(Book book) {
return bookDao.updateById(book)>0;
}
@Override
public Boolean delete(Integer id) {
return bookDao.deleteById(id)>0;
}
@Override
public Book getByID(Integer id) {
return bookDao.selectById(id);
}
@Override
public List<Book> getAll() {
return bookDao.selectList(null);
}
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
MyBatisPlus实现业务层的快速开发
业务层接口:
public interface IBookService extends IService<Book> {
}
业务层实现类:
//mybait-plus没提供的方法也可以自己编辑添加
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
}
测试:
//这些方法如getById(1)都是自带的,其他没列出来自行查看
@Autowired
private IBookService bookService;
@Test
void testGerById(){
System.out.println(bookService.getById(1));
}
@Test
void testUpDate() {
Book book = new Book();
bookService.updateById(book)
}
@Test
void testDelete() {
bookService.removeById(1);
}
5. 表现层开发
- 基于Restful进行表现层开发
- 使用Postman测试表现层接口功能
表现层开发
表现层功能类:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
// 下面的方法都是mybaitplus提供的
//@RequestBody表示参数在请求体
//@PathVariable表示参数在url路径
@GetMapping
public List<Book> getAll(){
return bookService.list();
}
@PostMapping
public Boolean save(@RequestBody Book book){
return bookService.save(book);
}
@PutMapping
public Boolean update(@RequestBody Book book){
return bookService.updateById(book);
}
@DeleteMapping("{id}")
public Boolean update(@PathVariable Integer id){
return bookService.removeById(id);
}
@GetMapping("{id}")
public Book getById(@PathVariable Integer id){
return bookService.getById(id);
}
//分页 由于mybaitplus中的分页方法不太方便,所以本处我们是自己写的方法具体见下
@GetMapping("{currentPage}/{pageSize}")
public IPage<Book> getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return bookService.getPage(currentPage,pageSize);
}
}
//分页方法
@Service
public class IBookServiceImpl extends ServiceImpl<BookDao, Book> implements IBookService {
@Autowired
private BookDao bookDao;
@Override
public IPage<Book> getPage(int currentPage, int pageSize) {
IPage page = new Page(currentPage,pageSize);
bookDao.selectPage(page,null);
return page;
}
}
表现层消息格式统一
因为之前我们每个消息传递数据的格式都不一致很麻烦,所以要对其进行统一
所以我们要改成这种统一的格式:
具体方法就是设计一个表现层的模型类:
@Data //包含get、set tostring等方法
public class R {
private Boolean flag;
private Object data;
protected R(){}
public R(Boolean flag, Object data) {
this.flag = flag;
this.data = data;
}
public R(Boolean flag ){
this.flag=flag;
}
}
之后修改我们之前的表现层功能类:
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
// 下面的方法都是mybaitplus提供的
//@RequestBody表示参数在请求体
//@PathVariable表示参数在url路径
@GetMapping
public R getAll(){
return new R(true,bookService.list());
}
@PostMapping
public R save(@RequestBody Book book){
return new R(bookService.save(book));
}
@PutMapping
public R update(@RequestBody Book book){
return new R(bookService.updateById(book));
}
@DeleteMapping("{id}")
public R update(@PathVariable Integer id){
return new R(bookService.removeById(id));
}
@GetMapping("{id}")
public R getById(@PathVariable Integer id){
return new R(true,bookService.getById(id));
}
@GetMapping("{currentPage}/{pageSize}")
public R getPage(@PathVariable int currentPage,@PathVariable int pageSize){
return new R(true,bookService.getPage(currentPage,pageSize));
}
}
之后就是一些前端的操作,就不再多写了
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/195234.html