目錄結構web
選擇Web、Mabatis、postgreSQLspring
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(運行時生效),因此並不影響代碼的運行。sql
修改方法:數據庫
右鍵點擊項目——選擇「open module settings」——點擊「Dependencies」,找到Maven:org.postgresql:postgresql:42.2.5將runtime修改成Compileapache
首先在postgreSQL數據庫中新建一個表table_one,並建立一個BeanUser的實體類。springboot
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
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 }
接收和處理客戶端的請求app
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 }
查詢post
插入this
修改url
刪除