目录结构

1、创建一个springboot项目

选择Web、Mabatis、postgreSQL

2、在application中写入配置文件

1 #配置数据源
2 spring.datasource.platform=postgres
3 spring.datasource.url=jdbc:postgresql://127.0.0.1:5432/postgres
4 spring.datasource.username=postgres
5 spring.datasource.password=123456
6 spring.datasource.driverClassName=org.postgresql.Driver

  写入完成后,org.postgresql.Driver会报错(可以不管)。原因是postgresql这个jar包依赖类型默认是runtime(运行时生效),所以并不影响代码的运行。

  修改方法:

  右键点击项目——选择“open module settings”——点击“Dependencies”,找到Maven:org.postgresql:postgresql:42.2.5将runtime修改为Compile

 

3、创建一个实例化的实体类

首先在postgreSQL数据库中新建一个表table_one,并创建一个BeanUser的实体类。

 1 package com.example.bean;
 2
 3 public class BeanUser {
 4
 5     private Integer id;
 6     private String user_name;
 7     private String pass_word;
 8
 9     public Integer getId() {
10
11         return id;
12
13     }
14
15     public void setId(Integer id) {
16
17         this.id = id;
18
19     }
20
21     public String getUser_name() {
22
23         return user_name;
24
25     }
26
27     public void setUser_name(String user_name) {
28
29         this.user_name = user_name;
30
31     }
32
33     public String getPass_word() {
34
35         return pass_word;
36
37     }
38
39     public void setPass_word(String pass_word) {
40
41         this.pass_word = pass_word;
42
43     }
44 }
45
46  

4、创建一个对表CRUD的实体类

 1 package com.example.mapper;
 2
 3 import com.example.bean.BeanUser;
 4
 5 import org.apache.ibatis.annotations.*;
 6
 7
 8
 9 //指定这是一个操作数据库的mapper
10
11 @Mapper
12
13 public interface BeanUserMapper {
14
15
16
17     //查询
18
19     @Select("select * from table_one where id=#{id}")
20
21     public BeanUser selectId(Integer id);
22
23
24
25     //删除
26
27     @Delete("delete from table_one where id=#{id}")
28
29     public int deleteId(Integer id);
30
31
32
33     //插入
34
35     @Insert("insert into table_one(id,user_name,pass_word) values(#{id},#{user_name},#{pass_word})")
36
37     public int insertBean(BeanUser beanUser);
38
39
40
41     //修改
42
43     @Update("update table_one set user_name=#{user_name},pass_word=#{pass_word} where id=#{id}")
44
45     public int UpdateBean(BeanUser beanUser);
46
47 }

5、创建一个controller控制器

接收和处理客户端的请求

 1 package com.example.controller;
 2
 3 import com.example.bean.BeanUser;
 4
 5 import com.example.mapper.BeanUserMapper;
 6
 7 import org.springframework.beans.factory.annotation.Autowired;
 8
 9 import org.springframework.web.bind.annotation.PathVariable;
10
11 import org.springframework.web.bind.annotation.RequestMapping;
12
13 import org.springframework.web.bind.annotation.RestController;
14
15
16 @RestController
17
18 public class ControllerBean {
19
20     @Autowired
21
22     BeanUserMapper beanUserMapper;
23
24     //查询
25
26     @RequestMapping("/get/{id}")
27
28     public BeanUser getBeanUser(@PathVariable("id") Integer id){
29
30         return beanUserMapper.selectId(id);
31
32     }
33     //插入
34
35     @RequestMapping("/insert")
36
37     public BeanUser insertBeanUser(BeanUser beanUser){
38
39         beanUserMapper.insertBean(beanUser);
40
41         return beanUser;
42
43     }
44     //修改
45
46     @RequestMapping("/update")
47
48     public BeanUser updateBeanUser(BeanUser beanUser){
49
50         beanUserMapper.UpdateBean(beanUser);
51
52         return beanUser;
53
54     }
55     //删除
56
57     @RequestMapping("/delete/{id}")
58
59     public String deleteBeanUser(@PathVariable("id")Integer id){
60
61          beanUserMapper.deleteId(id);
62
63          return "删除成功!";
64
65     }
66 }

6、运行成功

查询

插入

修改

删除

转载于:https://www.cnblogs.com/zh-lin/p/10293332.html

