将 JPA Spring 引导与 SQL Server 一起使用
Posted
技术标签:
【中文标题】将 JPA Spring 引导与 SQL Server 一起使用【英文标题】:Use JPA Spring boot with SQL Server 【发布时间】:2017-02-21 16:14:27 【问题描述】:我想在 Spring Boot 中通过 STS 使用 JPA 和 SQL Server 这是我的桌子:
行家
<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;
接口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”
请帮我解决这个异常。谢谢!!!
【问题讨论】:
你的 pom 有没有从您错过的依赖项中(检查您使用的 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
【讨论】:
属性 spring.jpa.hibernate.dialect 已更新为 spring.jpa.properties.hibernate.dialect以上是关于将 JPA Spring 引导与 SQL Server 一起使用的主要内容,如果未能解决你的问题,请参考以下文章
Spring:JPA 将原生 SQL 转换为非实体 pojo
如何将多个日期之间的搜索与 Spring Data JPA 的 CrudRepository 结合起来?
如何通过 Sum SQL 与 Spring Data JPA 一起使用组?
在 JPA/Hibernate 中使用 @OnetoMany 的实体中不存在时从数据库中删除子记录(Spring 引导应用程序)