项目场景:
此文章仅是记录hibernate的一些xml配置
、
一些XML模板:
hibernate.xml
<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.url">jdbc:mysql://localhost:3306/library</property>
<property name="connection.driver_class" >com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.username">root</property>
<!-- 数据库的登陆密码 -->
<property name="hibernate.connection.password">h6708231</property>
<!-- 指定连接数据库的编码 -->
<property name="connection.characterEncoding">utf8</property>
<!-- 指定数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
<!-- 显示Hibernate持久化操作所生成的SQL -->
<property name="show_sql">true</property>
<!-- 将SQL脚本进行格式化后再输出 -->
<property name="format_sql">true</property>
<!-- 指定自动生成数据表的策略 -->
<property name="hbm2ddl.auto">update</property>
<!-- DB schema will be updated if needed -->
<!-- 指定表的映射 -->
<mapping resource="user.hbm.xml"/>
</session-factory>
</hibernate-configuration>
user.hbm.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>
<class name="user" table="user" schema="hibernate">
<id name="username" column="username"/>
<property name="password" column="password"/>
<property name="type" column="type"/>
<property name="name" column="name"/>
</class>
</hibernate-mapping>
test.java
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
public class HibernateTest {
public static void main(String[] args) {
// 加载Hibernate默认配置文件
Configuration configuration = new Configuration().configure();
// 用Configuration创建SessionFactory
SessionFactory factory = configuration.buildSessionFactory();
// 创建Session
Session session = factory.openSession();
// 开启事务
Transaction transaction = session.beginTransaction();
// 实例化持久化类
user u = new user();
u.setType("1");
u.setUsername("123456");
u.setPassword("123456");
u.setName("hhh");
// 保存
session.save(u);
// 提交事务
transaction.commit();
// 关闭Session,释放资源
session.close();
factory.close();
}
}
idea maven的pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>test_hibernate</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>15</maven.compiler.source>
<maven.compiler.target>15</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.11.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
</project>
maven的setting.xml
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0
http://maven.apache.org/xsd/settings-1.0.0.xsd">
<!--本地仓库目录,注意此处目录应该与上面的设置Local Repository一致-->
<localRepository>C:/Users/86131/.m2/repository</localRepository>
<mirrors>
<mirror>
<!--该镜像的id-->
<id>nexus-aliyun</id>
<!--该镜像用来取代的远程仓库,central是中央仓库的id-->
<mirrorOf>central</mirrorOf>
<name>Nexus aliyun</name>
<!--该镜像的仓库地址,这里是用的阿里的仓库-->
<url>http://maven.aliyun.com/nexus/content/groups/public</url>
</mirror>
</mirrors>
</settings>