很多新手看了网上一堆springboot 搭建的 云里雾里 还出现很多错误,特此给大家带来一篇非常简单的springboot基础骨架,此骨架已经完成了增删改查分页功能。当然我们这里没有采用聚合工程,原理都是相同的。
后续会在这个骨架基础上继续添加一些东西,帮助新手快速进行真正实战学习!
1.搭建一个springboot项目
(1)新建项目
(2)选择依赖 也可以后期直接pom文件添加
建完如下图
新建包 ,完成一套增删改查操作
如下图骨架包路径和文件 里面的entity包暂时用不到
yml基本配置
server:
port: 9000
servlet:
context-path: /permise
spring:
datasource:
url: jdbc:mysql://42.192.112.129:3306/hllhj?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC&useSSL=false
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password:xxxx
mybatis:
mapper-locations: classpath:mapper/*/*.xml,classpath*:mapper/*.xml
#打印sql语句
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
配置类
mybatisPlusConfig
package com.zyw.permission.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 可以解决MybatisPlus的IPage失效问题及查询全部报错问题
* @author zyw
* @date 2021/1/27 20:21
*/
@Configuration
public class mybatisPlusConfig {
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
工具类
PageConfig
package com.zyw.permission.util;
import com.zyw.permission.entity.PageData;
import java.util.Map;
/**
* @author zyw
* @date 2021/1/27 19:14
*/
public class PageConfig {
/**
* 默认分页数值
* @param pageData
* @return
*/
public static PageData defaultPageSize(PageData pageData){
Object pageNum = pageData.get("pageNum");
Object pageSize = pageData.get("pageSize");
if(pageNum==null){
pageData.put("pageNum",1);
}
if(pageSize==null){
pageData.put("pageSize",10);
}
return pageData;
}
/**
* 设置无分页
* @param pageData
* @return
*/
public static PageData maxPageSize(PageData pageData){
Object pageNum = pageData.get("pageNum");
Object pageSize = pageData.get("pageSize");
if(pageNum==null){
pageData.put("pageNum",1L);
}
if(pageSize==null){
pageData.put("pageSize",2000);
}
return pageData;
}
}
controller层
package com.zyw.permission.controller;
import com.zyw.permission.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.Map;
/**
* @author zyw
* @date 2021/1/27 11:49
*/
@CrossOrigin("*")
@Controller
public class TestController{
@Autowired
TestService testService;
@RequestMapping("/test")
@ResponseBody
public Object test(@RequestParam Map<String,Object> map){
return testService.list(map);
}
@RequestMapping("/test2")
@ResponseBody
public Object test2(@RequestParam Map<String,Object> map){
return testService.listTwo(map);
}
@PostMapping("/test3")
@ResponseBody
public Object insert(@RequestBody Map<String,Object> map){
return testService.insert(map);
}
@PostMapping("/test4")
@ResponseBody
public Object update(@RequestBody Map<String,Object> map){
return testService.update(map);
}
@PostMapping("/test5")
@ResponseBody
public Object deletebyIds(@RequestBody Map<String,Object> map){
return testService.deletebyIds(map);
}
}
TestMapper
package com.zyw.permission.mapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyw.permission.entity.PageData;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.stereotype.Repository;
import java.util.List;
import java.util.Map;
/**
* @author zyw
* @date 2021/1/27 11:50
*/
@Mapper
@Repository
public interface TestMapper {
/**
* 测试接口数据
* @param map
* @return
*/
public List<Map<String,Object>> testlist(Map<String,Object> map) ;
public IPage<Map<String, Object>> testOne(Page<Map<String, Object>> page, Map<String,Object> map) ;
public Integer insert(Map<String,Object> map);
public Integer update(Map<String,Object> map);
public Integer deletebyIds(Map<String,Object> map);
}
TestService
package com.zyw.permission.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageInfo;
import java.util.Map;
/**
* @author zyw
* @date 2021/1/27 14:48
*/
public interface TestService {
/**
* 分页查询数据 PageHelper.startPage(page,pageSize);
* PageInfo 包装结果集
* @return
*/
PageInfo<Map<String,Object>> list(Map<String,Object> map);
/**
* 分页查询数据(2)
* Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageNum,pageSize);
* IPage<Map<String,Object>> ipage = testMapper.testOne(page,map);
* return ipage
* @param map
* @return
*/
IPage<Map<String,Object>> listTwo(Map<String, Object> map);
/**
* 动态新增数据
* @param map
* @return
*/
Integer insert(Map<String, Object> map);
/**`
* 动态修改数据
* @param map
* @return
*/
Integer update(Map<String, Object> map);
/**
* 批量删除
* {"ids":"1,115"} 此格式传输
* @param map
* @return
*/
Integer deletebyIds(Map<String,Object> map);
}
TestServiceImpl
package com.zyw.permission.service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.permission.mapper.TestMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import java.util.Map;
/**
* @author zyw
* @date 2021/1/27 14:51
*/
@Service
public class TestServiceImpl implements TestService {
@Autowired
TestMapper testMapper;
/**
* 1.PageHelper 形式分页 紧跟着查询类别否则失效
* 或者原始写法: map.put("page", (page-1)*pageSize);
* map.put("pageSize", pageSize);
* SQL..... limit #{page},#{pageSize}
* @param map
* @return
*/
@Override
public PageInfo<Map<String, Object>> list(Map<String, Object> map) {
//PageHelper 紧跟着查询类别否则失效
//魔法值
int page = map.get("page")!=null?Integer.valueOf( map.get("page").toString()):0;
int pageSize = map.get("pageSize")!=null?Integer.valueOf( map.get("pageSize").toString()):0;
if(page!=0&&pageSize!=0){
PageHelper.startPage(page,pageSize);
}
List<Map<String,Object>> list = testMapper.testlist(map);
//结果进行包装
return new PageInfo<>(list);
}
@Override
public IPage<Map<String, Object>> listTwo(Map<String, Object> map) {
int pageNum = map.get("pageNum")!=null?Integer.valueOf( map.get("pageNum").toString()):1;
int pageSize = map.get("pageSize")!=null?Integer.valueOf( map.get("pageSize").toString()):-1;
Page<Map<String, Object>> page = new Page<Map<String, Object>>(pageNum,pageSize);
return testMapper.testOne(page,map);
}
@Transactional
@Override
public Integer deletebyIds(Map<String, Object> map) {
//两种形式 一种可以循环单删除 另一种直接sql批量删除 这里我们写直接sql批量删除
String[] ids =map.get("ids").toString().split(",");
String idss = "";
for (int i = 0; i < ids.length; i++) {
System.out.println(ids[i]);
if(i<ids.length-1){
idss+="\""+ids[i]+"\""+",";
}else {
idss+="\""+ids[i]+"\"";
}
}
map.put("idall",idss);
return testMapper.deletebyIds(map);
}
@Transactional
@Override
public Integer update(Map<String, Object> map) {
return testMapper.update(map);
}
@Transactional
@Override
public Integer insert(Map<String, Object> map) {
return testMapper.insert(map);
}
}
至此 最基本的 springboot 项目 骨架已经完成,后续springboot 整合其他东西都是在此基础上。