简单的完成一个springboot和mybatis的增删改查
pom中一定要引入的两个包
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.0.1
mysql
mysql-connector-java
runtime
我新建的表结构
applicatio.properties中的配置信息
#端口号
server.port: 8083// 端口号是可以修改的
#数据库链接设置
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url: jdbc:mysql://localhost:3306/new?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
spring.datasource.username: 你自己的用户名
spring.datasource.password: 你自己的代码
#整合Mybatis
mybatis.type-aliases-package: com.example.firstspringboot.entity
#mybatis 对应的 .xml文件路径
mybatis.mapper-locations: classpath:mapper/*.xml
# 多层级目录 mapper-locations: classpath:mapper/**/**.xml
实体类People.class
public class People {
private String id;
private String name;
public People() {
}
public People(String id, String name) {
this.id = id;
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Peoplemapper.java
package com.example.firstspringboot.mapper;
import com.example.firstspringboot.entity.People;
public interface Peoplemapper {
/**
* 完成简单的增删改查功能
*/
int insert(People people);
int deleteByPrimaryKey(String id);
int updateByPrimaryKey(People people);
People selectByPrimaryKey(String id);
}
注意此时的@SpringBootApplication要多加上一个配置
@SpringBootApplication
@MapperScan("com.example.firstspringboot.mapper")
public class FirstspringbootApplication {
public static void main(String[] args) {
SpringApplication.run(FirstspringbootApplication.class, args);
}
}
PeopleService.java这个应该是一个接口,然后由接口的实现类去实现逻辑功能,太麻烦直接就在里面写了
@Service
public class PeopleService {
@Autowired
private Peoplemapper peoplemapper;
public int insert(People people) {
peoplemapper.insert(people);
return 0;
}
public int deleteByPrimaryKey(String id) {
peoplemapper.deleteByPrimaryKey(id);
return 0;
}
public int updateByPrimaryKey(People people) {
peoplemapper.updateByPrimaryKey(people);
return 0;
}
public People selectByPrimaryKey(String id) {
return peoplemapper.selectByPrimaryKey(id);
}
}
PeopleController
@RestController
public class PeopleController {
@Autowired
private PeopleService peopleService;
@RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
public void deletepeople(@PathVariable("id") String id){
peopleService.deleteByPrimaryKey(id);
}
@ResponseBody
@RequestMapping(value = "/insert",method = RequestMethod.POST)
public void insertpeople(@RequestBody People people){
System.out.println("运行");
peopleService.insert(people);
}
@RequestMapping(value = "/update",method = RequestMethod.POST)
public void updatepeople(@RequestBody People people){
peopleService.updateByPrimaryKey(people);
}
@ResponseBody
@RequestMapping(value = "/select/{id}",method = RequestMethod.GET)
public People selectpeople(@PathVariable("id") String id ){
return peopleService.selectByPrimaryKey(id);
}
}
peoplemapper.xml
delete from people where id = #{id}
insert into people(id ,name ) values(#{id},#{name})
update people set name =#{name } where id =#{id}
然后我使用的postman做的测试。