简单使用Spring Boot+JpaRepository+hibernate搭建项目

 

sql:

-- --------------------------------------------------------
-- 主机:                           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 */;
View Code

第一步:创建好spring boot项目

教程:https://www.cnblogs.com/weibanggang/p/10200945.html

第二步:配置

1、build.gradle加入依赖

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')
}
View Code

2、配置数据application

#数据驱动
#  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
View Code

第三步:创建类、接口、访问数据

  1、Role类:
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;
    }
}
View Code

  2、 RoleMapper接口继承JpaRepository

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> {

}
View Code

  3、RoleService

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);
}
View Code

  4、RoleServiceImpl实现类

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);
    }
}
View Code

  5、RoleController

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();
    }

}
View Code

第四步:index.html

<!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>
View Code

测试

 

demo:https://github.com/weibanggang/springboothibernate.git

posted @ 2018-12-31 13:50  韦邦杠  阅读(2749)  评论(0编辑  收藏  举报