hql语言的使用(一)

            1.Hibernate作为一种框架,在使用方面,确实有很多的方便,直接上步骤,

             2. 先把 常用的 jar包导入--------配置一个 hibernare.cfg.xml文件--------创建entitybao(最好与数据库中的 意义对应,列名也对应,)------写impl文件------具体的jsp或者servlet

            3.上一波 hibernate.cfg.xml文件的写法

<?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>
		
        <!-- Database connection settings -->
        <property name="connection.driver_class"><span style="background-color: rgb(51, 204, 0);">oracle.jdbc.driver.OracleDriver</span></property>
        <property name="connection.url"><span style="background-color: rgb(51, 204, 0);">jdbc:oracle:thin:@localhost:1521:orcl</span></property>
        <property name="connection.username"><span style="color:#ff0000;background-color: rgb(51, 204, 0);">xiao</span></property>
        <property name="connection.password"><span style="background-color: rgb(51, 204, 0);">123456</span></property>

        <!-- JDBC connection pool (use the built-in) -->
        <property name="connection.pool_size">1</property>

        <!-- SQL dialect <span style="background-color: rgb(204, 0, 0);">方言</span>-->
        <property name="dialect"><span style="background-color: rgb(51, 204, 0);">org.hibernate.dialect.Oracle10gDialect</span></property>

        <!-- Enable Hibernate's automatic session context management -->
        <property name="current_session_context_class">thread</property>

        <!-- Disable the second-level cache  -->
        <property name="cache.provider_class">org.hibernate.cache.internal.NoCacheProvider</property>

        <!-- Echo all executed SQL to stdout -->
        <property name="show_sql">true</property>

        <!-- Drop and re-create the database schema on startup -->
        <property name="hbm2ddl.auto">update</property>

 <!--        <mapping resource="com/xt/hiber/entitys/Dept.hbm.xml"/>
        
        <mapping class="com.xt.hiber.entitys.Person"/>  -->
       <span style="background-color: rgb(51, 204, 0);"> <mapping class="com.xt.hiber.entitys.depts"/> </span>

    </session-factory>

</hibernate-configuration>

就是 把 dao包和 impl 中部分的代码进行了简化,大大 提高 操作 表的速度,

            2.entitys 中的文件

@Entity
@Table(name="person")
public class Person implements Serializable {
	
	@Id
	private int id;}
其中 @id 是必须要写的,如果 表名和数据库 中的 名字不一样,那么 加上table的name

import javax.persistence.Entity;
import javax.persistence.Id; 是这个包中的id

   3.上一波操作的方法impl

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.xt.hiber.entitys.Person;

public class PersonTest {
	
	Session session=null;
	
	
	@Before
	public void BeforeMethod(){
		Configuration cfg=new Configuration().configure();
	    SessionFactory sessionfactory = cfg.buildSessionFactory();
	    session=sessionfactory.openSession();
	}
	
	@After
	public void CloseMethod(){
		if(session!=null){
			session.close();
		}
	}
	
	@Test
	public void TestAdd(){
		
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
		Transaction tx= session.beginTransaction();
		try{
			Person p1=new Person(2, "李元霸", "1", 20, sdf.parse("1990-12-21"), "山西晋城");
		
			session.save(p1);
			
			Person p1=new Person(2, "李元", "1", 20, sdf.parse("1990-12-21"), "山西晋城");
			
			session.update(p1);
		
			Person p1=new Person();
			p1.setId(2);
			session.delete(p1);
			tx.commit();
		}catch(Exception e){
			e.printStackTrace();
			tx.rollback();
		}
	}
	
	
	
	@Test
	public void TestLoad(){
		
		try{
			Person p1 = (Person)session.load(Person.class, new Integer(1));
			System.out.println("姓名:"+p1.getName());
		}catch(Exception e){
			e.printStackTrace();
		}
	}
	
	
	
	

}
插入数据 save,更新数据update,获得 数据 load,删除 delete

当然简便一点的还有 HQL语言,直接使用 sql 标准 操作 entitys 中的表,

可以改变 数据库中的 表,

package com.xt.test;

import java.text.SimpleDateFormat;
import java.util.List;


import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.xt.hiber.entitys.Person;
import com.xt.hiber.entitys.UserInfo;

public class UserInfoTest {
	
	Session session=null;
	
	
	@Before
	public void BeforeMethod(){
		Configuration cfg=new Configuration().configure();
	    SessionFactory sessionfactory = cfg.buildSessionFactory();
	    session=sessionfactory.openSession();
	}
	
	@After
	public void CloseMethod(){
		if(session!=null){
			session.close();
		}
	}
	
