當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
Springboot 使用Mybatis对postgreSQL实现CRUD
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                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 464、創建一個對表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的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 使用DevCpp/DevC++调试的设置
- 下一篇: 软件设计文档国家标准—软件需求说明书(G
