整合mybatis
引入依来<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.12.RELEASE</version><!--换成稳定版本--> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.lr</groupId> <artifactId>springboot-ssm</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-ssm</name> <description>springboot-ssm</description> <properties> <java.version>8</java.version><!--把jdk改为8--> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- mysql的驱动依赖--> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <!--mybatis和springboot整合的jar--> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>2.3.1</version><!--也要与springboot依赖对应--> </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> </dependencies>
配置文件
spring.application.name=springboot-ssm #数据源信息 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/qy174?serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root #配置mybatis映射文件的路径 mybatis.mapper-locations=classpath:mapper/*.xml
实体类
dao层
映射文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.lr.springbootssm.dao.FoodDao"> <insert id="addFood"> insert into food values (null,#{name},#{price},#{des}) </insert> <update id="update"> update food set name=#{name},price=#{price},des=#{des} where id=#{id} </update> <delete id="delete"> delete from food where id=#{id} </delete> <select id="selectById" resultType="com.lr.springbootssm.entity.Food"> select*from food where id=#{id} </select> <select id="selectAll" resultType="com.lr.springbootssm.entity.Food"> select*from food </select> </mapper>
vo
@Data @NoArgsConstructor @AllArgsConstructor public class R { private Integer code; private String msg; private Object data; }
service
public interface FoodService { R addFood(Food food); R update(Food food); R delete(Integer id); R selectBId(Integer id); R selectAll(Food food); }
@Service public class FoodServiceImpl implements FoodService { @Autowired private FoodDao foodDao; @Override public R addFood(Food food) { int i = foodDao.addFood(food); if (i>0){ return new R(200,"添加成功",null); } return new R(500,"添加失败",null); } @Override public R update(Food food) { int update = foodDao.update(food); if (update>0){ return new R(200,"修改成功",null); } return new R(500,"修改失败",null); } @Override public R delete(Integer id) { int delete = foodDao.delete(id); if (delete>0){ return new R(200,"删除成功",null); } return new R(500,"删除失败",null); } @Override public R selectBId(Integer id) { Food food = foodDao.selectById(id); if (food!=null){ return new R(200,"查询成功",food); } return new R(500,"查询失败",food); } @Override public R selectAll(Food food) { List<Food> foods = foodDao.selectAll(); if (foods!=null){ return new R(200,"查询成功",foods); } return new R(500,"查询成功",foods); } }
controller
@RestController @RequestMapping("/food") public class FoodController { @Autowired private FoodService foodService; @PostMapping("/addfood") public R addfood(@RequestBody Food food){ return foodService.addFood(food); } @PutMapping("/update") public R update(@RequestBody Food food){ return foodService.update(food); } @DeleteMapping("/delete") public R delete(Integer id){ return foodService.delete(id); } @GetMapping("selectbyid") public R selectbyid(Integer id){ return foodService.selectBId(id); } @GetMapping("/selectall") public R selectall(){ return foodService.selectAll(); } }