hibernate快速搭建开发环境
程序员文章站
2024-03-23 13:12:10
...
1.ecplise集成hibernate插件快速方便hibernate的开发
Hibernate Tools是用于Hibernate的工具集,实现为Eclipse插件的集成套件 ,以及用于集成到构建周期中的统一Ant任务。Hibernate Tools是JBoss Tools的核心组件, 因此也是JBoss Developer Studio的一部分。
如果创建hibernate.cfg.xml失败在选择版本的时候降低版本选择就可以
在ecplise市场中进行搜索在线安装
2.maven搭建,引入pom
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.0.7.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-entitymanager -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.7.Final</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.12</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-log4j12 -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.38</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.20</version>
<!-- <scope>provided</scope> -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>hibernate_project</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_war_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
3.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="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql://192.168.174.135/hibernate</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root3306</property>
<!-- 配置hibernate信息 -->
<!-- 打印sql,格式化sql -->
<property name="hibernate.show_sql">true</property>
<property name="hibernate.format_sql">true</property>
<!-- hibernate创建表 ,有则更新,无则创建-->
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- sql方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<!-- 映射文件 -->
<mapping resource="com/cn/xulei/hibernate/entity/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
4.创建实体
public class User {
private int id;
private String uname;
private String email;
private int age;
private int sex;
}
创建user.hbm.xml
<?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 2019-10-12 22:50:04 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="com.cn.xulei.hibernate.entity.User" table="USER">
<id name="id" type="int">
<column name="ID" />
<generator class="native" />
</id>
<property name="uname" type="java.lang.String">
<column name="UNAME" />
</property>
<property name="email" type="java.lang.String">
<column name="EMAIL" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<property name="sex" type="int">
<column name="SEX" />
</property>
</class>
</hibernate-mapping>
5.测试代码
package com.cn.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.Test;
import it.com.cn.hibernate.entity.User;
public class TestHibernate {
@Test
public void testAdd() {
//src目录下加载配置文件
Configuration cfg = new Configuration();
cfg.configure();
/**
* 读取hibernate核心配置文件,创建sessionfactory
* 根据映射文件创建数据库表
*/
SessionFactory sessionFactory = cfg.buildSessionFactory();
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
User user = new User(19,"杨过","aaa@qq.com",19,1);
session.save(user);
transaction.commit();
session.close();
sessionFactory.close();
}
}
上一篇: Hibernate 5.3(一)
下一篇: ThreadLocal