主要依赖包
<!-- https://mvnrepository.com/artifact/org.hibernate/hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>3.6.10.Final</version> </dependency> <!-- https://mvnrepository.com/artifact/org.javassist/javassist --> <dependency> <groupId>org.javassist</groupId> <artifactId>javassist</artifactId> <version>3.20.0-GA</version> </dependency>User对象
public class User { public int id; public String name; public String password; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }表
CREATE TABLE `t_user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(20) DEFAULT NULL, `password` varchar(20) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8hibernate配置文件
<?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 元素用于配置Hibernate中的属性 键:值 --> <!-- hibernate.connection.driver_class : 连接数据库的驱动 --> <property name="hibernate.connection.driver_class"> com.mysql.jdbc.Driver </property> <!-- hibernate.connection.username : 连接数据库的用户名 --> <property name="hibernate.connection.username">root</property> <!-- hibernate.connection.password : 连接数据库的密码 --> <property name="hibernate.connection.password">xxxx</property> <!-- hibernate.connection.url : 连接数据库的地址,路径 --> <property name="hibernate.connection.url"> jdbc:mysql://localhost:3306/xxx </property> <!--向控制台打印sql语句--> <property name="show_sql">true</property> <!--打印sql语句,会先将sql语句格式化--> <property name="format_sql">true</property> <!--是否自动生成表结构,如果把表删除,会自动创建表--> <property name="hbm2ddl.auto">update</property> <!--事务自动提交--> <property name="hibernate.connection.autocommit">true</property> <!--引入orm映射文件--> <mapping resource="User.hbm.xml"/> </session-factory> </hibernate-configuration>映射文件
<?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:实体完整类名,table:表名--> <class name="hibernate.User" table="t_user"> <id name="id" column="id"> <!--主键生成策略,naive:有数据库维护主键--> <generator class="native"></generator> </id> <!--实体中属性与表中列对应--> <property name="name" column="name"></property> <property name="password" column="password"></property> </class> </hibernate-mapping>测试类
public class Test { @org.junit.jupiter.api.Test public void demo(){ //读取配置文件 Configuration configuration = new Configuration().configure(); //根据配置获取factory SessionFactory sessionFactory = configuration.buildSessionFactory(); Session session = sessionFactory.openSession(); User user = new User(); user.setName("tom"); user.setPassword("123"); session.save(user); session.close(); sessionFactory.close(); } } Mon Mar 13 09:09:47 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification. Hibernate: insert into t_user (name, password) values (?, ?) Process finished with exit code 0