SpringBoot+Vue+Element-ui前后端分离(增删改查)(⼋)
1. properties配置
server.port=8080
pe=com.alibaba.druid.pool.DruidDataSource
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.sql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/db01?characterEncoding=utf8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=tr ue
#扫描xml
mybatis.mapper-locations= classpath:mapper/*.xml
#分页
pagehelper.helperDialect=mysql
pagehelper.supportMethodsArguments=true
pagehelper.pageSizeZero=true
pagehelper.params=countSql
2. l依赖
<!--web依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--mysql依赖-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
springboot结构<!--mybatis依赖-->
<dependency>
<groupId&batis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>
<!--druid连接池-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.21</version>
</dependency>
<!--加⼊分页依赖-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
3. 项⽬结构
4. 实体类
public class Dept {
private Integer deptno;
private String dname;
private String loc;
@Override
public String toString(){
return"Dept{"+
"deptno="+ deptno +
", dname='"+ dname +'\''+
", loc='"+ loc +'\''+
'}';
}
public Integer getDeptno(){
return deptno;
}
public void setDeptno(Integer deptno){
this.deptno = deptno;
}
public String getDname(){
return dname;
}
public void setDname(String dname){
this.dname = dname;
}
public String getLoc(){
return loc;
}
public void setLoc(String loc){
this.loc = loc;
}
}
5. mapper接⼝
@Mapper
public interface DeptMapper {
int addDept(Dept dept);
int deleteById(Integer deptno);
int updateDeptById(Dept dept);
List<Dept>findAllDept(@Param("keywords") String keywords); }
6. service
public interface DeptService {
int addDept(Dept dept);
int deleteById(Integer id);
int updateDeptById(Dept dept);
List<Dept>findAllDept(String keywords);
}
7. serviceimpl
@Service
public class DeptServiceImpl implements DeptService {
@Autowired
private DeptMapper deptMapper;
@Override
public int addDept(Dept dept){
return deptMapper.addDept(dept);
}
@Override
public int deleteById(Integer id){
return deptMapper.deleteById(id);
}
@Override
public int updateDeptById(Dept dept){
return deptMapper.updateDeptById(dept);
}
@Override
public List<Dept>findAllDept(String keywords){
return deptMapper.findAllDept(keywords);
}
}
8. controller接⼝
@RestController
@RequestMapping("/dept")
public class DeptController {
@Autowired
private DeptService deptService;
/**
* 分页查询
* @param page
* @param size
* @param keywords
* @return
*/
@GetMapping("/")
public RespPageBean findAll(@RequestParam(defaultValue ="1") Integer page,@RequestParam(defaultValue ="10") Integer size, String keywords){ PageHelper.startPage(page, size);
List<Dept> list = deptService.findAllDept(keywords);
PageInfo pageInfo =new PageInfo(list);
RespPageBean bean =new RespPageBean();
bean.List());
bean.Total());
return bean;
}
/**
* 删除
* @param deptno
* @return
*/
@DeleteMapping("/{deptno}")
public RespBean deleteDept(@PathVariable Integer deptno){
if(deptService.deleteById(deptno)==1){
return RespBean.DELETE_SUCCESS;//删除成功
}
return RespBean.DELETE_ERROR;//删除失败
}
/**
* 添加
* @param dept
* @return
*/
@PostMapping("/")
public RespBean addDept(@RequestBody Dept dept){
if(deptService.addDept(dept)==1){
return RespBean.ADD_SUCCESS;//添加成功
}
return RespBean.ADD_ERROR;//添加失败
}
/**
* 更新
* @param dept
* @return
*/
@PutMapping("/")
public RespBean updateDept(@RequestBody Dept dept){
if(deptService.updateDeptById(dept)==1){
return RespBean.UPDATE_SUCCESS;//添加成功
}
return RespBean.UPDATE_ERROR;//添加失败
}
9. 还有两个帮助类
public class RespBean {
private Integer status;
private String msg;
private Object obj;
public RespBean(Integer status, String msg){
this.status = status;
this.msg = msg;
}
public static final RespBean DELETE_SUCCESS =new RespBean(200,"删除成功"); public static final RespBean DELETE_ERROR =new RespBean(-1,"删除失败");
public static final RespBean ADD_SUCCESS =new RespBean(200,"添加成功"); public static final RespBean ADD_ERROR =new RespBean(-1,"添加失败");
public static final RespBean UPDATE_SUCCESS =new RespBean(200,"更新成功"); public static final RespBean UPDATE_ERROR =new RespBean(-1,"更新失败");
public Integer getStatus(){
return status;
}
public void setStatus(Integer status){
this.status = status;
}
public String getMsg(){
return msg;
}
public void setMsg(String msg){
this.msg = msg;
}
public Object getObj(){
return obj;
}
public void setObj(Object obj){
this.obj = obj;
}
private RespBean(Integer status, String msg, Object obj){
this.status = status;
this.msg = msg;
this.obj = obj;
}
private RespBean(){
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论