Hibernate4学习(一)入门
程序员文章站
2022-04-14 09:18:23
...
笔者的第一个hibernate框架应用入门例子 hibernate版本号是 4.3.11
-
hibernate核心配置文件hibernate.cfg.xml, 笔者将配置文件放在conf.hibernate包下,配置内容如下
<?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> <!-- Database connection settings --> <property name="connection.driver_class">com.mysql.jdbc.Driver</property> <property name="connection.url"> jdbc:mysql://127.0.0.1:3306/lian?autoReconnect=true&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <!-- JDBC connection pool (use the built-in) --> <property name="connection.pool_size">5</property> <!-- <property name="javax.persistence.validation.mode">none</property> --> <!-- SQL dialect --> <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property> <!-- Enable Hibernate's automatic session context management --> <property name="current_session_context_class">thread</property> <!-- Disable the second-level cache --> <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property> <!-- Echo all executed SQL to stdout --> <property name="show_sql">true</property> <!-- Drop and re-create the database schema on startup --> <property name="hbm2ddl.auto">update</property> <mapping resource="conf/hibernate/domain/Music.hbm.xml"/> </session-factory> </hibernate-configuration>
-
实体类映射文件Music.hbm.xml,该配置文件放在conf.hibernate.domain路径下
<?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 package="org.lian.domain"> <class name="Music" table="t_music"> <id name="id" column="id"> <!-- id生成策略采用uuid --> <generator class="uuid"/> </id> <!-- id;name;singer;album; duration; author; composer; style; id,name,singer,author,composer,album,duration,style --> <!-- <property name="date" type="timestamp" column="EVENT_DATE"/> --> <property name="name" column="name"/> <property name="singer" column="singer"/> <property name="author" column="author"/> <property name="composer" column="composer"/> <property name="album" column="album"/> <property name="duration" column="duration"/> <property name="style" column="style"/> </class> </hibernate-mapping>
-
实体类org.lian.domain.Music.java
public class Music { private String id; /** * 名称 */ private String name; /** * 歌手 */ private String singer; /** * 专辑 */ private String album; /** * 歌曲时长 */ private String duration; //作词 private String author; //作曲 private String composer; //风格 private String style;
-
hibernateUtil.java 根据配置文件Hibernate创建SessionFactory
public class HibernateUtil { private static final SessionFactory sessionFactory = buildSessionFactory(); private static SessionFactory buildSessionFactory() { try { // Create the SessionFactory from hibernate.cfg.xml // return new // Configuration().configure("/conf/hibernate/hibernate.cfg.xml") // .buildSessionFactory(new StandardServiceRegistryBuilder().build()); Configuration configuration = new Configuration(); configuration.configure("/conf/hibernate/hibernate.cfg.xml"); ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder() .applySettings(configuration.getProperties()).build(); SessionFactory sessionFactory = configuration .buildSessionFactory(serviceRegistry); return sessionFactory; } catch (Throwable ex) { // Make sure you log the exception, as it might be swallowed System.err.println("Initial SessionFactory creation failed." + ex); throw new ExceptionInInitializerError(ex); } } public static SessionFactory getSessionFactory() { return sessionFactory; } }
-
测试类Main.java
public class Main { public static void main(String[] args) { // 获取session连接 Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Music music = new Music(); music.setName("红色高跟鞋"); music.setAuthor("蔡健雅"); music.setSinger("蔡健雅"); music.setComposer("蔡健雅"); music.setAlbum("若你碰到他"); music.setDuration("03:26"); session.save(music); session.getTransaction().commit(); // 关闭session工厂 HibernateUtil.getSessionFactory().close(); } }