eclipse 下配置hibernate以及第一个hibernate小程序
2014-07-23 11:19
295 查看
http://blog.csdn.net/newflypig/article/details/2072059
一般我们配置hibernate框架的时候IDE首选为MyEclipse,只是为了更为清晰地认识hibernate工作原理,而且MyEclipse的集成度太高,有点牛刀小试的架势,作为hibernate的初学者,我来说一说在最原始最纯洁的eclipse下面配置hibernate框架的步骤 ,操作系统:ubuntu,这个与OS平台无关,可以通用。
首先下载hibernate的工作包:
hibernate-3.2.5.ga.tar.gz
1.解压后将其中的lib目录中的jar包全部复制,放到一个eclipse使用合适的目录中去,此目录最好固定,以后使用hibernate工程都需要引入lib目录中的这些包,比如eclipse/mylib目录中,然后将压缩包中的hibernate3.jar仍然解压到该目录。
2.打开eclipse,创建工程Mytest,在该工程的JavaBuilderPath中引入刚才我们解压缩的所有包,当然还有一个不开源的包需要手动下载,这是hibernate开源框架无权引入的一个包,名字叫commons-lang-2.3.jar,可以在apache网站下载:http://jakarta.apache.org/commons/lang/
3.配置就到这里,可以编写第一个hibernate程序了,以MYSQL数据库为例,在新建工程项目Mytest的根目录下面创建一个名为:hibernate.cfg.xml的文件,配置信息如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-configuration
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-configuration>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<session-factory>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.url">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jdbc:mysql://数据库所在主机IP地址/数据库名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.driver_class">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
com.mysql.jdbc.Driver
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.username">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
你的数据库登录名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.password">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
你的数据库登录密码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.dialect">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
org.hibernate.dialect.MySQLDialect
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="show_sql">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.pool_size">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<mapping resource="cn/hxex/message/model1/User.hbm.xml"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<mapping resource="cn/hxex/message/model1/Message.hbm.xml"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</session-factory>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-configuration>
将其中的中文改为你需要的字段就可以了,然后配置数据库。
4.配置数据库,进入你的数据库shell,创建一个表User,具体的创建脚本参见:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create database message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
grant all privileges on message to message@'%' identified by 'message';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
use message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--创建用户表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table user(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
id varchar(32) not null,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name varchar(32) not null,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
password varchar(41) not null,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
primary key(id)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
)engine=innodb;
完成好了以后,我们需要编写该表的POJO类(Plain Ordinary Java Object),具体代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class User {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getId() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setId(String id) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getName() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setName(String name) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getPassword() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setPassword(String password) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.password=Md5Encrypt.encrypt(password);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String toString() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ReflectionToStringBuilder.toString(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
其中Md5Encrypt是我自己编写的一段加密函数,此处不表,大家可以像其他字段一样setPassword就可以了,可以发现,这个POJO类其实就是一个JavaBean,包含了所有属性的setter和getter函数,并且满足对应数据库中的表中的值。OK,有了POJO,我们还需要编写一个与此对应的xml告诉hibernate这个POJO跟数据库是怎样一个关系。
5.编写POJO的xml文件,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Created by the Middlegen Hibernate plugin 2.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
http://boss.bekk.no/boss/middlegen/
http://www.hibernate.org/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="cn.hxex.message.model1.User"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
table="user"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
dynamic-update="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="id"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="java.lang.String"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column="id"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="name"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="java.lang.String"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column="name"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
not-null="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
length="32"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="password"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="java.lang.String"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column="password"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
not-null="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
length="41"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Associations -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
此文件保证于POJO类保存在同一目录,当然这个xml需要命名为:User.hbm.xml。
插一句话,我们的POJO跟User.hbm.xml文件都可以使用专用的工具自动生成,有兴趣的朋友可以参考一下hibernate教程,CSDN的下载频道有。
6.编写User的操作类:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class UserOperate {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**This Class is the tool class for Class User
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param null
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Session session = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserOperate() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
File file = new File("hibernate.cfg.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (file.exists()) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Configuration config = new Configuration().configure(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SessionFactory factory = config.buildSessionFactory();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.session = factory.openSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} else {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("error");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.exit(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//构造函数,应该不会错吧
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void insert(User user) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.session.save(user);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//session.close();
}//插入User数据函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public User queryByName(String name){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User user=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="FROM User as p where p.name=?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.setString(0,name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> userList=q.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator<User> iter=userList.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(iter.hasNext()){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user=iter.next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return user;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//按名字查找数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void delete(String name){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="delete User where name=?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.setString(0, name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.executeUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//删除数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<User> queryAll(){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> list=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="from User";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list=q.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//查找所有
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<User> queryNameLike(String content){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> list=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="from User where name like ?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.setString(0, "%"+content+"%");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list=q.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//模糊查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public boolean canLogin(String name,String password){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boolean flag=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User userGet=this.queryByName(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(userGet!=null){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
password=Md5Encrypt.encrypt(Md5Encrypt.encrypt(password));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(userGet.getPassword().equals(password))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag=true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("密码错误");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("没有该用户存在");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return flag;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//自己写的判断能否登录的函数,其中两次对password加密,可以判断提取出来的密码跟输入的password的一致性
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
7.OK,万事具备,只欠东风了,编写主函数:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class Object {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User user=new User();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UserOperate userop=new UserOperate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setName("Newflypig");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setPassword("8215085");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userop.insert(user);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User[] userArray=new User[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0;i<10;i++){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userArray[i]=new User();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userArray[i].setName("dingding"+String.valueOf(i));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userArray[i].setPassword("newflypig");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userop.insert(userArray[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> list=userop.queryAll();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator<User> iter=list.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while(iter.hasNext()){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(iter.next().getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(userop.canLogin("dingding0", "newflypig"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
至此,我们的第一个hibernate程序编写完毕,如果跟数据库的连接没问题的话就可以正常插入删除查询了。这只是一个一般步骤,具体的细节还需要大家自己深入。写此文章,用以备注。
一般我们配置hibernate框架的时候IDE首选为MyEclipse,只是为了更为清晰地认识hibernate工作原理,而且MyEclipse的集成度太高,有点牛刀小试的架势,作为hibernate的初学者,我来说一说在最原始最纯洁的eclipse下面配置hibernate框架的步骤 ,操作系统:ubuntu,这个与OS平台无关,可以通用。
首先下载hibernate的工作包:
hibernate-3.2.5.ga.tar.gz
1.解压后将其中的lib目录中的jar包全部复制,放到一个eclipse使用合适的目录中去,此目录最好固定,以后使用hibernate工程都需要引入lib目录中的这些包,比如eclipse/mylib目录中,然后将压缩包中的hibernate3.jar仍然解压到该目录。
2.打开eclipse,创建工程Mytest,在该工程的JavaBuilderPath中引入刚才我们解压缩的所有包,当然还有一个不开源的包需要手动下载,这是hibernate开源框架无权引入的一个包,名字叫commons-lang-2.3.jar,可以在apache网站下载:http://jakarta.apache.org/commons/lang/
3.配置就到这里,可以编写第一个hibernate程序了,以MYSQL数据库为例,在新建工程项目Mytest的根目录下面创建一个名为:hibernate.cfg.xml的文件,配置信息如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0" encoding="utf-8"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-configuration
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-configuration>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<session-factory>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.url">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
jdbc:mysql://数据库所在主机IP地址/数据库名称
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.driver_class">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
com.mysql.jdbc.Driver
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.username">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
你的数据库登录名
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.password">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
你的数据库登录密码
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.dialect">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
org.hibernate.dialect.MySQLDialect
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="show_sql">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
false
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property name="hibernate.connection.pool_size">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
2
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</property>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<mapping resource="cn/hxex/message/model1/User.hbm.xml"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<mapping resource="cn/hxex/message/model1/Message.hbm.xml"/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</session-factory>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-configuration>
将其中的中文改为你需要的字段就可以了,然后配置数据库。
4.配置数据库,进入你的数据库shell,创建一个表User,具体的创建脚本参见:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create database message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
grant all privileges on message to message@'%' identified by 'message';
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
use message;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
--创建用户表
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
create table user(
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
id varchar(32) not null,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name varchar(32) not null,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
password varchar(41) not null,
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
primary key(id)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
)engine=innodb;
完成好了以后,我们需要编写该表的POJO类(Plain Ordinary Java Object),具体代码如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class User {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private String password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getId() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setId(String id) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.id = id;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getName() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setName(String name) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.name = name;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String getPassword() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return password;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void setPassword(String password) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.password=Md5Encrypt.encrypt(password);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public String toString() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return ReflectionToStringBuilder.toString(this);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
其中Md5Encrypt是我自己编写的一段加密函数,此处不表,大家可以像其他字段一样setPassword就可以了,可以发现,这个POJO类其实就是一个JavaBean,包含了所有属性的setter和getter函数,并且满足对应数据库中的表中的值。OK,有了POJO,我们还需要编写一个与此对应的xml告诉hibernate这个POJO跟数据库是怎样一个关系。
5.编写POJO的xml文件,如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<?xml version="1.0"?>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!DOCTYPE hibernate-mapping PUBLIC
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!--
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
Created by the Middlegen Hibernate plugin 2.1
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
-->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<class
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="cn.hxex.message.model1.User"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
table="user"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
dynamic-update="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<id
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="id"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="java.lang.String"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column="id"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<generator class="uuid.hex" />
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</id>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="name"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="java.lang.String"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column="name"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
not-null="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
length="32"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<property
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
name="password"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
type="java.lang.String"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
column="password"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
not-null="true"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
length="41"
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
/>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<!-- Associations -->
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</class>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</hibernate-mapping>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
此文件保证于POJO类保存在同一目录,当然这个xml需要命名为:User.hbm.xml。
插一句话,我们的POJO跟User.hbm.xml文件都可以使用专用的工具自动生成,有兴趣的朋友可以参考一下hibernate教程,CSDN的下载频道有。
6.编写User的操作类:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class UserOperate {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
/**This Class is the tool class for Class User
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
* @param null
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
*/
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
private Session session = null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public UserOperate() {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
File file = new File("hibernate.cfg.xml");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if (file.exists()) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Configuration config = new Configuration().configure(file);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SessionFactory factory = config.buildSessionFactory();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.session = factory.openSession();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
} else {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("error");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.exit(0);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//构造函数,应该不会错吧
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void insert(User user) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
this.session.save(user);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
//session.close();
}//插入User数据函数
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public User queryByName(String name){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User user=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="FROM User as p where p.name=?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.setString(0,name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> userList=q.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator<User> iter=userList.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(iter.hasNext()){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user=iter.next();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return user;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//按名字查找数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public void delete(String name){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="delete User where name=?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.setString(0, name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().begin();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.executeUpdate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.getTransaction().commit();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
session.close();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//删除数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<User> queryAll(){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> list=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="from User";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list=q.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//查找所有
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
@SuppressWarnings("unchecked")
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public List<User> queryNameLike(String content){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> list=null;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
String hql="from User where name like ?";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Query q=this.session.createQuery(hql);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
q.setString(0, "%"+content+"%");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
list=q.list();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return list;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//模糊查询
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public boolean canLogin(String name,String password){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
boolean flag=false;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User userGet=this.queryByName(name);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
if(userGet!=null){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
password=Md5Encrypt.encrypt(Md5Encrypt.encrypt(password));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if(userGet.getPassword().equals(password))
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
flag=true;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
else
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("密码错误");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
else{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println("没有该用户存在");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return flag;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}//自己写的判断能否登录的函数,其中两次对password加密,可以判断提取出来的密码跟输入的password的一致性
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
7.OK,万事具备,只欠东风了,编写主函数:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
public class Object {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
public static void main(String[] args) {
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User user=new User();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
UserOperate userop=new UserOperate();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setName("Newflypig");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
user.setPassword("8215085");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userop.insert(user);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
User[] userArray=new User[10];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
for(int i=0;i<10;i++){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userArray[i]=new User();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userArray[i].setName("dingding"+String.valueOf(i));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userArray[i].setPassword("newflypig");
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
userop.insert(userArray[i]);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
List<User> list=userop.queryAll();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Iterator<User> iter=list.iterator();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
while(iter.hasNext()){
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(iter.next().getName());
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
System.out.println(userop.canLogin("dingding0", "newflypig"));
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
至此,我们的第一个hibernate程序编写完毕,如果跟数据库的连接没问题的话就可以正常插入删除查询了。这只是一个一般步骤,具体的细节还需要大家自己深入。写此文章,用以备注。
相关文章推荐
- eclipse 下配置hibernate以及第一个hibernate小程序
- NDK开发之——eclipse配置以及第一个NDK程序
- Servlet第一个Hello Servlet小程序(不用eclipse,手动配置路径)
- anjuta的安装、配置以及第一个hello程序
- pcl1.8.0+QT5.7.0+vs2013 win7 x64环境配置以及第一个小程序实现
- 【JAVA初学者】配置JDK的详细过程,以及第一个JAVA程序的编写。
- PCL学习笔记(1):pcl1.6.0+vs2010环境配置以及第一个pcl程序
- Eclipse3.5下的struts2的配置,以及建立第一个struts项目HelloWorld
- Web程序开发配置(Eclipse + Tomcat + Webwork + Hibernate)
- 单片机--1.开发环境配置以及第一个单片机程序
- 配置第一个Hibernate程序
- java Android SDK安装与环境变量配置以及开发第一个Android程序
- 从JDK安装、卸载到tomcat和eclipse的安装、配置再到经典的第一个JAVA程序——Hello World
- C语言 + GTK3+ Visual Studio 2015环境配置 以及第一个GTK界面程序
- 【安装eclipse, 配置java环境教程】 编写第一个java程序
- Java环境配置以及第一个小程序HelloWorld
- windows xp下配置JDK环境变量(附:第一个例子程序,命令行下和eclipse下)
- Servlet简介以及web配置以及第一个基于Servlet的HelloWorld程序
- JDK安装、配置以及编译运行我第一个JAVA程序
- 【安装eclipse, 配置java环境教程】 编写第一个java程序