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