This article introduces the steps to build a hibernate 5.0 environment under eclipse, and shares it with you. The details are as follows:
2. Install the hibernate plug-in
Open eclipse, click help-->eclipse marketplace, enter: Hibernate Tools, then click the Goa button to find JBoss Tools
Click install to install
Select Hibernate Tools as shown in the figure and click Confrm to install. Restart eclipse after installation is completed.
3. Create a project
1. Create a new project hibernateDemo and create a lib folder under the project. Open the directory of the jar package, import the jar package under lib/required and database, add to build path
Create a new file under src
Click next, default file name, click next, configure database information as shown in the figure
Select UTF-8 encoding method, click finish, and the generated hibernate.cfg.xml configuration file content is as follows
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">a123</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <property name="hibernate.connection.username">sherman</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> </session-factory></hibernate-configuration>
Note that remove the name attribute of <session-factory name ="MySQL" >, otherwise an org.hibernate.engine.jndi.JndiException is reported, and some configurations are added to the file, as shown in the figure:
<?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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.password">a123</property> <property name="hibernate.connection.url">jdbc:mysql://localhost:3306/tb_test</property> <property name="hibernate.connection.username">sherman</property> <!-- Configuring database dialect--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property> <!-- Print sql statement on the console--> <property name="show_sql">true</property> <!-- Format sql --> <property name="format_sql">true</property> <!--Update the database according to the configuration at startup --> <property name="hibernate.hbm2ddl.auto">update</property> <!-- Configure the number of connections to the connection pool --> <property name="connection.pool_size">20</property> <!-- Register the entity mapping class --> <mapping/> </session-factory></hibernate-configuration>
Create a new package com.gdut.app.entity under src, and store the persistent class News, and the News class code is as follows
package com.gdut.app.entity;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.GenerationType;import javax.persistence.Id;import javax.persistence.Table;@Entity@Table(name="NEWS_INFO")public class News {@Id@GeneratedValue(strategy=GenerationType.IDENTITY)private Integer id;private String title;private String content;public News() {}public News(Integer id, String title, String content) { this.id = id; this.title = title; this.content = content;}public Integer getId() { return id;}public void setId(Integer id) { this.id = id;}public String getTitle() { return title;}public void setTitle(String title) { this.title = title;}public String getContent() { return content;}public void setContent(String content) { this.content = content;}@Overridepublic String toString() { return "News [id=" + id + ",, content=" + content + "]";}}Write test classes:
package com.gdut.app.entity;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.junit.Test;public class BeanTest { @Test public void beanTest() {// final StandardServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder()// .configure("hibernate.cfg.xml").build();// // SessionFactory sf = new MetadataSources(serviceRegistry).buildMetadata().buildSessionFactory(); // Both methods can be obtained SessionFactory Configuration cfg = new Configuration().configure(); SessionFactory sf = cfg.buildSessionFactory(); Session sess = sf.openSession(); Transaction transaction = sess.beginTransaction(); News n = new News(); n.setContent("graduate in Guangxi"); n.setTitle("graduation season"); sess.save(n); transaction.commit(); sess.close(); }}Successfully tested
Or by mapping files
Resume a News.hbm.xml mapping configuration file under com.gdut.app.entity package, modify the class attribute of genarator to active
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><!-- Generated 2018-5-22 23:45:23 by Hibernate Tools 3.5.0.Final --><hibernate-mapping> <class name="com.gdut.app.entity.News" table="NEWS"> <id name="id" type="java.lang.Integer"> <column name="ID" /> <generator/> </id> <property name="title" type="java.lang.String"> <column name="TITLE" /> </property> <property name="content" type="java.lang.String"> <column name="CONTENT" /> </property> </class></hibernate-mapping>
Configure in hibernate.cfg.xml
<mapping resource="com/gdut/app/entity/News.hbm.xml"/>
The test verification was successful.
The entire project architecture is shown in the figure:
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.