Spring boot 入门四:spring boot 整合mybatis 实现CRUD操作
- 作者: 五速梦信息网
- 时间: 2026年04月04日 13:27
开发环境延续上一节的开发环境这里不再做介绍
添加mybatis依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>
DAO层接口(这里是直接通过注解实现数据库操作,不做DAO层实现)
@Mapper
@Repository
public interface IAccountMybatisDao {
@Insert("insert into account(name,money) values (#{name},#{money})")
int add(@Param("name") String name, @Param("money") double money);
@Update("update account set name=#{name},money=#{money} where id=${id}")
int update(@Param("name") String name,@Param("money") double money,@Param("id") int id);
@Delete("delete from account where id=#{id}")
int delete(@Param("id") int id);
@Select("select * from account where id=#{id}")
Account findAccount(@Param("id") int id);
@Select("select id,name as name,money as money from account")
List<Account> findAccountList();
}
Service层接口
public interface IAccountMybatisService {
int add(Account account);
int update(Account account);
int delete(int id);
Account findAccountById(int id);
List<Account> findAccountList();
}
service层实现
@Service
public class AccountMybatisServiceImpl implements IAccountMybatisService{
@Autowired
private IAccountMybatisDao accountMybatisDao;
@Override
public int add(Account account) {
return accountMybatisDao.add(account.getName(),account.getMoney());
}
@Override
public int update(Account account) {
return accountMybatisDao.update(account.getName(),account.getMoney(),account.getId());
}
@Override
public int delete(int id) {
return accountMybatisDao.delete(id);
}
@Override
public Account findAccountById(int id) {
return accountMybatisDao.findAccount(id);
}
@Override
public List<Account> findAccountList() {
return accountMybatisDao.findAccountList();
}
}
控制器
@RestController
@RequestMapping("/accountMybatis")
public class AccountMybatisController {
@Autowired
private IAccountMybatisService service;
@Autowired
private Account account;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public List<Account> getAccounts() {
return service.findAccountList();
}
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public Account getAccountById(@PathVariable("id") int id) {
return service.findAccountById(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name", required = true) String name,
@RequestParam(value = "money", required = true) double money) {
account.setId(id);
account.setMoney(money);
account.setName(name);
int t = service.update(account);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable(value = "id") int id) {
int t = service.delete(id);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
@RequestMapping(value = "", method = RequestMethod.POST)
public String postAccount(@RequestParam(value = "name") String name,
@RequestParam(value = "money") double money) {
account.setName(name);
account.setMoney(money);
int t = service.add(account);
if (t == 1) {
return "success";
} else {
return "fail";
}
}
}
以上增删改查都通过postman测试通过,读者可自行验证
相关文章
-
spring boot 使用redis 及redis工具类
spring boot 使用redis 及redis工具类
- 互联网
- 2026年04月04日
-
spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 spring boot 2.3.3)
spring boot:swagger3文档展示分页和分栏的列表数据(swagger 3.0.0 spring boot 2.3.3)
- 互联网
- 2026年04月04日
-
Spring Boot打war包和jar包的目录结构简单讲解
Spring Boot打war包和jar包的目录结构简单讲解
- 互联网
- 2026年04月04日
-
Spring Boot 配置文件加载位置及优先级
Spring Boot 配置文件加载位置及优先级
- 互联网
- 2026年04月04日
-
spring boot websocket stomp 实现广播通信和一对一通信聊天
spring boot websocket stomp 实现广播通信和一对一通信聊天
- 互联网
- 2026年04月04日
-
spring boot controller访问不到
spring boot controller访问不到
- 互联网
- 2026年04月04日






