gpt4 book ai didi

java - 将 JPA Spring boot 与 SQL Server 结合使用

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:24 25 4
gpt4 key购买 nike

我想在 Spring boot 中通过 STS 将 JPA 与 SQL Server 一起使用这是我的 table :

enter image description here

MAVEN

<dependencies>

<dependency>
<groupId>com.hynnet</groupId>
<artifactId>sqljdbc-chs</artifactId>
<version>4.0.2206.100</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</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.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

应用程序属性

spring.datasource.driver-class-  name=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=quanlybanhang
spring.datasource.username=sa
spring.datasource.password=1
spring.jpa.hibernate.ddl-auto=create
spring.datasource.initialize=true
spring.jpa.database=SQL_SERVER

模型.帐户.类

  @Entity
@Table(name="taikhoan",uniqueConstraints=@UniqueConstraint(columnNames = { "tendangnhap" }) )
public class Account {
@Id @GeneratedValue(strategy=GenerationType.AUTO)
private int id;
@NotNull

private String tendangnhap;
@NotNull
private String matkhau;
public Account(String tendangnhap, String matkhau) {
super();
this.tendangnhap = tendangnhap;
this.matkhau = matkhau;
}
public Account() {
super();
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTendangnhap() {
return tendangnhap;
}

public void setTendangnhap(String tendangnhap) {
this.tendangnhap = tendangnhap;
}
public String getMatkhau() {
return matkhau;
}
public void setMatkhau(String matkhau) {
this.matkhau = matkhau;
}
}

接口(interface)AccountDAO

public interface AccountDAO extends JpaRepository<Account, Integer>{

}

ServiceAccount.class

@Service
public class ServerAccount {
@Autowired
AccountDAO server;
public void them(Account acc){
server.save(acc);
}
public List<Account> lietke(){

return server.findAll();
}
}

ServicesAccount.class

@Service
public class ServerAccount {
@Autowired
AccountDAO server;

Account acc=new Account("khang", "1");

public void addAccount(){
server.save(acc);
}


public List<Account> lietke(){

return server.findAll();
}

}

我在 Controller 中调用了方法addAccount(),这是我得到的异常

“java.lang.NoClassDefFoundError:org/springframework/orm/jpa/support/PersistenceAnnotationBeanPostProcessor”

“由以下原因引起:java.lang.ClassNotFoundException:org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor”

请帮我解决这个异常。谢谢!!!

最佳答案

从你错过的依赖项中(检查你使用的java版本):

 <dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.5.2.jre8-preview</version>
<scope>test</scope>
</dependency>

并进入应用程序属性更正此行:

spring.datasource.driver-class-  name=com.microsoft.sqlserver.jdbc.SQLServerDriver

spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver
spring.jpa.hibernate.dialect=org.hibernate.dialect.SQLServer2012Dialect

关于java - 将 JPA Spring boot 与 SQL Server 结合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40005737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com