使用hibernate进行单表的增删改查
1.使用Hibernate进行增删改查(假设你已经导入相关jar包)
2.配置hibernate.cfg.xml文件(放在src根目录下面)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!--数据库驱动-->
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<!--password-->
<property name="hibernate.connection.password">root</property>
<!--url-->
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/my</property>
<!--username-->
<property name="hibernate.connection.username">root</property>
<!--dialect-->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 不使用注解,关联实体类 -->
<mapping resource="hy_people/entity/People.hbm.xml"/>
<!-- 使用注解 -->
<mapping class="hy_people.entity.Provinces"/>
<mapping class="hy_people.entity.Citys"/>
<mapping class="hy_stu_score.Coustomer"/>
<mapping class="hy_stu_score.Score"/>
<mapping class="hy_stu_score.Student"/>
</session-factory>
</hibernate-configuration>
3.配置People.hbm.xml文件(放在POJO包下面)
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-8-4 15:04:28 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="hy_people.entity.People" table="PEOPLE">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" />
</property>
<property name="age" type="int">
<column name="AGE" />
</property>
<property name="address" type="java.lang.String">
<column name="ADDRESS" />
</property>
</class>
</hibernate-mapping>
4.POJO层
public class People {
private String name;
private int age;
private String address;
private int id;
public People() {
}
public People(String name, int age, String address) { super(); this.name = name; this.age = age; this.address = address; }
public String getName() { return name; }
public void setName(String name) { this.name = name; }
public int getAge() { return age; }
public void setAge(int age) { this.age = age; }
public String getAddress() { return address; }
public void setAddress(String address) { this.address = address; }
public int getId() { return id; }
public void setId(int id) { this.id = id; }
}
5.Dao层
public Integer addPeople(String name,int age,String address){
Session session = factory.openSession();
Transaction tx = null;
Integer num = null;
tx = session.beginTransaction();
People p = new People(name,age,address);
num = (Integer)session.save(p);
tx.commit();
return num;
}
private void updatePeople(String name,int id){
Session session = factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
//id为表的主键
People p = (People) session.get(People.class, id);
p.setName(name);
session.update(p);
tx.commit();
session.close();
}
private void deletedpeople(int id){
Session session = factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
People p = (People)
//id为表的主键
session.get(People.class, id);
session.delete(p);
tx.commit();
session.close();
}
private List selectPople(){
List list = new ArrayList();
Session session = factory.openSession();
Transaction tx = null;
tx = session.beginTransaction();
String hql = "from People";
Query query = session.createQuery(hql);
list = query.list();
tx.commit();
return list;
}