	@Test
	public void TestQuery1(){
		String uname="z%";
		String hql="from UserInfo where uname like '"+uname+"'";
		
		Query query = session.createQuery(hql);
		List<UserInfo> list= query.list();
		if(list!=null&& list.size()>0){
			for(UserInfo u : list){
				System.out.println(u.getUname());
			}
		}
	
	}
	
	@Test
	public void TestQuery2(){
		//sql注入攻击
		String uname="zhang' or 1=1 or 1='";
		String upwd="asdhjad";
		//采用拼接字符串
		String hql="select count(*) from UserInfo where uname = '"+uname+"' and upwd='"+upwd+"'";
		
		Query query = session.createQuery(hql);
		Object obj = query.uniqueResult();
		int cnt=Integer.parseInt(obj.toString());
		System.out.println("cnt = "+cnt);
		if(cnt>0){
			System.out.println("登陆成功!");
		}else{
			System.out.println("登陆失败,用户名或者密码不正确!");
		}
	}
	
	@Test
	public void TestQuery3(){
		//sql注入攻击
		String uname="zhang' or 1=1 or 1='";
		String upwd="asdhjad";
		//hql语句的参数采用占位符的形式
		String hql="select count(*) from UserInfo where uname = ? and upwd=?";
		
		Query query = session.createQuery(hql);
		query.setString(0, uname);
		query.setString(1, upwd);
		Object obj = query.uniqueResult();
		int cnt=Integer.parseInt(obj.toString());
		System.out.println("cnt = "+cnt);
		if(cnt>0){
			System.out.println("登陆成功!");
		}else{
			System.out.println("登陆失败,用户名或者密码不正确!");
		}
	}
	
	@Test
	public void TestQuery4(){
		//sql注入攻击
		String uname="zhang' or 1=1 or 1='";
		String upwd="asdhjad";
		//hql语句的参数采用参数名称绑定的形式
		String hql="select count(*) from UserInfo where uname = :uname and upwd=:upwd";
		
		Query query = session.createQuery(hql);
		query.setString("uname", uname);
		query.setString("upwd", upwd);
		Object obj = query.uniqueResult();
		int cnt=Integer.parseInt(obj.toString());
		System.out.println("cnt = "+cnt);
		if(cnt>0){
			System.out.println("登陆成功!");
		}else{
			System.out.println("登陆失败,用户名或者密码不正确!");
		}
	}
	
	@Test
	public void TestQuery5(){
		//sql注入攻击
		String uname="zhangsan";
		String upwd="123456";
		//hql语句的参数采用参数名称绑定的形式
		String hql="from UserInfo where uname = :uname and upwd=:upwd";
		
		Query query = session.createQuery(hql);
		query.setString("uname", uname);
		query.setString("upwd", upwd);
		UserInfo uinfo =(UserInfo)query.uniqueResult();
	
		System.out.println("用户名:"+uinfo.getUname());
		
	}
	
		
	

}
当然还有 分页的出现

public void TestQuery1(){
String hql="from Dept";
Query query = session.createQuery(hql);
List<Dept> list = query.list();
if(list!=null && list.size()>0){
for(Dept d : list){
System.out.println("部门编号:"+d.getDno().trim()+",部门名称:"+d.getDname());
}
}
}

@Test
public void TestQuery2(){
String hql="from Dept order by dno asc";
Query query = session.createQuery(hql);
List<Dept> list = query.list();
if(list!=null && list.size()>0){
for(Dept d : list){
System.out.println("部门编号:"+d.getDno().trim()+",部门名称:"+d.getDname());
}
}
}

@Test
public void TestQueryPage(){

int pagesize=2;//页大小
int pagecount=0;//总页数
int pageidx=2;//当前第几页

String hql2="select count(*) from Dept";
Query query2 = session.createQuery(hql2);
int rowcount =Integer.parseInt(query2.uniqueResult().toString());
pagecount=rowcount%pagesize==0?rowcount/pagesize:rowcount/pagesize+1;

String hql="from Dept order by dno asc";
Query query = session.createQuery(hql);

query.setFirstResult((pageidx-1)*pagesize);//设置第几页
query.setMaxResults(pagesize);
//设置页大小

List<Dept> list = query.list();
if(list!=null && list.size()>0){
for(Dept d : list){
System.out.println("部门编号:"+d.getDno().trim()+",部门名称:"+d.getDname());
}
}
}
 使用 HQL中 是直接 使用的 javabean 中的 名字,不要搞错了

获得 列表 就是 

            Session.createQuery(HQL).list();

      还可以 设置 从第几个开始显示,并且显示几行,
















  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值