sql:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
-- -------------------------------------------------------- -- 主機: 127.0.0.1 -- 服務器版本: 10.3.9-MariaDB - mariadb.org binary distribution -- 服務器操做系統: Win64 -- HeidiSQL 版本: 9.4.0.5125 -- -------------------------------------------------------- /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET NAMES utf8 */; /*!50503 SET NAMES utf8mb4 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; -- 導出 wbg_logistics 的數據庫結構 CREATE DATABASE IF NOT EXISTS `wbg_logistics` /*!40100 DEFAULT CHARACTER SET utf8 */; USE `wbg_logistics`; -- 導出 表 wbg_logistics.role 結構 CREATE TABLE IF NOT EXISTS `role` ( `id` int(11) NOT NULL AUTO_INCREMENT, `role_name` varchar(20) DEFAULT NULL, `note` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=103 DEFAULT CHARSET=utf8; -- 數據導出被取消選擇。 /*!40101 SET SQL_MODE=IFNULL(@OLD_SQL_MODE, '') */; /*!40014 SET FOREIGN_KEY_CHECKS=IF(@OLD_FOREIGN_KEY_CHECKS IS NULL, 1, @OLD_FOREIGN_KEY_CHECKS) */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
第一步:建立好spring boot項目
教程:https://www.cnblogs.com/weibanggang/p/10200945.htmlhtml
第二步:配置
一、build.gradle加入依賴
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
repositories { //使用阿里雲倉庫 maven{ url 'http://maven.aliyun.com/nexus/content/groups/public/'} mavenCentral() } dependencies { implementation('org.springframework.boot:spring-boot-starter-data-jpa') implementation('org.springframework.boot:spring-boot-starter-thymeleaf') implementation('org.springframework.boot:spring-boot-starter-web') // mariadb compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.3.0' // hibernate compile group: 'org.hibernate', name: 'hibernate-core', version: '5.4.0.Final' implementation('org.mybatis.spring.boot:mybatis-spring-boot-starter:1.3.2') testImplementation('org.springframework.boot:spring-boot-starter-test') }
二、配置數據application
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
#數據驅動 # compile group: 'org.mariadb.jdbc', name: 'mariadb-java-client', version: '2.3.0' spring.datasource.driver-class-name=org.mariadb.jdbc.Driver #數據庫鏈接 spring.datasource.url=jdbc:mariadb://localhost:3306/wbg_logistics #數據庫密碼 spring.datasource.password=123456 #數據庫用戶名 spring.datasource.username=root #設置數據庫鏈接的平臺 spring.jpa.database-platform=org.hibernate.dialect.MariaDB10Dialect spring.jpa.hibernate.ddl-auto=update #監控 management.endpoints.web.exposure.include=* server.port=8080
第三步:建立類、接口、訪問數據
一、Role類:
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
package com.wbg.springbootdemo.entity; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table public class Role { @Override public String toString() { return "Role{" + "id=" + id + ", roleName='" + roleName + '\'' + ", note='" + note + '\'' + '}'; } @Id int id; String roleName; String note; public Role() { } public int getId() { return id; } public Role(int id) { this.id = id; } public void setId(int id) { this.id = id; } public String getRoleName() { return roleName; } public void setRoleName(String roleName) { this.roleName = roleName; } public String getNote() { return note; } public void setNote(String note) { this.note = note; } public Role(int id, String roleName, String note) { this.id = id; this.roleName = roleName; this.note = note; } }
二、 RoleMapper接口繼承JpaRepositoryjava
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
package com.wbg.springbootdemo.dao; import com.wbg.springbootdemo.entity.Role; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; @Repository public interface RoleMapper extends JpaRepository<Role,Integer> { }
三、RoleServicegit
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
package com.wbg.springbootdemo.service; import com.wbg.springbootdemo.entity.Role; import java.util.List; import java.util.Optional; public interface RoleService { List<Role> listAll(); void save(Role role); void update(Role role); void delete(int id); Optional<Role> findById(int id); }
四、RoleServiceImpl實現類github
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
package com.wbg.springbootdemo.service.impl; import com.wbg.springbootdemo.dao.RoleMapper; import com.wbg.springbootdemo.entity.Role; import com.wbg.springbootdemo.service.RoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class RoleServiceImpl implements RoleService { @Autowired private RoleMapper roleMapper; /** * * 查詢所有 * @return */ @Override public List<Role> listAll() { return roleMapper.findAll(); } /** * 添加 * @param role */ @Override public void save(Role role) { roleMapper.save(role); System.out.println("添加"); } //若是有就修改、沒有就添加 @Override public void update(Role role) { roleMapper.saveAndFlush(role); } /** * 刪除 * @param id */ @Override public void delete(int id) { roleMapper.delete(new Role(id)); } /** * 查詢 * @param id * @return */ @Override public Optional<Role> findById(int id) { return roleMapper.findById(id); } }
五、RoleControllerweb
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
package com.wbg.springbootdemo.controller; import com.wbg.springbootdemo.entity.Role; import com.wbg.springbootdemo.service.RoleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; import java.util.Optional; @RestController @RequestMapping("/role") public class RoleController { @Autowired private RoleService roleService; @GetMapping("/all") public List<Role> listAll(){ return roleService.listAll(); } @GetMapping("/delete") public List<Role> delete(int id){ roleService.delete(id); return roleService.listAll(); } @GetMapping("/getById") public Optional<Role> getById(int id){ return roleService.findById(id); } @PostMapping("/insert") public List<Role> insert( Role role){ roleService.save(role); return roleService.listAll(); } @PostMapping("/update") public List<Role> update(Role role){ roleService.update(role); return roleService.listAll(); } }
第四步:index.html
![](http://static.javashuo.com/static/loading.gif)
![](http://static.javashuo.com/static/loading.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <a href="/role/all">查詢所有</a> <br/> <br/> <form action="/role/getById"> <p>id: <input type="number" name="id"/><input type="submit" value="單個查詢"/></p> </form><br/> <br/> <br/> <form action="/role/delete"> <p>id: <input type="number" name="id"/><input type="submit" value="刪除"/></p> </form> <br/> <br/> <form method="post" action="/role/insert"> <p>id:<input type="number" name="id"/></p> <p>roleName:<input type="text" name="roleName"/></p> <p>note:<input type="text" name="note"/></p> <p><input type="submit" value="添加"/></p> </form> <br/> <br/> <form method="post" action="/role/update"> <p>id:<input type="number" name="id"/></p> <p>roleName:<input type="text" name="roleName"/></p> <p>note:<input type="text" name="note"/></p> <p><input type="submit" value="修改"/></p> </form> </body> </html>
測試
demo:https://github.com/weibanggang/springboothibernate.gitspring