一,环境
idea,jdk8,oracle
二,实现效果
三,步骤
1 新建一个springboot项目
file->new->project->Spring Initializr
next->填写自己的groupid等信息->next->选择web和jpa组件
next项目就新建成功了.
2 修改为自己的maven仓库(如果想使用默认的仓库也可以不修改)
file->settings
3 修改项目为sources,便于后面新建class,package文件(这一步可以等你新建类出问题的时候才做)
file->project structure->点击下面的sources
4 在pom.xml中引入oracle依赖(正常情况是不应该写version的,由于懒得去找springboot对应的版本了,就先这样,但很容易引起冲突)
com.oracle
ojdbc6
11.1.0.6.0
如果maven仓库中没有这个jar包请,去官网下载,并手动编译到本地仓库,编译命定如下
mvn install:install-file -Dfile=E:/ojdbc6.jar -DgroupId=com.oracle -DartifactId=ojdbc6 -Dversion=11.1.0.6.0 -Dpackaging=jar -DgeneratePom=true
5 配置数据连接 application.yml文件
#配置数据库连接
spring:
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@127.0.0.1:1521:orcl
username: xiaobai
password: 1
jpa:
hibernate:
ddl-auto: update
show-sql: true
#配置端口号
server:
port: 8080
6 编写dao,service,controller层代码
EmpDao.java
package com.yxf.springboothibernate.dao;
import com.yxf.springboothibernate.model.Emp;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
@Repository
public interface EmpDao extends JpaRepository {
@Query("select t from Emp t where t.guid = :id")//无法注入empdao
public Emp findUserById(@Param("id") String id);//参数需要用@param
}
EmpService.java与EmpServiceImpl.java
package com.yxf.springboothibernate.service;
import com.yxf.springboothibernate.model.Emp;
public interface EmpService {
Emp findUserById(String id);
}
package com.yxf.springboothibernate.service;
import com.yxf.springboothibernate.dao.EmpDao;
import com.yxf.springboothibernate.model.Emp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class EmpServiceImpl implements EmpService {
@Autowired
private EmpDao empDao;
@Override
public Emp findUserById(String id) {
return empDao.findUserById(id);
}
}
Emp.java
package com.yxf.springboothibernate.model;
import javax.persistence.Entity;
import javax.persistence.Id;
@Entity
//@Table(name="emp")
public class Emp /*implements Serializable */{
@Id
//@GeneratedValue
private String guid;
private String name;
private double sal;
private String deno;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getSal() {
return sal;
}
public void setSal(double sal) {
this.sal = sal;
}
public String getDeno() {
return deno;
}
public void setDeno(String deno) {
this.deno = deno;
}
public String getGuid() {
return guid;
}
public void setGuid(String guid) {
this.guid = guid;
}
@Override
public String toString() {
return "Emp{" +
"guid='" + guid + '\'' +
", name='" + name + '\'' +
", sal=" + sal +
", deno='" + deno + '\'' +
'}';
}
}
EmpController.java
package com.yxf.springboothibernate.controller;
import com.yxf.springboothibernate.model.Emp;
import com.yxf.springboothibernate.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class EmpController {
@Autowired
private EmpService empService;
@ResponseBody
@RequestMapping("/query")
public String queryEmp(){
Emp emp = empService.findUserById("F003C33CB34349AAA0A860CC9A2D208F");
System.out.println(emp.toString());
return emp.toString();
}
}
7 运行主文件
四 问题总结
在写这个demo的过程中也遇到过好几个问题,由于对hibernate的知识有些遗忘了
1 启动的时候出现如下错误
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'empDao':
明显是dao这个bean对象创建错误,但找了很久才发现是我的查询语句里面的表写的是emp,我数据库里面是emp,java对象是Emp
此时这里应该是使用Emp不然就会出现以上错误
2 访问的时候报以下错误
IllegalStateException: For queries with named parameters you need to use provide names for method param
需要在传的参数加上@Param
补充
数据库表字段
文件目录