写了一个简单的项目,里面有两个实体类,一个叫users类,一个叫volunteer类,实现在前台对它们的增删改查。
而后我对它们进行表连接时报错了。。错误如下:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: com.example.demo.pojo.Users, at table: volunteer, for columns: [org.hibernate.mapping.Column(users)]
我到网上找了很多方法,有的说是注释只能在属性上,或者是在get方法上使用,不能混着来用,但是我并没有犯这个错误,我也是初学这方面的知识,现在一筹莫展,求各位大佬能帮忙看看,如需更多代码我马上附上。
下面是代码。
结构:
users类
package com.example.demo.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Users {
@Id
@GeneratedValue
private Integer uSId;
private String uName;
private String uPwd;
private Integer uAge;
private String uSex;
private Integer uPhone;
private String uNum;
private String uEmail;
public Integer getuSId() {
return uSId;
}
public void setuSId(Integer uSId) {
this.uSId = uSId;
}
public String getuName() {
return uName;
}
public void setuName(String uName) {
this.uName = uName;
}
public String getuPwd() {
return uPwd;
}
public void setuPwd(String uPwd) {
this.uPwd = uPwd;
}
public Integer getuAge() {
return uAge;
}
public void setuAge(Integer uAge) {
this.uAge = uAge;
}
public String getuSex() {
return uSex;
}
public void setuSex(String uSex) {
this.uSex = uSex;
}
public Integer getuPhone() {
return uPhone;
}
public void setuPhone(Integer uPhone) {
this.uPhone = uPhone;
}
public String getuNum() {
return uNum;
}
public void setuNum(String uNum) {
this.uNum = uNum;
}
public String getuEmail() {
return uEmail;
}
public void setuEmail(String uEmail) {
this.uEmail = uEmail;
}
}
volunteer类
package com.example.demo.pojo;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Volunteer {
@Id
@GeneratedValue
private Integer vId;
private Integer mId;
private Users users;
public Integer getvId() {
return vId;
}
public void setvId(Integer vId) {
this.vId = vId;
}
public Integer getmId() {
return mId;
}
public void setmId(Integer mId) {
this.mId = mId;
}
public Users getUsers() {
return users;
}
public void setUsers(Users users) {
this.users = users;
}
}
application.yml
spring:
thymeleaf:
prefix: classpath:/templates/
datasource:
driver-class-name: oracle.jdbc.driver.OracleDriver
url: jdbc:oracle:thin:@localhost:1521:orcl
username: c##match
password: 123456
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.forrest</groupId>
<artifactId>matcharrange</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>matcharrange</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>12</java.version>
</properties>
<dependencies>
<!--开发web项目相关依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--springboot单元测试-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0.1.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
usermapper类
package com.example.demo.dao;
import com.example.demo.pojo.Users;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface UserMapper {
@Select("select * from users order by uSId")
List<Users> findAll();
@Insert("INSERT INTO users VALUES(#{uSId},#{uName},#{uPwd},#{uAge},#{uSex},#{uPhone},#{uNum},#{uEmail})")//自增id去掉?了
Integer addUser(Users users);
@Delete("delete from users where usid=#{uSId}")
Integer deleteUser(Integer uSId);
@Update("update users set uName=#{uName},uPwd=#{uPwd},uAge=#{uAge},uSex=#{uSex},uPhone=#{uPhone},uNum=#{uNum},uEmail=#{uEmail} where uSId=#{uSId}")
Integer updateUser(Users users);
@Select("select * from users where usid=#{uSId}")
Users queryById(Integer uSId);
@Select("select Max(usid) from users")
Integer getMaxId();
@Select("select d.* from (select u.*,rownum r from users u ORDER BY usId) d where r>(#{index}-1)*#{size} and r<= #{index}*#{size}")
List<Users> findAllByPage(@Param("index") Integer index, @Param("size") Integer size);
@Select("select count(usid) from users")
Integer countInfo();
}
volunteermapper类
package com.example.demo.dao;
import com.example.demo.pojo.Volunteer;
import org.apache.ibatis.annotations.*;
import java.util.List;
@Mapper
public interface VolunteerMapper {
@Select("select * from volunteer order by vid")
List<Volunteer> findAll();
@Insert("INSERT INTO volunteer VALUES(#{vId},#{mId},#{uSId})")//自增id去掉?了
Integer addVolunteer(Volunteer volunteer);
@Delete("delete from volunteer where vId=#{vId}")
Integer deleteVolunteer(Integer vId);
@Update("update volunteer set mId=#{mId},uSId=#{uSId} where vId=#{vId}")
Integer updateVolunteer(Volunteer volunteer);
@Select("select * from volunteer where vid=#{vId}")
Volunteer queryById(Integer vId);
@Select("select Max(vId) from volunteer")
Integer getMaxId();
@Select("select d.* from (select v.*,rownum r from volunteer v ORDER BY vId) d where r>(#{index}-1)*#{size} and r<= #{index}*#{size}")
@Results({
@Result(property = "users",column = "uSId",one = @One(select = "com.forrest.matcharrange.dao.UserMapper.queryById"))
})
List<Volunteer> findAllByPage(@Param("index") Integer index, @Param("size") Integer size);
@Select("select count(vId) from volunteer")
Integer countInfo();
}
demoapplication
package com.example.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.scheduling.annotation.EnableScheduling;
@SuppressWarnings("SpringBootApplicationSetup")
@ComponentScan(basePackages = {"com.example.demo.controller","com.example.demo.service"})
@MapperScan(basePackages = {"com.example.demo.dao"})
@EnableScheduling
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
这里是表创建
我是个大学生,也是第一次来csdn发文,最近想写项目参加比赛的,实在走投无路了,大哥们救救孩子吧。