天天看點

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測試通過,讀者可自行驗證

轉載于:https://www.cnblogs.com/bape/p/8905973.html