springboot JPA-Hibernate查询数据

1、先在pom.xml文件添加依赖

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>

2、再在application.properties文件新增数据库配置

pring.datasource.url = jdbc:mysql://127.0.0.1:3306/数据库名?useSSL=FALSE&serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8

spring.datasource.username = root 用户

spring.datasource.password = 密码

spring.datasource.driverClassName = com.mysql.cj.jdbc.Driver
spring.datasource.max-active=20

spring.datasource.max-idle=8

spring.datasource.min-idle=8

spring.datasource.initial-size=10


########################################################

### Java Persistence Api

########################################################

# Specify the DBMS

spring.jpa.database = MYSQL

# Show or not log for each sql query

spring.jpa.show-sql = true

# Hibernate ddl auto (create, create-drop, update)

#spring.jpa.hibernate.ddl-auto = create-drop //如果打开这个注释,重新编译再操作数据表,数据表会删除重新建表导致数据被清

# Naming strategy

#[org.hibernate.cfg.ImprovedNamingStrategy #org.hibernate.cfg.DefaultNamingStrategy]

spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy

# stripped before adding them to the entity manager)

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5Dialect

3、前期工作做好之后,先看一下我的目录结构

 


   4、先建一个数据表对应的实体对象class放在domain目录下,Node.java,我的表名是node

@Entity
@Table(name = "node")
public class Node extends JpaRepositoriesAutoConfiguration {
@Id //主键注解一定要加上,不然要抛异常
@GeneratedValue
private long id;
private String name;
private String title;
private int status;
private int sort;
private int pid;
private int level;

public long getId() {
return id;
}

public void setId(long id) {
this.id = id;
}
public String getName(){
return name;
}

public void setName(String name){
this.name=name;
}

public String getTitle(){
return title;
}
public void setTitle(String title){
this.title=title;
}
public int getStatus(){
return status;
}
public void setStatus(int status){
this.status=status;
}

public int getSort(){
return sort;
}
public void setSort(int sort){
this.sort=sort;
}
public void setPid(int pid){
this.pid=pid;
}
public int getPid(){
return pid;
}

public void setLevel(int level){
this.level=level;
}
public int getLevel(){
return level;
}
}

5、再建一个interface NodeDao.java放在dao目录下

import java.util.List;
import com.spring.first.domain.Node;
public interface NodeDao extends CrudRepository<Node,Long> {

//根据编号查询
public Node findById(long id);
@Query(value = "select * from node where name like %?1%",nativeQuery = true)
@Modifying
public List<Node> findByNameLike(String name);
}


6、再建一个interface NodeService.java放在service目录下
package com.spring.first.service;
import com.spring.first.domain.Node;

import java.util.List;

public interface NodeService {
public void save(Node node);

public List<Node> findByNameLike(String name);
}

7、接着建一个NodeServiceImpl.java放在serviceimpl目录下
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spring.first.service.NodeService;
import com.spring.first.dao.NodeDao;
import com.spring.first.domain.Node;

import java.util.List;

@Service
public class NodeServiceImpl implements NodeService{
@Autowired
private NodeDao nodeDao;

public void save(Node node){

nodeDao.save(node);
}

public List<Node> findByNameLike(String name){
return nodeDao.findByNameLike(name);
}

}
8、最后就是写controller了
package com.spring.first.controller;
import javax.annotation.Resource;
import javax.validation.Valid;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.spring.first.service.DemoService;
import com.spring.first.service.NodeService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import com.spring.first.domain.*;
import com.spring.first.vo.NodeVo;
import java.util.List;


@RestController
public class DemoController {


@Resource
private NodeService nodeService;
@RequestMapping("/getData")
@ResponseBody
//这个方法只能返回单条数据 返回的是json对象
public ResponseEntity<Object> getData(@RequestParam(name = "name") String name){
return new ResponseEntity<>(demoService.getDemoByTname(name), HttpStatus.OK);
}


@RequestMapping("/getDatas")
@ResponseBody
//这个方法可返回多条数据,返回json数组
public List<Node> findName(@RequestParam(name="name") String name){
List<Node> bList=nodeService.findByNameLike(name);
return bList;

}
}


再浏览器上输入地址访问http://localhost:9090/getDatas?name=same
返回如图格式的数据

 

 

 

  完结!






posted @   smellycats  阅读(858)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 因为Apifox不支持离线,我果断选择了Apipost!
· 通过 API 将Deepseek响应流式内容输出到前端
点击右上角即可分享
微信分享提示