Springboot 使用Mybatis对postgreSQL实现CRUD相关推荐

  1. SpringBoot整合Mybatis超详细流程

    SpringBoot整合Mybatis超详细流程 文章目录 SpringBoot整合Mybatis超详细流程 前言 详细流程 0.引入Mybatis 1.创建数据 2.创建程序目录 3.理解后台访问流 ...

  2. springboot 和 mybatis整合:参数查询和动态sql

    springboot 和 mybatis整合: mapper定义的是数据库的操作方法: @Mapper public interface UserMapper {} 单参数的处理: @Select(& ...

  3. SpringBoot:Mybatis + Druid 数据访问

    SpringBoot:Mybatis + Druid 数据访问 文章目录 SpringBoot:Mybatis + Druid 数据访问 1.简介 2.JDBC 3.CRUD操作 4.自定义数据源 D ...

  4. SpringBoot集成MyBatis的分页插件PageHelper(回头草)

    俗话说:好?不吃回头草,但是在这里我建议不管你是好马还是不好马,都来吃吃,带你复习一下分页插件PageHelper. 昨天给各位总结了本人学习springboot整合mybatis第一阶段的一些学习心 ...

  5. Springboot整合mybatis plus生成代码

    一.Springboot整合mybatis plus生成代码 1.介绍 1.1.前言 从零开始搭建一个项目骨架,最好选择合适熟悉的技术,并且在未来易拓展,适合微服务化体系等.所以一般以Springbo ...

  6. Mybatis完整功能实现CRUD

    Mybatis完整功能实现CRUD Mybatis完整功能实现CRUD 对于刚使用的新手提供了完整的增删改查并附带测试,首先我们介绍下ORM 对象关系映射的框架 (3)JDBC有优缺点 (4)JPA的 ...

  7. springboot之mybatis完美一击mybatis-plus

    springboot之mybatis完美一击mybatis-plus 说明 添加依赖 entity mapper 分页插件 mp的通用crud 一些细节 两个update操作的区别 查询 删除 条件构 ...

  8. SpringBoot集成Mybatis项目实操

    本文为<从零打造项目>系列第三篇文章,首发于个人网站. <从零打造项目>系列文章 比MyBatis Generator更强大的代码生成器 SpringBoot项目基础设施搭建 ...

  9. 基于SpringBoot集合Mybatis的增删改查实现,通过postman测试(1)

    本篇介绍基于SpringBoot和Mybatis+mysql+maven的简单实现增删改查的案例,详细介绍如下, 代码地址附上:https://git.lug.ustc.edu.cn/nantian_ ...

最新文章

  1. Mysql 查看连接数,状态 最大并发数 怎么设置才合理
  2. js中的if与Java中的if_JavaScript if...else 语句
  3. 加密界又一响声:WhatsApp宣布对所有通讯信息进行端到端加密
  4. Qt中打开excel文件
  5. 官方消息:微软再次提醒IE浏览器将于6月15日停止支持
  6. linux ssl 证书服务器,Linux下Nginx安全证书ssl配置方法
  7. Windows Server 2003 SP2中文版开放下载
  8. SpringBoot2.x填坑(一):使用CROS解决跨域并解决swagger 访问不了问题
  9. 使用getopt函数对命令行短形参进行处理
  10. Serial垃圾回收器总结
  11. idea Mac格式化代码快捷键
  12. 在线英英词典项目实现
  13. Unity DoTween
  14. 计算机和现代通讯的应用,现代计算机通信技术特点及通信网络的应用.docx
  15. CAD工具——批量打印
  16. tomcat解决get请求中文乱码问题(两种解决方案)
  17. 女神瓦萨比-小黑中国力鉴淘宝给力明星店
  18. CES 2019上芯片巨头们的争夺焦点:光线追踪、“永远”在线PC、汽车...
  19. 终于给自己买了台电脑
  20. 如何处理编码GBK的不可映射字符

热门文章

  1. Could not get lock /var/lib/apt/lists/lock - open (11: Resource temporarily unavailable)
  2. BestCoder Round #39 解题报告
  3. Hibernate读书笔记
  4. 自动备份多个MOSS站点集的脚本
  5. Redis数据结构之简单动态字符串SDS
  6. 最小编辑代价-golang
  7. 第 11 章 Paragraphs
  8. java: cannot execute binary file 如果遇到这个错,一般是操作系统位数出问题了。
  9. electron 入坑记
  10. javascript 变量作用域