<!-- hibernate依赖 --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.5.5.Final</version> </dependency> <!-- mysql依赖 --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.28</version> </dependency>
<?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 name="foo"> <!-- 初始化JDBC连接 --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url">jdbc:mysql:///hibernate</property> //数据库名 ///三个斜杠中间//(这个是默认端口号)(localhost:3306)/ <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- 方言 --> <property name="dialect">org.hibernate.dialect.MySQLDialect</property> <!-- 关联对象配置文件 --> <mapping resource="com/uestc/User.hbm.xml"/> </session-factory> </hibernate-configuration>
public class User implements Serializable { private Integer id; private String name; private String pwd; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPwd() { return pwd; } public void setPwd(String pwd) { this.pwd = pwd; } @Override public String toString() { return "User{" + "id=" + id + ", name='" + name + '\'' + ", pwd='" + pwd + '\'' + '}'; } }
<?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="com.uestc.User" table="user"> //如果table="user",column="name",column="pwd"等字样变红,需要加入database,在点击Tool Windows下面的Persistence,在再爆红的字符串加上alt+enter <!-- 主键自增策略 --> <id name="id"> <generator class="native"/> </id> <!-- 属性映射 --> <property name="name" column="name"/> <property name="pwd" column="pwd"/> </class> </hibernate-mapping>
public class HibernateTest { @Test public void testInit(){ StandardServiceRegistry registry = null; SessionFactory sessionFactory = null; Session session = null; Transaction tx = null; try{ //5.X版本的写法 //初始化注册服务对象 registry = new StandardServiceRegistryBuilder() .configure() // 默认加载hibernate.cfg.xml配置文件,如果配置文件名称被修改,.configure("修改的名称") .build(); //从元信息获取Session工厂 sessionFactory = new MetadataSources(registry) .buildMetadata() .buildSessionFactory(); //从工厂创建Session连接 session = sessionFactory.openSession(); //开启事务 tx = session.beginTransaction(); //创建实例 User user = new User(); user.setName("zhangsan"); user.setPwd("123"); session.save(user); //提交事务 tx.commit(); }catch (Exception e){ //回滚事务 tx.rollback(); }finally { if(session != null && session.isOpen()) session.close(); } } }
<build> <!-- 读取配置文件 --> <resources> <resource> <directory>src/main/resources</directory> </resource> <resource> <directory>src/main/java</directory> <includes> <include>**/*.xml</include> </includes> <filtering>false</filtering> </resource> </resources> </build>