hibernate 中的增删改查,以及返回值类型的查询

 

首先配置下hibernate.cfg.xml

<?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">
<hibernate-configuration>
<session-factory>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">
<![CDATA[jdbc:mysql://127.0.0.1:3306/java4?userUnicode=true&characterEncoding
=utf8]]>
</property>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="show_sql">true</property>
<property name="format_sql">false</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="hbm2ddl.auto">update</property>



<mapping resource="com/hp/bean/Cat.hbm.xml" /> 
</session-factory>
</hibernate-configuration>

 

接下来写session工厂HibernateConn.java类

 

package com.hp.dbconn;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateConn {

public static Session getSession() {

Configuration cfg = new Configuration().configure("hibernate.cfg.xml");

SessionFactory sf = cfg.buildSessionFactory();

Session session = sf.openSession();

return session;
}

}

javabean中通过反向生成Cat.java类。

package com.hp.bean;

public class Cat {
private int id;
private String Sex;
private String name;
private String pinzhong;
private double price;
private int age;

public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getSex() {
return Sex;
}
public void setSex(String sex) {
Sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPinzhong() {
return pinzhong;
}
public void setPinzhong(String pinzhong) {
this.pinzhong = pinzhong;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
public Cat() {
super();
// TODO Auto-generated constructor stub
}
public Cat(int id, String sex, String name, String pinzhong, double price,int age) {
super();
this.id = id;
Sex = sex;
this.name = name;
this.pinzhong = pinzhong;
this.price = price;
this.age=age;
}

 

}

接着写 CatDao.java类。代码如下:

package com.hp.dao;

import java.nio.channels.SeekableByteChannel;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.hp.bean.Cat;
import com.hp.dbconn.HibernateConn;

public class CatDao {

Session session = HibernateConn.getSession();

Transaction tr = session.beginTransaction();
//增加数据
public void saveCat() {
Cat c = new Cat(1, "公", "兰兰1", "加菲猫",100,2);
Cat c1 = new Cat(2, "母", "兰兰2", "加菲猫",200,3);
Cat c2 = new Cat(3, "公", "兰兰3", "加菲猫",10,4);
Cat c3 = new Cat(4, "公", "兰兰4", "加菲猫",50,5);

session.save(c);
session.save(c1);
session.save(c2);
session.save(c3);

tr.commit();
session.close();

}
//返回多个值
public void ListCat(){
String hql="from Cat";
Query q = session.createQuery(hql);
List<Cat> cat = q.list();
for(Cat cats:cat){
System.out.print(cats.getId());
System.out.print(cats.getName());
System.out.print(cats.getSex());
System.out.print(cats.getPinzhong());
System.out.println(cats.getPrice());
}

}
//返回单个值
public void CountCat(){
String hql="select count(c)from Cat c";
Query q = session.createQuery(hql);
Number m =(Number) q.uniqueResult();
int i =m.intValue();
System.out.println("表中一共有"+i+"条数据");
}

//返回list集合
public void ListCat2(){
String hql = "select new list(c.name, c.Sex, c.pinzhong) from Cat c";
Query q = session.createQuery(hql);
List<List> l = q.list();
for(List list:l){
System.out.println(list.get(0));
System.out.println(list.get(1));
System.out.println(list.get(2));
}

}

//返回一个object[]数组
public void ShuzuCat(){
String hql="select c.name, c.Sex, c.pinzhong from Cat c";
Query q =session.createQuery(hql);
List <Object[] > l = q.list();
for(Object[] list : l){
System.out.println(list[0]);
System.out.println(list[1]);
System.out.println(list[2]);

}
//返回多个值,结果为map数组
public void mapCat(){
String hql="select new map(c.name, c.Sex, c.pinzhong ,c.price) from Cat c";
List<Map> map = session.createQuery(hql).list();
for(Map maps:map){
System.out.println(map.get(0));
System.out.println(map.get(1));
System.out.println(map.get(2));
System.out.println(map.get(3));
}
}
//返回多个值,结果为自定义类型
public void zidingyiCat(){
String hql=" from Cat c";
List<Cat> l = session.createQuery(hql).list();
for(Cat cats:l){
System.out.println(cats.getName());
System.out.println(cats.getPinzhong());

}
}
//带有查询条件的hql语句
public void WhereCat(){
List<Cat> c = session.createQuery("from Cat c where c.age>3").list();
for(Cat cats:c){
System.out.println(cats.getId());
System.out.println(cats.getName());

}

}
//带有查询条件 (占位符传入参数)
public void GetwhereCat(){
Query q = session.createQuery("from Cat c where c.age>?");
q.setParameter(0, 3);
List<Cat> l = q.list();
for(Cat cat:l){
System.out.println(cat.getId());
System.out.println(cat.getName());
System.out.println(cat.getAge());
}
}




//删除数据
public void deleteCat(){
Cat c = new Cat();
Cat c1 = (Cat) session.get(c.getClass(), 1);
Cat c2 = (Cat) session.get(c.getClass(), 2);
Cat c3 = (Cat) session.get(c.getClass(), 3);
Cat c4 = (Cat) session.get(c.getClass(), 4);
session.delete(c1);
session.delete(c2);
session.delete(c3);
session.delete(c4);
tr.commit();
session.close();



}
public void update(){
Cat c = (Cat)session.get(Cat.class, 2);
c.setName("见见");
c.setAge(3);
c.setPinzhong("ss");
session.update(c);
tr.commit();
session.close();
}


}

 最后通过一个测试test方法来实现

转载于:https://www.cnblogs.com/lyh-8/p/6085665.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值