日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

hibernate 初学 第一个例子

發布時間:2025/7/14 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate 初学 第一个例子 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

2019獨角獸企業重金招聘Python工程師標準>>>

閑來沒事,就把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://localhost:3306/testadmin</property><property name="connection.username">root</property><property name="connection.password">123</property><!-- JDBC connection pool (use the built-in) --><property name="connection.pool_size">10</property><!-- SQL dialect --><property name="dialect">org.hibernate.dialect.MySQL5Dialect</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><mapping class="com.hhdys.domain.Account" /></session-factory></hibernate-configuration>



想把數據庫連接等相關信息寫道preperties文件里面,但是,沒有在文檔上找到相關的設置,所以就沒有寫了,有知道的煩請告知一下如果操作,謝謝。(例如db.preperties,不是hibernate.properties)

下來,是工具類:代碼如下

package com.hhdys.util;import org.hibernate.SessionFactory; import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.cfg.Configuration; import org.hibernate.service.ServiceRegistry;public class HibernateUtil {static SessionFactory factory = null;public static SessionFactory getSessionFactory() {if (factory == null) {synchronized (HibernateUtil.class) {if (factory == null) {Configuration configuration = new Configuration().configure();ServiceRegistry serviceRegistry = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties()).build();factory = configuration.buildSessionFactory(serviceRegistry);}}}return factory;} }



實體bean:

package com.hhdys.domain;import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.Id;@Entity public class Account {@Id@GeneratedValueprivate int id;private String username;private String password;private String name;private int department;private int sex;private int age;private int inuse;@Column(name = "create_time")private long createTime;@Column(name = "last_login_time")private long lastLoginTime;private int role;private int position;/*** @return the id*/public int getId() {return id;}/*** @param id* the id to set*/public void setId(int id) {this.id = id;}/*** @return the username*/public String getUsername() {return username;}/*** @param username* the username to set*/public void setUsername(String username) {this.username = username;}/*** @return the password*/public String getPassword() {return password;}/*** @param password* the password to set*/public void setPassword(String password) {this.password = password;}/*** @return the name*/public String getName() {return name;}/*** @param name* the name to set*/public void setName(String name) {this.name = name;}/*** @return the department*/public int getDepartment() {return department;}/*** @param department* the department to set*/public void setDepartment(int department) {this.department = department;}/*** @return the sex*/public int getSex() {return sex;}/*** @param sex* the sex to set*/public void setSex(int sex) {this.sex = sex;}/*** @return the age*/public int getAge() {return age;}/*** @param age* the age to set*/public void setAge(int age) {this.age = age;}/*** @return the createTime*/public long getCreateTime() {return createTime;}/*** @param createTime* the createTime to set*/public void setCreateTime(long createTime) {this.createTime = createTime;}/*** @return the lastLoginTime*/public long getLastLoginTime() {return lastLoginTime;}/*** @param lastLoginTime* the lastLoginTime to set*/public void setLastLoginTime(long lastLoginTime) {this.lastLoginTime = lastLoginTime;}/*** @return the role*/public int getRole() {return role;}/*** @param role* the role to set*/public void setRole(int role) {this.role = role;}/*** @return the position*/public int getPosition() {return position;}/*** @param position* the position to set*/public void setPosition(int position) {this.position = position;}/*** @return the inuse*/public int getInuse() {return inuse;}/*** @param inuse the inuse to set*/public void setInuse(int inuse) {this.inuse = inuse;} }



測試代碼:

package com.hhdys.test;import java.util.List; import java.util.Map;import org.hibernate.SQLQuery; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.transform.Transformers;import com.hhdys.domain.Account; import com.hhdys.util.HibernateUtil;public class Test1 {private static SessionFactory factory = HibernateUtil.getSessionFactory();public static void main(String[] args) {System.out.println("開始");Session session = factory.openSession();List<Account> list = session.createQuery("from Account").list();System.out.println(list.size());SQLQuery query=session.createSQLQuery("select *from department");query.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP);List<Map<String, String>> ll=query.list();System.out.println(ll.size());session.flush();session.close();} }



ok一個簡單的查詢例子,第一個是按照hql查詢返回Account的list,第二個是按照正常的sql返回一個包含map的list。

轉載于:https://my.oschina.net/hhdys412/blog/184065

總結

以上是生活随笔為你收集整理的hibernate 初学 第一个例子的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。