引入依赖
在pom文件引入依赖:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JDBC依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<!-- 连接类及连接池 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
<!-- mybatis依赖包 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
dao层
package com.xiami.dao;
import java.util.List;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import com.xiami.entity.MyLove;
@Mapper
public interface MyLoveMapper
{
@Insert("insert into myLove(name,age,sex) values (#{name},#{age},#{sex})")
int add(@Param("name") String name,@Param("age") String age,
@Param("sex") String sex);
@Update("update myLove set name = #{name}, age = #{age}, sex= #{sex} where id = #{id}")
int update(@Param("name") String name,@Param("age") String age,
@Param("sex") String sex, @Param("id") int id);
@Delete("delete from myLove where id = #{id}")
int delete(int id);
@Select("select name, sex, age from myLove where id = #{id}")
MyLove findMlove(@Param("id") int id);
@Select("select name, sex, age from myLove")
List<MyLove> findMyLoveList();
}
service层
package com.xiami.service;
import java.util.List;
import com.xiami.entity.MyLove;
public interface MyLoveMapperService {
public int add(String name, String sex, String age);
public int update(String name, String sex, String age, int id);
public int delete(int id);
public MyLove findMyLove(int id);
public List<MyLove> findMyLoveList();
}
Impl层
package com.xiami.service;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.xiami.dao.MyLoveMapper;
import com.xiami.entity.MyLove;
@Service
public class MyLoveMapperServiceImpl implements MyLoveMapperService {
@Autowired
private MyLoveMapper myLoveMapper;
@Override
public int add(String name, String sex, String age) {
return myLoveMapper.add(name, age, sex);
}
@Override
public int update(String name, String sex, String age, int id) {
return myLoveMapper.update(name, age, sex, id);
}
@Override
public int delete(int id) {
return myLoveMapper.delete(id);
}
@Override
public MyLove findMyLove(int id) {
return myLoveMapper.findMlove(id);
}
@Override
public List<MyLove> findMyLoveList() {
return myLoveMapper.findMyLoveList();
}
}
controller层
package com.xiami.myLoveController;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import com.xiami.entity.MyLove;
import com.xiami.service.MyLoveMapperService;
public class MyLoveMapperController {
@Autowired
MyLoveMapperService myLoveMapperService;
@RequestMapping(value = "/list", method = RequestMethod.POST)
public List<MyLove> POSTAccounts() {
return myLoveMapperService.findMyLoveList();
}
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public MyLove POSTAccountById(@PathVariable("id") int id) {
return myLoveMapperService.findMyLove(id);
}
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String updateAccount(@PathVariable("id") int id, @RequestParam(value = "name") String name,
@RequestParam(value = "age") String age,@RequestParam(value = "sex") String sex)
{
int t= myLoveMapperService.update(name, sex, age, id);
if(t==1) {
return "success";
}else {
return "fail";
}
}
@RequestMapping(value = "/{id}", method = RequestMethod.POST)
public String delete(@PathVariable(value = "id")int id) {
int t= myLoveMapperService.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 = "age") String age,@RequestParam(value = "sex") String sex) {
int t= myLoveMapperService.add(name, sex, age);
if(t==1) {
return "success";
}else {
return "fail";
}
}
}
通过postman完成测试。