新建一个JAVA项目,在项目属性中选择”JAVA构建路径”,在库标签中点“添加外部JAR”
添加Hibernate必须的JAR(当然也要把MYSQL驱动JAR也加上),如下:
antlr.jar
cglib.jar
asm.jar
asm-attrs.jars
commons-collections.jar
commons-logging.jar
hibernate3.jar
jta.jar
dom4j.jar
log4j.jar
1.建立hibernate.cfg.xml如下:
/p>
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
test
jdbc:mysql://localhost:3306/test
org.hibernate.dialect.MySQLDialect
mysqlprofile
test
org.gjt.mm.mysql.Driver
1
2.建立与数据库表想对应的配置文件
/p>
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
3.建立一个封装员工信息的类
package query;
public class Employee {
private int id;
private String name;
private String department;
private double salary;
public void setId(int id){this.id=id;}
public void setName(String name){this.name=name;}
public void setDepartment(String department ){this.department=department;}
public void setSalary(double salary){this.salary=salary;}
public int getId(){return id;}
public String getName(){return name;}
public String getDepartment(){return department;}
public double getSalary(){return salary;}
}
4.创建管理SESSION的类
package query;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class HibernateUtil {
private static Log log=LogFactory.getLog(HibernateUtil.class);
private static final SessionFactory sessionFactory;
static {
try {
sessionFactory=new Configuration().configure().buildSessionFactory();
} catch (Throwable ex) {
ex.printStackTrace();// TODO: handle exception
throw new ExceptionInInitializerError(ex);
}
}
public static final ThreadLocal session=new ThreadLocal();
public static Session currentSession()throws HibernateException{
Session s=(Session)session.get();
if(s==null||!s.isOpen()){
s=sessionFactory.openSession();
session.set(s);
}
return s;
}
public static void closeSession()throws HibernateException{
Session s=(Session)session.get();
session.set(null);
if(s!=null)s.close();
}
}
5.建立员工信息查询类:
package query;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import javax.swing.*;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class QueryFrame extends JFrame implements ActionListener {
/**
*
*/
private static final long serialVersionUID = 1L;
JTextField jf=new JTextField(20);
JButton find=new JButton("查询");
JPanel panel_find=new JPanel();
JPanel panel_content=new JPanel();
public List getEmployees(String empname)throws HibernateException{
Session session=HibernateUtil.currentSession();
Transaction tx=session.beginTransaction();
//String sql="from Emloyee where name like '%"+empname+"%'";
String sql="from Employee";
Query query=session.createQuery(sql);
List list=query.list();
tx.commit();
HibernateUtil.closeSession();
return list;
}
public QueryFrame(){
this.setTitle("查询系统");
Container c=this.getContentPane();
jf.addActionListener(this);
find.addActionListener(this);
panel_find.add(jf);
panel_find.add(find);
c.add(panel_find, "North");
c.add(panel_content,"Center");
this.setSize(400,400);
this.setLocation(200,100);
this.setVisible(true);
}
public void actionPerformed(ActionEvent e){
panel_content.removeAll();
String content=jf.getText();
JLabel label1=new JLabel("员工号"+"----"+"员工姓名"+"----"+"员工部门"+"----"+"员工薪水");
JPanel panel= new JPanel();
panel.add(label1);
int i=0;
for(Iterator it=this.getEmployees(content).iterator();it.hasNext(); ){
Employee emp=(Employee)it.next();
JLabel label=new JLabel(emp.getId()+"----"+emp.getName()+"----"+emp.getDepartment()+"----"+emp.getSalary());
panel.add(label);
panel_content.add(panel);
i++;
}
panel_content.setLayout(new GridLayout(i,1));
panel_content.validate();
}
public static void main(String[] args) {
new QueryFrame();// TODO Auto-generated method stub
}
}