IDEA版:
1.添加依赖
//web
compile('org.springframework.boot:spring-boot-starter-web')
//mybatis依赖
compile('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2')
//mysql依赖
runtime('mysql:mysql-connector-java')
//lombok
compileOnly('org.projectlombok:lombok')
2.controller层代码
@RestController
@RequestMapping(value = "/mybatis")
public class PersonController {
@Resource
private PersonService personService;
//添加用户
@RequestMapping(value = "/addperson", method = RequestMethod.GET)
public String addPerson() {
Person person = new Person();
person.setName("千夜");
person.setAge(24);
personService.addPerson(person);
return "SUCCESS";
}
//根据id查询用户
@RequestMapping(value = "/getpersonbyid/id/{id}", method = RequestMethod.GET)
public Person getPersonById(@PathVariable(value = "id") Integer id) {
return personService.getPersonById(id);
}
}
3.service层代码
public interface PersonService {
void addPerson(Person person);
Person getPersonById(Integer id);
}
@Service
public class PersonServiceImpl implements PersonService {
@Resource
private PersonMapper personMapper;
@Override
public void addPerson(Person person) {
personMapper.addPerson(person);
}
@Override
public Person getPersonById(Integer id) {
return personMapper.getPersonById(id);
}
}
4.repository层代码
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Person {
private Integer id;
private String name;
private Integer age;
}
@Mapper
public interface PersonMapper {
@Insert("insert into person (name, age) values (#{name}, #{age})")
void addPerson(Person person);
@Select("select * from person where id = #{id}")
Person getPersonById(Integer id);
}