簡單使用hibernate(idea中使用)
阿新 • • 發佈:2018-10-13
his config mariadb connect url true ride .config clas
首先創建一個maven項目
創建成功後,進行創建數據庫的表
CREATE TABLE BOOK( ID INT AUTO_INCREMENT PRIMARY KEY, NAME VARCHAR(20), NUMBER int ); insert into BOOK(NAME,NUMBER) VALUES (‘Java程序設計‘,10), (‘數據結構‘,10), (‘設計模式‘,10)
然後再剛剛創建的項目裏創建一個類,該類名稱、字段要與數據庫的一樣
必須註意使用的註釋
package com.wbg.entity; import javax.persistence.Entity; importjavax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class Book { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) Long id; String name; int number; public Book() { } public Book(Long id, String name, intnumber) { this.id = id; this.name = name; this.number = number; } public Long getBook_id() { return id; } public void setBook_id(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) {this.name = name; } public int getNumber() { return number; } public void setNumber(int number) { this.number = number; } @Override public String toString() { return "Book{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", number=" + number + ‘}‘; } }
進行在pom.xml文件中添加依賴
<dependencies> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.3.6.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.mariadb.jdbc/mariadb-java-client --> <dependency> <groupId>org.mariadb.jdbc</groupId> <artifactId>mariadb-java-client</artifactId> <version>2.3.0</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.11</version> <scope>test</scope> </dependency> </dependencies>
在resources資源文件夾目錄下面創建一個hibernate.xml配置文件
<?xml version=‘1.0‘ encoding=‘UTF-8‘?> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <!--數據庫驅動--> <property name="hibernate.connection.driver_class">org.mariadb.jdbc.Driver</property> <!--連接--> <property name="hibernate.connection.url">jdbc:mariadb://localhost:3306/stu</property> <!--用戶名--> <property name="hibernate.connection.username">root</property> <!--密碼--> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MariaDB10Dialect</property> <property name="hibernate.show_sql">true</property> <mapping class="com.wbg.entity.Book"/> </session-factory> </hibernate-configuration>
測試成功
package com.wbg; import com.wbg.entity.Book; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.cfg.Configuration; public class test { public static void main(String[] args) { Configuration configuration=new Configuration(); Configuration configure=configuration.configure("hibernate.xml"); SessionFactory sessionFactory=configuration.buildSessionFactory(); Session session=sessionFactory.openSession(); Book book= session.get(Book.class,2L); System.out.println(book.toString()); } }
簡單使用hibernate(idea中使用)