上篇我们讲解了Spring Boot Mybatis工程如何搭建以及关键配置文件配置
详见(https://blog.csdn.net/stronglyh/article/details/80951718)
今天我们主要讲Mybatis数据库操作,我们先来看一下目录结构
其中controller文件夹写和页面的交互
service写业务逻辑
mapper就是dao层,写数据库操作
model写实体类,和数据库映射的或者需要使用到的其他类
mapping写映射关系
下面我们开始写增删改查
==========================华丽的分割线==========================
首先我们要启动程序,可能会出各种错,没关系,我们先给启动主程序进行配置
我们看看SmybatisApplication.java的内容
package com.example.smybatis;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@MapperScan("com.example.smybatis.mapper")
public class SmybatisApplication {
public static void main(String[] args) {
SpringApplication.run(SmybatisApplication.class, args);
}
}
如上,我们需要添加MapperScan标签
代码如下
controller:PersonC
package com.example.smybatis.controller;
import com.example.smybatis.model.Person;
import com.example.smybatis.service.PersonS;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@RestController
public class PersonC {
@Resource
private PersonS personS;
//增加
@PostMapping(value="/person")
public int savePerson(@RequestParam("id") Integer id,@RequestParam("age") Integer age,@RequestParam("name") String name){
return personS.savePerson(id,age,name);
}
//访问
@GetMapping(value="/person/{id}")
public Person getPerson(@PathVariable("id") Integer id){
return personS.getPerson(id);
}
//修改
@PutMapping(value="/person/{id}")
public int updatePerson(@PathVariable("id") Integer id,@RequestParam("age") Integer age,@RequestParam("name") String name){
return personS.updatePerson(id,age,name);
}
//删除
@DeleteMapping(value="/person/{id}")
public int deletePerson(@PathVariable("id")Integer id){
return personS.deletePerson(id);
}
}
service:PersonS
package com.example.smybatis.service;
import com.example.smybatis.mapper.PersonMapper;
import com.example.smybatis.model.Person;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@Service
public class PersonS {
@Resource
private PersonMapper personM;
public Person getPerson(Integer id){
return personM.selectByPrimaryKey(id);
}
public int savePerson(Integer id,Integer age,String name){
Person p = new Person();
p.setId(id);
p.setAge(age);
p.setName(name);
return personM.insert(p);
}
public int updatePerson(Integer id,Integer age,String name){
Person p = personM.selectByPrimaryKey(id);
p.setAge(age);
p.setName(name);
return personM.updateByPrimaryKey(p);
}
public int deletePerson(Integer id){
return personM.deleteByPrimaryKey(id);
}
}
dao:PersonMapper
package com.example.smybatis.mapper;
import com.example.smybatis.model.Person;
public interface PersonMapper {
int deleteByPrimaryKey(Integer id);
int insert(Person record);
int insertSelective(Person record);
Person selectByPrimaryKey(Integer id);
int updateByPrimaryKeySelective(Person record);
int updateByPrimaryKey(Person record);
}
一:新增操作
我们用postman执行下post请求
执行成功,我们看一下数据库
ok,数据入库了
二:查询操作
下面我们执行ge请求获取刚才的数据
http://localhost:1234/person/1
三)修改操作
我们看一下数据库
符合预期
四)删除操作
我们再去数据库表中看下id为1的数据是否存在
至此,我们Spring Boot+Mybatis增删改查写完了,欢迎留言沟通