框架
-2为带有注解的版本,但是总体上是差不多的
工程框架
teacher类:
package cn.edu.ldu.entity; import javax.persistence.*; @Entity @Table public class Teacher { private int id; private String name; private String title; @Id @GeneratedValue(strategy=GenerationType.AUTO) public int getId() { return id; } public void setId(int id) { this.id = id; } //@Column(name = "name",length = 255) public String getName() { return name; } public void setName(String name) { this.name = name; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } }
hibernate.cfg.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="url"> jdbc:mysql://localhost:3306/hibernate?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC </property> <!-- ?characterEncoding=utf-8&useSSL=false&serverTimezone=UTC--> <property name="driverClassName"> com.mysql.cj.jdbc.Driver </property> <property name="username">root</property><!--connection.--> <property name="password">123456</property> <!-- DB schema will be updated if needed --> <property name="connection.provider_class"> com.alibaba.druid.support.hibernate.DruidConnectionProvider </property> <property name="filters">stat</property> <property name="hibernate.current_session_context_class">thread</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="hbm2ddl.auto">create</property> <property name="hibernate.hibernate.hbm2ddl.auto">update</property> <!-- <property name="hibernate.hibernate.hbm2ddl.auto">validate</property>--> <!-- validate--> <!-- 进行修改--> <property name="hibernate.dialect">org.hibernate.dialect.MySQL8Dialect</property> <!-- 加入映射--> <mapping class="cn.edu.ldu.entity.Teacher"/> </session-factory> </hibernate-configuration>
log4j.properties:
# # Hibernate, Relational Persistence for Idiomatic Java # # License: GNU Lesser General Public License (LGPL), version 2.1 or later. # See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. # ### direct log messages to stdout ### log4j.appender.stdout=org.apache.log4j.ConsoleAppender log4j.appender.stdout.Target=System.out log4j.appender.stdout.layout=org.apache.log4j.PatternLayout log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### direct messages to file hibernate.log ### #log4j.appender.file=org.apache.log4j.FileAppender #log4j.appender.file.File=hibernate.log #log4j.appender.file.layout=org.apache.log4j.PatternLayout #log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n ### set log levels - for more verbose logging change 'info' to 'debug' ### log4j.rootLogger=warn, stdout #log4j.logger.org.hibernate=info log4j.logger.org.hibernate=debug ### log HQL query parser activity #log4j.logger.org.hibernate.hql.ast.AST=debug ### log just the SQL ## 2021-05-10取消注释 log4j.logger.org.hibernate.SQL=debug ### log JDBC bind parameters ### ## log4j.logger.org.hibernate.type=info log4j.logger.org.hibernate.type=debug ### log schema export/update ### log4j.logger.org.hibernate.tool.hbm2ddl=debug ### log HQL parse trees #log4j.logger.org.hibernate.hql=debug ### log cache activity ### #log4j.logger.org.hibernate.cache=debug ### log transaction activity #log4j.logger.org.hibernate.transaction=debug ### log JDBC resource acquisition #log4j.logger.org.hibernate.jdbc=debug ### enable the following line if you want to track down connection ### ### leakages when using DriverManagerConnectionProvider ### #log4j.logger.org.hibernate.connection.DriverManagerConnectionProvider=trace
teacherTest类
package cn.edu.ldu.entity; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class TeacherTest { public static void main(String[] args) { Teacher t = new Teacher(); t.setName("Tao"); t.setTitle("教授"); Configuration cfg = new Configuration().configure("/hibernate.cfg.xml"); SessionFactory sf = cfg.buildSessionFactory(); Session session = sf.openSession(); Transaction trans = session.beginTransaction(); session.save(t); trans.commit(); session.close(); sf.close(); System.out.println("save teacher ok"); } }
文章知识点与官方知识档案匹配,可进一步学习相关知识
Java技能树使用JDBC操作数据库JDBC概述24568 人正在系统学习中