ThreadLocal

Posted Onlyだ

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ThreadLocal相关的知识,希望对你有一定的参考价值。

public class Test1 {
    
    public static void main(String[] args) {
        Class clazz=Student.class;
        System.out.println(clazz);
    }
     Session session;
     Transaction tx;
     @Before
     public void initData(){
         session = HibernateUtil.getSession();
         tx= session.beginTransaction();
     }

    
    @After
    
    public void endTest(){
        
        tx.commit();
         HibernateUtil.getSession();

    }
    
    //添加数据
     @Test
      public void testAdd(){
          //1.1构建一个学生对象
          Student stu=new Student();
          stu.setAge(22);
          stu.setName("金龙");
          stu.setId(8);
         
          Configuration cf=new Configuration().configure("hibernate.cfg.xml");
          SessionFactory factory = cf.buildSessionFactory();
         session = factory.openSession();
           tx = session.beginTransaction();
          //1.3保存   
          session.save(stu);
    
     }
     
     
     //检索所有列
     @Test
     public void AllColumn(){
         session = HibernateUtil.getSession();
         String hql="from Student"; 
         Query query=session.createQuery(hql);
         List<Student>list=query.list();
         for (Student student : list) {
            System.out.println(student);
        }
         
         
         
     }
     
     
     //检索某些列
     @Test
     public void someColumn(){
         
         session = HibernateUtil.getSession();
         String hql="select name,age from Student"; 
         Query query=session.createQuery(hql);
         List<Object[]>list=query.list();
        for (Object[] item : list) {
            System.out.println(((Student)item[0]));
        }
        }
     
     @Test
     public void  LoadTest(){
         
        Student stu=(Student)session.load(Student.class, 3);
        System.out.println(stu);
      
        
         
     }
     
     
     //删除数据
     @Test
        public void delTest(){
            
             Student stu=new Student();
             stu.setId(3);
             session.delete(stu);
            
             System.out.println("del ok!");
        }
     
     
     
     @Test
        public void getTest(){
             Student stu=(Student)session.get(Student.class, 3);
             System.out.println("=============");
             Student stu2=(Student)session.load(Student.class, 3);
             
            
        }
     
     
     //有名称占位符
     @Test
     public void named(){
         session = HibernateUtil.getSession();
         String hql="from Student where name=:name"; 
         Query query=session.createQuery(hql);
         
         query.setParameter("name","金龙");
          List<Student>list=query.list();
         for (Student item : list) {
            
             System.out.println(item.getName());
        }
     }
     
     
     //模糊查询
     @Test
        public void likeTest(){
            String hql="from Student where name like ‘%金%‘";
            Query query = session.createQuery(hql);
            
            List<Student> list = query.list();
            
            for (Student item : list) {
            
                System.out.println(item.getName());
            }
        }
     
     
     //动态加载
   @Test
   public void dynamicSelectTest(){
        
        StudentCondition condition=new StudentCondition();
        condition.setAge(22);
        condition.setName("金龙");
        
        StringBuilder sb=new StringBuilder();
        sb.append("from Student where 1=1 ");
        Student stu=new Student();
        
        if (condition.getAge()!=null) {
            sb.append(" and age=:age");
            stu.setAge(condition.getAge());
        }
        
        if(condition.getName()!=null){
            sb.append(" and name=:name");
            stu.setName(condition.getName());
        }
        
        Query query=session.createQuery(sb.toString());
        List<Student> list = query.list();
       
       for (Student item : list) {
      
            System.out.println(item.getName()+item.getAge());
        }
    }
   
   
   
   //匿名占位符
     @Test
     public void anonymous (){
         session = HibernateUtil.getSession();
         String hql="from Student where name=?"; 
         Query query=session.createQuery(hql);
         
         query.setParameter(0,"金龙");
          List<Student>list=query.list();
         for (Student item : list) {
            
             System.out.println(item.getName());
        }
     }
     
    }

      

 

以上是关于ThreadLocal的主要内容,如果未能解决你的问题,请参考以下文章

源代码系列02——ThreadLocal源码分析(基础篇)

java中的ThreadLocal详解及示例代码

ThreadLocal介绍

MyBatis基础:使用java提供的ThreadLocal类优化代码

Java 单线程代码ThreadLocal串值问题

ThreadLocal