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做的测试。

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值