springboot与mybatis完成简单的增删改查
简单的完成一个springboot和mybatis的增删改查
pom中一定要引入的两个包
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.0.1</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
我新建的表结构
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
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.firstspringboot.mapper.Peoplemapper">
<resultMap id = "BaseResultMap" type="com.example.firstspringboot.entity.People">
<id column ="id" jdbcType = "VARCHAR" property = "id"></id>
<result column= "name" jdbcType = "VARCHAR" property="name"/>
</resultMap>
<select id ="selectByPrimaryKey" parameterType = "String" resultMap = "BaseResultMap">
select * from people where id =#{id}
</select>
<delete id = "deleteByPrimaryKey" parameterType = "String" >
delete from people where id = #{id}
</delete>
<insert id = "insert" parameterType = "com.example.firstspringboot.entity.People" >
insert into people(id ,name ) values(#{id},#{name})
</insert>
<update id ="updateByPrimaryKey" parameterType="com.example.firstspringboot.entity.People" >
update people set name =#{name } where id =#{id}
</update>
</mapper>
然后我使用的postman做的测试。