要使用c3p0,首先在hibernate中选择required的jar和optional的c3p0目录的jar包,再加上mysql的驱动。
com.mysql.jdbc.Driver jdbc:mysql://localhost:3306/simpledb root system 20 5 5000 100 3000 2 true org.hibernate.dialect.MySQLDialect create
准备映射的类。
package com.lin.domain;
public class News {
private int id;
private String title;
private String content;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
映射文件:News.hbm,xml
测试代码:
System.out.println("starting ............");
Configuration conf = new Configuration().configure();
SessionFactory factory = conf.buildSessionFactory();
Session session = factory.openSession();
Transaction tx = session.beginTransaction();
News n = new News();
n.setId(1);
n.setTitle("wanku");
n.setContent("this is wanku project!!!");
session.save(n);
tx.commit();
session.close();
factory.close();
System.out.println("ending................");