- 论坛徽章:
- 0
|
用hibernate写了第一个ORM程序,控制台的,实现往数据库中写,必须得包括以下几个包:
asm.jar
cglib-2.1.3.jar
commons-collections-2.1.1.jar
commons-logging-1.0.4.jar
dom4j-1.6.1.jar
ehcache-1.1.jar
hibernate3.jar
jta.jar
log4j-1.2.11.jar
mysql-connector-java-3.1.12-bin.jar
要实现读出,必须加入以下包:
antlr-2.7.6rc1.jar
建立数据库与表:
`computer`.CREATE DATABASE `computer` /*!40100 DEFAULT CHARACTER SET latin1 */;
DROP TABLE IF EXISTS `computer`.`computer`;
CREATE TABLE `computer`.`computer` (
`id` int(10) unsigned NOT NULL auto_increment,
`cpu` varchar(45) default NULL,
`display` varchar(45) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
配置了以下文件:
hibernate.cfg.xml(置于/src目录中)
org.hibernate.dialect.MySQLDialect
org.gjt.mm.mysql.Driver
jdbc:mysql://localhost/computer
root
aoe
*.hbm.xml(mapping--与相关类放在同一目录)
log4j.properties(置于根下)
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.rootLogger=warn, stdout
编写对象类:
public class computer {
private int id;
private String cpu;
private String display;
public int getId(){
return id;
}
public void setId(int id){
this.id=id;
}
public String getCpu(){
return cpu;
}
public void setCpu(String cpu){
this.cpu=cpu;
}
public String getDisplay(){
return display;
}
public void setDisplay(String display){
this.display=display;
}
/** Creates a new instance of computer */
public computer() {
}
}
注意每一个变量都要有相对应的setXX与getXX方法,也包括为主键自动增长的id字段.
持久化编程
import java.sql.SQLException;
import org.apache.log4j.PropertyConfigurator;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public static void main(String[] args) throws HibernateException{
PropertyConfigurator.configure("log4j.properties");
Configuration cfg = new Configuration();
cfg.configure();
SessionFactory sf = cfg.buildSessionFactory();
Session sess = sf.openSession();
computer pc = new computer();
pc.setCpu("Inter 奔腾 4 2.8B GHz");
pc.setDisplay("LG 563LS");
sess.save(pc);
sess.flush();
try{
sess.connection().commit();
}catch(HibernateException e){
e.printStackTrace();
}catch(SQLException e){
e.printStackTrace();
}
sess.close();
}
但是还有一个警告信息存在:
No configuration found. Configuring ehcache from ehcache-failsafe.xml found in the classpath: jar:file:/D:/ztkdir/java/hibernate/hibernate-3.1.3/lib/ehcache-1.1.jar!/ehcache-failsafe.xml
缺少ehcache.xml配置文件,在src目录下新建ehcache.xml,并配置如下:
网上的资料说,还要在hibernate.cfg.xml与*.hbm.xml中加入相应的配置,这是对hibernate3.0以前的版本说的.对3.0版本只须建立ehcache.xml即可,不要再做其它操作就OK了!
另外:我的mysql的数据编程为utf8,从程序中写入的汉字为乱码,些问题待研究.
原因:Hibernate的基础还是JDBC,所以一样需要设置characterEncoding!
解决方法:
在hibernate.cfg.xml中应该这样写
property name="connection.url">jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8property>如果用hibernate.properties
#hibernate.connection.url jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8
ps:处理中文的话,characterEncoding用GBK一样可以。
但字节编码问题最好的解决方法还是统一使用UTF-8!!!
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/17347/showart_121416.html |
|