我从一个项目工程中找到了一段代码,并作简单程度的编写实现了对商品信息的增删改查:
商品类:
package com.lxy.entity;
public class Goods {
private int id;
private String name;
private int stock;//库存
private String address;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getStock() {
return stock;
}
public void setStock(int stock) {
this.stock = stock;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Goods(String name, int stock, String address) {
this.name = name;
this.stock = stock;
this.address = address;
}
public Goods() {
super();
// TODO Auto-generated constructor stub
}
@Override
public String toString() {
return "Goods [id=" + id + ", name=" + name + ", stock=" + stock
+ ", address=" + address + "]";
}
}
hbm文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd
">
<hibernate-mapping package="com.lxy.entity">
<class name="Goods" table="Goods" select-before-update="true" dynamic-update="true">
<id name="id" column="id">
<generator class="identity" />
</id>
<property name="name" length="100"/>
<property name="stock" />
<property name="address" length="100"/>
</class>
</hibernate-mapping>
cfg文件:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="show_sql">true</property>
<property name="hibernate.dialect">
org.hibernate.dialect.MySQL5Dialect
</property>
<property name="hibernate.connection.driver_class">
com.mysql.jdbc.Driver
</property>
<property name="hibernate.connection.url">
jdbc:mysql://localhost:3306/users
</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">1941785654</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<!-- 保证每个线程有一个属于自己的Session对象 -->
<property name="current_session_context_class">thread</property>
<mapping resource="com/lxy/entity/Goods.hbm.xml" />
</session-factory>
</hibernate-configuration>
Dao层:
package com.DAO;
import com.lxy.entity.Goods;
public interface GoodsDao {
public Goods selectGoods(int id);
public void updateGoods(int id,Goods goods);
public void daleteGoods(int id);
public void addGoods(Goods goods);
}
impl实现类:
package com.DAO;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import com.lxy.entity.Goods;
public class GoodsDaoImpl implements GoodsDao {
public Goods selectGoods(int id) {
// TODO Auto-generated method stub
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
Goods p = new Goods();
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
p = session.get(Goods.class,id);
System.out.println(p.toString());
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
return p;
}
public void updateGoods(int id,Goods goods) {
// TODO Auto-generated method stub
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
goods.setId(id);
session.update(goods);
System.out.println("-- "+goods.getId()+" --");
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
public void daleteGoods(int id) {
// TODO Auto-generated method stub
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
Goods p = session.get(Goods.class, id);
session.delete(p);
System.out.println("-- "+p.getId()+"--");
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
public void addGoods(Goods goods) {
// TODO Auto-generated method stub
SessionFactory sf = null;
Session session = null;
Transaction ts = null;
System.out.println("1");
try {
sf = HibernateUtil.getSessionFactory();
session = sf.getCurrentSession();
ts = session.beginTransaction();
session.save(goods);
System.out.println("--"+goods.toString()+"--");
ts.commit();
} catch (HibernateException e) {
// TODO Auto-generated catch block
if(ts != null)
{
ts.rollback();
}
e.printStackTrace();
}
}
}
sessionfactory单例:
package com.DAO;
import org.hibernate.HibernateException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtil {
private static Configuration cfg;
private static SessionFactory sf;
static
{
try {
cfg = new Configuration().configure();
sf = cfg.buildSessionFactory();
} catch (HibernateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory(){
return sf;
}
}
测试类:
package com.DAO;
import org.junit.Test;
import com.lxy.entity.Goods;
public class TestDao {
GoodsDaoImpl goodsDaoImpl = new GoodsDaoImpl();
@Test
public void testadd(){
Goods e = new Goods("ceshi1",66,"china");
goodsDaoImpl.addGoods(e);
}
@Test
public void testselect(){
goodsDaoImpl.selectGoods(1);
}
@Test
public void testdel(){
goodsDaoImpl.daleteGoods(5);
}
@Test
public void testupdate(){
Goods e = new Goods("lixinyu1",91,"china");
goodsDaoImpl.updateGoods(2, e);
}
}
通过测试,代码运行正常,可以实现增删改查的功能。