java 对SQL Server使用JPA Sping Boot

2cmtqfgy  于 2022-11-20  发布在  Java
关注(0)|答案(1)|浏览(165)

我想在Sping Boot 中使用JPA和STS的SQL Server这是我的表:

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>

application.properties

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

Model.Account.class

@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;
}   
}

接口帐户DAO

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();    
    }

}

我在控制器中调用了方法addAccount(),这是我得到的异常
“未找到类定义错误:支持/持久性注解Bean后处理器”
“导致的原因:java.lang.ClassNotFoundException:支持持久性注解Bean后处理器”
请帮助我修复此异常。谢谢!!!

mzillmmw

mzillmmw1#

从您的依赖项中,您遗漏了(检查您使用的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

相关问题