Spring Boot Starter III: Spring boot integration mybatis for CRUD operations

Source: Internet
Author: User


Development environment Continuation The development environment of the previous section no more introductions here.

Add MyBatis Dependency

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.1</version>
</dependency>

DAO layer Interface (here is the implementation of database operations directly through annotations, not the DAO layer implementation)

@Mapper
@Repository
Public interface Iaccountmybatisdao {
@Insert ("Insert into account (Name,money) VALUES (#{name},#{money})")
int Add (@Param ("name") String name, @Param ("money") of 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 Layer Interface

Public interface Iaccountmybatisservice {

int Add (account account);

int update (account account);

int delete (int id);

Account Findaccountbyid (int id);

List<account> findaccountlist ();
}

Service Layer Implementation
@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 ();
}
}

Controller

@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) {

}

@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";
}
}
}
The above additions and deletions are passed through the postman test, the reader can verify their own

Spring Boot Starter III: Spring boot integration mybatis for CRUD operations

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.