SSH:hiberate實現資料的查詢(單查詢和全查詢)

燕雙嚶發表於2019-01-01

先配置一大堆檔案

1,User.java-物件類

package Test;

import java.util.Date;

public class User {
	private Integer id;
	private String name;
	private String password;
	private Integer age;
	private String gender;
	private Date birthday;
	

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public Integer getAge() {
		return age;
	}

	public void setAge(Integer age) {
		this.age = age;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + this.id;
		return result;
	}
}

2,配置hiberate.cfg.xml


<!DOCTYPE hibernate-configuration PUBLIC  "-//Hibernate/Hibernate Configuration DTD 3.0//EN"  

 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> 

 <hibernate-configuration>

   <session-factory>

     <property name="hibernate.connection.driver_class">com.mysql.cj.jdbc.Driver</property>

     <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/Hibernatedatabase3?serverTimezone=GMT</property>

     <property name="hibernate.connection.username">root</property>

     <property name="hibernate.connection.password">123456</property>

     <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property>
	 
	 <property name="hbm2ddl.auto" >update</property>
     
     <mapping resource="User.hbm.xml" /> 
   </session-factory>

 </hibernate-configuration>

3,配置User.hub.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
	<class name="Test.User" table="User">
		<id name="id" column="id" type="int">
			<generator class="native"/>
		</id>
		<property name="name" type="string" not-null="true" length="50">
			<column name="name"/>
		</property>
		<property name="password" type="string" not-null="true" length="50">
			<column name="password"/>
		</property>
		<property name="age">
			<column name="age"/>
		</property>
		<property name="gender" type="string" not-null="true" length="50">
			<column name="gender"/>
		</property>
		<property name="birthday" type="date" >
			<column name="birthday"/>
		</property>
	</class>
</hibernate-mapping>

操作類---核心

註釋為根據主鍵查詢,非註釋為查詢全表資訊

package Test;

import java.util.Date;
import java.util.List;

import org.hibernate.*;
import org.hibernate.cfg.Configuration;

public class UserTest {
	public static void main(String[] args) {
		SessionFactory sf = null; // 負責Session的實現和建立
		Session session = null; // Session與資料庫之間的一個會話,是核心
		Configuration cfg = new Configuration().configure();// 主要負責載入和管理Hiberate的配置資訊,也可載入對映檔案資訊
		sf = cfg.buildSessionFactory();
		session = sf.openSession();
//		User user = (User) session.get(User.class, 1);//查詢主鍵id=1的使用者
//		System.out.println(user.getName() + " " + user.getPassword() + " " + user.getGender());
		String hql = "from User";
		List<User> userList = session.createQuery(hql).list();
		for (User user : userList) {
			System.out.println(user.getName() + " " + user.getPassword() + " " + user.getGender());
		}
	}
}

 

相關文章