Springboot 使用Mybatis对postgreSQL实现CRUD

 

目录结构

 

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/463642.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

boost学习之boost::lock_guard源码分析

boost::lock_guard可以说是一种比boost::unique_lock轻量级的lock, 简单一些场景可以用它就行了。源码如下&#xff1a; template<typename Mutex> class lock_guard { private: Mutex& m; explicit lock_guard(lock_guard&); lock_guard& operator(lock…

搜索连接字符串存储过程【原创】

1、CREATE PROCEDURE dbo.SearchUserInfo (Age int ) AS SELECT * FROM UserInfo WHERE Age like %cast(Age as nvarchar(50))% RETURN 2、 CREATE PROCEDURE dbo.Mfx_SearchUserInfo --用户搜索[state-审核状态(2为全部)、UserLever-用户级别(0为全部)、keyWord(关键字)] …

mysql my.cnf 配置建议

/usr/local/share/mysql下面有5个my-xxxx.cnf文件 my-small.cnf 最小配置安装&#xff0c;内存⇐64M&#xff0c;数据数量最少 my-large.cnf 内存512M my-medium.cnf 32M<内存<64M&#xff0c;或者内存有128M&#xff0c;但是数据库与web服务器公用内存 my-huge.cnf …

不得不说,这是我面过的最优秀的Linux运维!

Linux可以说是运维之“本”。无论中小企业还是大厂&#xff0c;现在的企业有95%甚至更多是使用Linux服务器。而对于Linux运维来说&#xff0c;Linux基础越扎实、会的工具越多&#xff0c;能解决的问题就越多&#xff0c;技术也能走的更远。Linux&#xff0c;甚至可以说是进入IT…

boost学习之boost::lock_guardT与boost::unique_lockT的区别

(1)、boost::lock_guard没有unlock()功能 boost::mutex mutex; void foo( ) { boost::lock_guard<boost::mutex> lock(mutex); process(data); ///没有unlock()功能&#xff0c;程序结束自动析构 }; (2)、unique_lock允许设置超时&#xff0c;推迟锁定lock以及在对象…

用友财务软件主要数据表字段含义

用友财务软件主要数据表字段含义 /*rdrecords : 收发记录子表AutoID : auto ID 自动编号 automatism identification(identity)ID : ID 与收发记录主表关联项cInvCode : c…

重叠IO之完成例程

http://hi.baidu.com/%CC%EC%D1%C4_jmf/blog/item/7718ccd99b03cb2710df9bc2.html转载于:https://www.cnblogs.com/vcdebug2010/archive/2012/06/23/2559096.html

一个中科大「差生」的8年程序员工作总结

今年终于从大菊花厂离职了&#xff0c;离职前收入大概60w不到吧&#xff01;在某乎属于比较差的&#xff0c;今天终于有空写一下自己的职场故事&#xff0c;也算是给自己近8年的工作做个总结复盘。近8年有些事情做对了&#xff0c;也有更多事情做错了&#xff0c;在这里记录一下…

Java IO File

#file file的一些方法&#xff0c;因为windows和Linux开发环境的问题&#xff0c;在file中最好统一用 / 输出流操作 转载于:https://www.cnblogs.com/cykfory/p/10294981.html

gtest使用例子

最近使用gtest进行单元测试&#xff0c;采用打桩的形式。关于gtest的详细说明就不多说了&#xff0c;网上的资料一大堆。主要讲解使用时的参数如何配置以及遇到的问题。下面的例子模拟是加、减、乘、除四则运算&#xff0c;前提是不知道加、减、乘、除四则运算是如何实现的。 …

游戏开发中的数学和物理算法(7):角度 vs 弧度

我们通常使用的笛卡尔坐标系统&#xff0c;角点通常在(0,0),即原点。初始边在x轴正半轴&#xff0c;终边与初始边成夹角。初始边逆时针旋转为正值&#xff0c;顺时针旋转为逆值。数学表示&#xff1a;角度&#xff1a;degreeradian*180/π 弧度&#xff1a;radiandegree*π/18…

医学影像PACS系统解决方案与成功案例汇总

经历过2010年一年时间&#xff0c;HC3i论坛也与大家相伴快一年了&#xff0c;这一年中&#xff0c;网友分享专业医疗信息化资料超过15000个&#xff0c;HC3i感谢大家的支持与厚爱&#xff01;岁末年初之极&#xff0c;也整理盘点一下&#xff0c;方便大家回顾一年中精华资源&am…

npm 安装包报错 rollbackFailedOptional

npm config rm proxynpm config rm https-proxy 然后使用npm install -g cnpm --registryhttps://registry.npm.taobao.org安装淘宝的cnpm 然后就可以使用cnpm命令了转载于:https://www.cnblogs.com/xtjatswc/p/10295734.html

一个小老板的春天

大家周末好继续之前说的采访专栏&#xff0c;前两天和一个做生意的朋友聊天&#xff0c;总结了一些内容分享给大家&#xff0c;觉得不错的点赞收藏起来&#xff0c;可能后面你自己当老板了会用到。我这个老板的名字和公司名字我就不说出来了&#xff0c;他现在做的是细分领域&a…

boost学习之boost::shared_ptr

Boost智能指针——shared_ptr boost::scoped_ptr虽然简单易用&#xff0c;但它不能共享所有权的特性却大大限制了其使用范围&#xff0c;而boost::shared_ptr可以解决这一局限。顾名思义&#xff0c;boost::shared_ptr是可以共享所有权的智能指针&#xff0c;首先让我们通过一…

SharePoint GridView的使用2——DataSourceView的使用

首先创建一个abstract类&#xff0c;继承Microsoft.SharePoint.WebControls.DataTableDataSourceView。之后基于这个类可以创建多个显示不同数据的的DataSourceView&#xff0c;在上文的DataSource控件中有个ViewName属性&#xff0c;可以通过这个属性来区分不同的DataSourceVi…

瞧瞧UC浏览器对CSS样式的“关怀”

瞧瞧UC浏览器对CSS样式的“关怀” UC对CSS这样的“照顾”尤其8.0以下体现的是淋漓尽致&#xff01; 不"支持"font-family属性&#xff0c;也就是说&#xff0c;在UC浏览器你只能看到一种字体&#xff1b;不"支持"font-szie属性&#xff0c;也就是说&#…

我在ARM板上写的第一个驱动程序

有时大家喜欢拿电灯来当作笑谈&#xff0c;实际上点灯包含多内容很多&#xff0c;如下这篇文章就是关于嵌入式Linux点灯多技术栈&#xff0c;推荐给大家。摘要&#xff1a;搞嵌入式有两个方向&#xff0c;一个是嵌入式软件开发(MCU方向)&#xff0c;另一个是嵌入式软件开发(Lin…

从github上克隆hibernate项目

开发的项目用到了hibernate进行对象的持久化&#xff0c;最近项目上不忙&#xff0c;打算通过官方文档和源码来进行深度学习。第一步将hibernate部署到本地就折腾了好久&#xff0c;打算记录一下。 关于github的注册说一句&#xff0c;推荐使用outlook邮箱进行注册&#xff0c;…

string、char *、char []之间的相互转换

最近工作中遇到了string、char *、char []之间的相互转换&#xff0c;今天终于抽出时间将他们之间的转换记录下来&#xff0c;使用的是CodeBlocks软件&#xff0c;编译器为GNU GCC compiler&#xff0c;下面看代码&#xff1a; #include <iostream> #include <stdio.…