IDEA Hibernate入門配置
阿新 • • 發佈:2019-02-18
首先新建一個專案,我是用的最新版5.3.2,下載jar包要等一會兒,然後取個自己的專案名,我這裡就叫demo吧
我這裡選擇以maven方式來構建,在專案上右擊,選擇Add Framework Support
在之後的選擇Maven
之後在專案的右下角彈出一個提示框,選擇自動匯入
然後在pom.xml檔案下新增
<dependencies> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <dependency> <groupId>org.testng</groupId> <artifactId>testng</artifactId> <version>6.14.3</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.activation/activation --> <dependency> <groupId>javax.activation</groupId> <artifactId>activation</artifactId> <version>1.1.1</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.xml.bind/jaxb-api --> <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-core --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-core</artifactId> <version>2.3.0.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.sun.xml.bind/jaxb-impl --> <dependency> <groupId>com.sun.xml.bind</groupId> <artifactId>jaxb-impl</artifactId> <version>2.3.0.1</version> </dependency> </dependencies>
然後在resources,新建一個xml檔案,取名叫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> <!-- 資料庫連線配置 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property> <property name="connection.username">root</property> <property name="connection.password">1</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- 控制檯顯示sql --> <property name="show_sql">true</property> <!-- 自動更新表結構 --> <property name="hbm2ddl.auto">update</property> </session-factory> </hibernate-configuration>
然後在介面的右上角點選彈出的提示框,讓這個檔案成為hibernate的配置檔案,點選專案結構按鈕,就可以看到剛剛新增的配置檔案了
還是在resources資料夾下,新建一個xml檔案,名字叫Student.hbn.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping package="mode"> <class name="mode.Student" table="t_student"> <id name="id" column="stuId" > <generator class="native" /> </id> <property name="name" /> </class> </hibernate-mapping>
先去資料庫新建一個hibernate資料庫,我這裡就直接叫的hibernate,先新建一個兩個包,一個叫model,另一個叫util
然後在model包下新建一個實體類,名叫Student,並生成get、set方法,以及toString方法
public class Student {
private long id;
private String name;
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
在util包下新建一個工具類,名叫HibernateUtil
public class HibernateUtil {
private static final SessionFactory SESSION_FACTORY = buildSessionFactory();
private static SessionFactory buildSessionFactory(){
//建立服務註冊物件
ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().configure().build();
//建立會話工廠物件
return new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory();
}
public static SessionFactory getSessionFactory(){
return SESSION_FACTORY;
}
}
在任何一個類下按Alt+Enter,選擇Create Test
然後在彈出的對話方塊中選擇用TestNG來測試
然後就可以新建一個Test類了,名字就用預設就行了,然後在裡面新增如下的程式碼
package model;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import util.HibernateUtil;
public class StudentTest {
private SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
private Session session;
@BeforeMethod
public void setUp() {
session = sessionFactory.openSession();
session.beginTransaction();
}
@AfterMethod
public void tearDown() {
session.getTransaction().commit();
session.close();
}
@Test
public void add(){
Student student = new Student();
student.setName("張三");
session.save(student);
}
}
然後點選左邊的執行按鈕執行就可以看到資料庫裡有表了