06-springboot整合mybatis

引入依赖

在上一节基础上添加mybatis依赖:



    org.mybatis.spring.boot
    mybatis-spring-boot-starter
    1.3.0

DAO

@Mapper
public interface AccountMapper {

    @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(int id);

    @Select("select id, name as name, money as money from account where id = #{id}")
    Account findAccount(@Param("id") int id);

    @Select("select id, name as name, money as money from account")
    List findAccountList();
}

Service

Service
public class MybatisAccountService {
    @Autowired
    private AccountMapper accountMapper;

    public int add(String name, double money) {
        return accountMapper.add(name, money);
    }
    public int update(String name, double money, int id) {
        return accountMapper.update(name, money, id);
    }
    public int delete(int id) {
        return accountMapper.delete(id);
    }
    public Account findAccount(int id) {
        return accountMapper.findAccount(id);
    }
    public List findAccountList() {
        return accountMapper.findAccountList();
    }
}

Controller

@RestController
@RequestMapping("/mybatisAccount")
public class MybatisAccountController {

    @Autowired
    MybatisAccountService mybatisAccountService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public List getAccounts() {
        return mybatisAccountService.findAccountList();
    }

    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    public Account getAccountById(@PathVariable("id") int id) {
        return mybatisAccountService.findAccount(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) {
        int t= mybatisAccountService.update(name,money,id);
        if(t==1) {
            return "success";
        }else {
            return "fail";
        }

    }

    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    public String delete(@PathVariable(value = "id")int id) {
        int t= mybatisAccountService.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) {

       int t= mybatisAccountService.add(name,money);
       if(t==1) {
           return "success";
       }else {
           return "fail";
       }

    }

}

参考:http://blog.csdn.net/forezp/article/details/70768477

你可能感兴趣的:(06-springboot整合mybatis)