构造User对象(/domain),如果有数据库的表可以直接使用IDEA反向生成实例的类。过程如下:
打开IDEA:View-ToolWindows-Database。添加数据来源,选择数据库类型,输入数据库的ip。如果不是默认Windows登陆数据库,则取消勾选然后填入用户名,密码。如果Test Connection是暗的,则看看左下角有没有提醒下载驱动,有的话就下载后Test。成功后就添加了。
在resource文件夹下新建文件“hibernate.cfg.xml”
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- Database connection settings -->
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<property name="connection.url">jdbc:sqlserver://xxx;database=xxx</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<!-- JDBC connection pool (use the built-in) -->
<!--
<property name="connection.pool_size">1</property>
-->
<!-- SQL dialect -->
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<!-- Enable Hibernate's automatic session context management -->
<property name="current_session_context_class">thread</property>
<!-- Echo all executed SQL to stdout -->
<property name="show_sql">true</property>
<!-- Drop and re-create the database schema on startup -->
<!--
<property name="hbm2ddl.auto">update</property>
-->
</session-factory>
</hibernate-configuration>
我这里用的是SQLServer,如果别的数据库,记得修改方言等属性。connection url在之前添加的数据库中可以找到。保存后在ToolWindows中就可以找到Persistence(没有添加Hibernate.cfg.xml前是没有的)。
右键Persistence窗口中的项目
选择数据源(也就是刚刚添加过的)然后选择要生成的表。
如图勾选。Generate Separate XML per Entity勾选的话每个生成的实例类都会同时生成一个相应的配置文件UserEntity.hbm.xml 文件。Generate Column Properties是利用标注的方式进行数据库表与实体类的属性的匹配。这里选择第二种方式。
生成的实例类如下
package com.changhe.Domain;
import javax.persistence.*;
import java.util.Objects;
@Entity
public class UserEntity {
private int id;
private String name;
private Integer age;
@Id
@Column(name = "id")
@GeneratedValue
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Basic
@Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Basic
@Column(name = "age")
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
UserEntity that = (UserEntity) o;
return id == that.id &&
Objects.equals(name, that.name) &&
Objects.equals(age, that.age);
}
@Override
public int hashCode() {
return Objects.hash(id, name, age);
}
}
自己写也行。
@Entity
public class User {
@Id
@GeneratedValue
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer age;
//getter setter 省略
}
接下来创建实体类接口
package com.changhe.Domain;
import org.springframework.data.jpa.repository.JpaRepository;
public interface UserRepository extends JpaRepository<UserEntity,Integer> {
}
这是最通用的接口。里面全是默认的方法。jpa可以不实现然后定义很多方法。比如想按名字查找则
public UserEntity findByName(String Name);
就可以用了。如果没有声明则用不了。
这样就可以直接对数据库操作了。
之后就是RESTFUL的操作。跟上一篇一样即可。主要的不同是jdbcTemplate和jpa的不同而已。对外接口一致。