hibernate 初学 第一个例子
生活随笔
收集整理的這篇文章主要介紹了
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>
下來,是工具類:代碼如下
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;} }
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();} }
轉載于:https://my.oschina.net/hhdys412/blog/184065
總結
以上是生活随笔為你收集整理的hibernate 初学 第一个例子的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: RHEL6基础四十二之RHEL文件共享①
- 下一篇: HDU 1008 Elevator