java hibernate用法

java hibernate用法

调用rest api的方法
zuul网关调用方法

http://192.168.10.29:9000/api-datashare/catalog/listall
http://192.168.10.29:9000/api-datashare/catalog/tree

javascript调用方法

//http://192.168.10.22:8002/api-sys/oauth/token
//登录 OK
function API_login(strName, strPwd) {
    let loginParams = new FormData();   //必须用FormData()OK
    loginParams.append('client_id', 'app');
    loginParams.append('client_secret', '123456');
    loginParams.append('grant_type', 'password');
    loginParams.append('username', strName);
    loginParams.append('password', strPwd);
    return API_POST('/api-sys/oauth/token', loginParams)
};

本地调用方法

http://192.168.10.22:8006/catalog/listAll
http://192.168.10.22:8006/catalog/tree

引入pom.xml的库

<dependency>
    <groupId>org.postgresql</groupId>
     <artifactId>postgresql</artifactId>
     <version>42.2.5</version>
 </dependency>
 <dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-data-jpa</artifactId>
 </dependency>
 <dependency>
     <groupId>org.projectlombok</groupId>
     <artifactId>lombok</artifactId>
     <version>${lombok.version}</version>
 </dependency>

定义hibernate 表类 定义

package com.bootdo.clouddodatashare.domain;

import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;

@Data
@Entity
public class ldap_catalog {
    @Id
    private String id;
    private int sid;
    private String cate_name;
    private String parent_id;
    private String remark;
    private String cate_std;
}

定义表类对象的操作类

package com.bootdo.clouddodatashare.dao;

import com.bootdo.clouddocommon.dto.Tree;
import com.bootdo.clouddodatashare.domain.ldap_catalog;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;

@Repository
public interface CatalogDao extends JpaRepository<ldap_catalog,String> {
}

定义restController类 rest api定 义

package com.bootdo.clouddodatashare.controller;

import com.bootdo.clouddocommon.dto.Tree;
import com.bootdo.clouddocommon.utils.BuildTree;
import com.bootdo.clouddocommon.utils.uuidUtil;
import com.bootdo.clouddodatashare.dao.CatalogDao;
import com.bootdo.clouddodatashare.domain.ldap_catalog;
import com.bootdo.clouddodatashare.service.CatalogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RequestMapping("/catalog")
@RestController
public class catalogController {
    @Autowired
    private CatalogDao catalogDao;

    @ResponseBody
    @GetMapping("/listall")
    public List<ldap_catalog> listAll(){
        List<ldap_catalog> tlist= catalogDao.findAll();
        return tlist;
    }
    public ldap_catalog get(String id)
    {
        return catalogDao.getOne(id);
    }
    @ResponseBody
    @GetMapping("/tree")
    public Tree<ldap_catalog> getTree()
    {
        List<Tree<ldap_catalog>> trees = new ArrayList<Tree<ldap_catalog>>();
        List<ldap_catalog> tList = catalogDao.findAll();
        for (ldap_catalog c : tList) {
            Tree<ldap_catalog> tree = new Tree<ldap_catalog>();
            tree.setId(c.getId().toString());
            tree.setParentId(c.getParent_id().toString());
            tree.setText(c.getCate_name());
            tree.setObject(c);
            trees.add(tree);
        }
        // 默认顶级菜单为0,根据数据库实际情况调整
        Tree<ldap_catalog> t = BuildTree.build(trees);
        return t;
    }
    @ResponseBody
    @PostMapping("/add")
    public ldap_catalog add(ldap_catalog c)
    {
        c.setId(uuidUtil.getUUID());
        return catalogDao.save(c);
    }
    @ResponseBody
    @PostMapping("/update")
    public ldap_catalog update(ldap_catalog c)
    {
        return catalogDao.save(c);
    }
    @ResponseBody
    @DeleteMapping("/remove")
    public void remove(String Id)
    {
        catalogDao.deleteById(Id);
    }
    @ResponseBody
    @PostMapping("/batchremove")
    public void batchRemove(String[] cIds)
    {
        for (String cId : cIds)
        {
            catalogDao.deleteById(cId);
        }
        //List<String> delIds=new ArrayList<>();
        //Iterator<String> items=delIds.iterator();
        //catalogDao.deleteInBatch(catalogDao.findAllById(items).iterator());
    }


}

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页