标签:工作 支持 img 关系 t_sql 描述 cal util --
<dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>5.4.4.Final</version> </dependency>
CREATE DATABASE hibernate; USE hibernate; CREATE TABLE USER ( id INT PRIMARY KEY auto_increment, username VARCHAR ( 50 ), `password` VARCHAR ( 50 ) );
<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency>
package com.sunxiaping.hibernateDemo.domain; public class User { private Integer id; private String username; private String password; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
<?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.sunxiaping.hibernateDemo.domain.User" table="user" schema="hibernate"> <id name="id"> <column name="id" /> <!-- 使用本地生成策略 --> <generator class="native"/> </id> <property name="username"> <column name="username"/> </property> <property name="password"> <column name="password"/> </property> </class> </hibernate-mapping>
<?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"> <![CDATA[ jdbc:mysql://localhost:3306/hibernate?useUnicode=true&characterEncoding=utf-8&useSSL=false ]]> </property> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.username">root</property> <property name="connection.password">123456</property> <!--是否显示SQL--> <property name="show_sql">true</property> <!--是否格式化SQL--> <property name="format_sql">true</property> <!--是否自动提交事务--> <property name="connection.autocommit">true</property> <!--配置JavaBean和表的映射关系--> <mapping resource="com/sunxiaping/hibernateDemo/domain/User.hbm.xml"/> </session-factory> </hibernate-configuration>
package com.sunxiaping.hibernateDemo; import com.sunxiaping.hibernateDemo.domain.User; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; import org.junit.Test; public class HibernateTest { @Test public void test() { //加载hibernate.cfg.xml文件 Configuration configuration = new Configuration().configure(); //创建会话创建工厂 SessionFactory sessionFactory = configuration.buildSessionFactory(); //创建会话 Session session = sessionFactory.openSession(); //开启事务 Transaction transaction = session.beginTransaction(); User user = new User(); user.setUsername("张三"); user.setPassword("123456"); session.save(user); //提交事务 transaction.commit(); //关闭会话 session.close(); //关闭会话工厂 sessionFactory.close(); } }
标签:工作 支持 img 关系 t_sql 描述 cal util --
原文地址:https://www.cnblogs.com/xuweiweiwoaini/p/11465118.html