赞
踩
首先实现一个表的增加删除查询操作:
数据库中bm表(部门)
pom.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.7.14</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.example</groupId>
- <artifactId>demomvc</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <name>demomvc</name>
- <description>demomvc</description>
- <properties>
- <java.version>1.8</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
-
- <!-- thymeleaf依赖-->
- <dependency>
- <groupId>org.thymeleaf</groupId>
- <artifactId>thymeleaf-spring5</artifactId>
- </dependency>
- <!-- mysql依赖-->
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <version>5.1.43</version>
- </dependency>
- <!-- jdbc依赖-->
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-jdbc</artifactId>
- </dependency>
- <!-- mybatis依赖-->
- <dependency>
- <groupId>org.mybatis.spring.boot</groupId>
- <artifactId>mybatis-spring-boot-starter</artifactId>
- <version>2.1.0</version>
- </dependency>
-
-
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-devtools</artifactId>
- <scope>runtime</scope>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <optional>true</optional>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-test</artifactId>
- <scope>test</scope>
- </dependency>
- <dependency>
- <groupId>log4j</groupId>
- <artifactId>log4j</artifactId>
- <version>1.2.17</version>
- </dependency>
- </dependencies>
-
- <build>
- <plugins>
- <plugin>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-maven-plugin</artifactId>
- <configuration>
- <excludes>
- <exclude>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- </exclude>
- </excludes>
- </configuration>
- </plugin>
- </plugins>
- </build>
-
- </project>
yml文件配置
spring: server: port:9000 thymeleaf: mode: HTML cache: false datasource: driver-class-name: com.mysql.jdbc.Driver username: root password: 123456 url: jdbc:mysql://localhost:3306/boom?useUnicode=true&characterEncoding=utf-8&useSSL=false mybatis: mapper-locations: classpath:mapping/*.xml type-aliases-package: com.example.demo.pojo configuration: map-underscore-to-camel-case: true logging: level: com: example: demomvc: mapper : debug
Result统一响应
- package com.example.demomvc.pojo;
-
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Result {
- private int code;
- private String msg;
- private Object data;
-
- public static Result success(Object data){
- return new Result(1,"success",data);
- }
- public static Result success(){
- return new Result(1,"success",null);
- }
- public static Result error(String msg){
- return new Result(0,msg,null);
- }
-
- @Override
- public String toString() {
- return "Result{"+
- "code="+code+
- ",msg="+msg+'\''+
- ",data="+data+
- "}";
- }
- }
-
Bm(pojo类)
- package com.example.demomvc.pojo;
-
- import lombok.AllArgsConstructor;
- import lombok.Data;
- import lombok.NoArgsConstructor;
-
- import java.time.LocalDateTime;
-
- @Data
- @AllArgsConstructor
- @NoArgsConstructor
- public class Bm {
- private int bmid;
- private String bmname;
- private LocalDateTime creattime;
- private LocalDateTime updatetime;
- }
BmMapper
- package com.example.demomvc.mapper;
-
- import com.example.demomvc.pojo.Bm;
- import org.apache.ibatis.annotations.Delete;
- import org.apache.ibatis.annotations.Insert;
- import org.apache.ibatis.annotations.Mapper;
- import org.apache.ibatis.annotations.Select;
-
- import java.util.List;
-
- @Mapper
- public interface BmMapper {
-
- /*查询全部部门数据*/
- @Select("select * from bm")
- List<Bm> list();
-
- /*根据部门id删除信息*/
- @Delete("delete from bm where bmid=#{id}")
- void deleteById(int id);
-
- /*新增部门*/
- @Insert("insert into bm(bmid,bmname,creattime,updatetime) values(#{id},#{name},#{creattime},#{updatetime})")
- void setbm(Bm bm);
- }
BmService接口
- package com.example.demomvc.service;
-
-
- import com.example.demomvc.pojo.Bm;
-
- import java.util.List;
-
- public interface BmService {
-
- //查询全部部门数据
- List<Bm> list();
-
- void delete(int id);
-
- void setbm(Bm bm);
- }
BmServiceImp
- package com.example.demomvc.service.impl;
-
- import com.example.demomvc.mapper.BmMapper;
- import com.example.demomvc.pojo.Bm;
- import com.example.demomvc.service.BmService;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
-
- import java.time.LocalDateTime;
- import java.util.List;
-
- @Service
- public class BmServiceImpl implements BmService {
-
- @Autowired
- private BmMapper bmMapper;
-
- @Override
- public List<Bm> list() {
- return bmMapper.list();
- }
-
- @Override
- public void delete(int id) {
- bmMapper.deleteById(id);
- }
-
- @Override
- public void setbm(Bm bm) {
- bm.setCreattime(LocalDateTime.now());
- bm.setUpdatetime(LocalDateTime.now());
- bmMapper.setbm(bm);
- }
- }
BmController
- package com.example.demomvc.controller;
-
- import com.example.demomvc.pojo.Bm;
- import com.example.demomvc.pojo.Result;
- import com.example.demomvc.service.BmService;
- import lombok.extern.slf4j.Slf4j;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.*;
-
- import java.util.List;
- import java.util.logging.Logger;
-
- @Slf4j //日志有关log
- @RestController
- public class BmController {
-
- //private static Logger log= (Logger) LoggerFactory.getLogger(BmController.class);
- @Autowired
- private BmService bmService;
-
- @RequestMapping(value = "/bm",method = RequestMethod.GET)
- public Result list(){
-
- log.info("查询全部部门数据");
- List<Bm> bmList=bmService.list();
- return Result.success(bmList);
- }
-
- /*删除部门*/
- @RequestMapping("deletebm/{id}")
- public Result delete(@PathVariable int id){
- log.info("根据id删除部门:",id);
- //调用service,删除部门
- bmService.delete(id);
- return Result.success();
- }
- /*新增部门*/
- @RequestMapping("setbm")
- public Result setBm(@RequestBody Bm bm){
- log.info("新增部门:",bm);
- //调用service,新增部门
- bmService.setbm(bm);
- return Result.success();
- }
-
-
- }
ok~现在是访问实现!!!
这里是把id是9的删了,又select了一下
查看数据库也没有了(对比一下
结束啦~道阻且长呢
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。