Spring boot 入门三:spring boot 整合mybatis 实现CRUD操作


开发环境延续上一节的开发环境这里不再做介绍

添加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测试通过,读者可自行验证

猜你喜欢

转载自www.cnblogs.com/bape/p/8905973.html
今日推荐