日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) >

hibernate 初学 第一个例子

發(fā)布時(shí)間:2025/7/14 44 豆豆
生活随笔 收集整理的這篇文章主要介紹了 hibernate 初学 第一个例子 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>

閑來(lái)沒(méi)事,就把hibernate也給學(xué)了吧。

首先,上配置文件:

<?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>



想把數(shù)據(jù)庫(kù)連接等相關(guān)信息寫(xiě)道preperties文件里面,但是,沒(méi)有在文檔上找到相關(guān)的設(shè)置,所以就沒(méi)有寫(xiě)了,有知道的煩請(qǐng)告知一下如果操作,謝謝。(例如db.preperties,不是hibernate.properties)

下來(lái),是工具類:代碼如下

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;} }



實(shí)體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;} }



測(cè)試代碼:

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("開(kāi)始");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一個(gè)簡(jiǎn)單的查詢例子,第一個(gè)是按照hql查詢返回Account的list,第二個(gè)是按照正常的sql返回一個(gè)包含map的list。

轉(zhuǎn)載于:https://my.oschina.net/hhdys412/blog/184065

總結(jié)

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

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。