码迷,mamicode.com
首页 > 其他好文 > 详细

关于缓存一些问题

时间:2017-12-25 00:59:34      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:jdk   info   value   repos   XML   数据库驱动   server   static   adl   

1.新建一个工具类

技术分享图片

2.编写代码

package cn.happy.day01.util;

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

/**
 * Created by CY on 2017/12/23.
 */
public class HibernateUtil {
    private static ThreadLocal<Session> tl=new ThreadLocal<Session>();
    private static Configuration cfg;
    private static SessionFactory sessionFactory;
    static {
        cfg=new Configuration().configure();
        sessionFactory=cfg.buildSessionFactory();
    }
    public static  Session getSession(){
        Session session=tl.get();
        if(session==null){
             session = sessionFactory.openSession();
           tl.set(session);
        }
        return session;
    }
    public static void closeSession(){
        Session session = (Session) tl.get();
        tl.set(null);
        session.close();
    }
}

注意这里使用的是openSession();

3.大配置小配置:不用解了

<?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="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
        <property name="connection.url">jdbc:oracle:thin:@localhost:1521:orcl</property>
        <property name="connection.username">happy</property>
        <property name="connection.password">happy</property>
        <!--sql 方言-->
        <property name="dialect">org.hibernate.dialect.Oracle10gDialect</property>
        <!--连接池-->
        <!--<property name="connection.pool_size">1</property>-->
        <!--和当前线程绑定-->
        <property name="current_session_context_class">thread</property>
        <!--echo 打印控制台语句 shout-->
        <property name="show_sql">true</property>
        <!--格式化代码-->
        <property name="format_sql">true</property>
        <!--自动更新表结构 createe  先delete表结构 在创建  update直接更新表结构-->
        <property name="hbm2ddl.auto">update</property>
        <mapping resource="cn/happy/day01/entity/Dog.hbm.xml" />
        <mapping resource="cn/happy/day01/entity/Student.hbm.xml" />
    </session-factory>

</hibernate-configuration>
@Entity
public class Student {
    private  Integer sid;
    private  String sname;
    private  Integer sage;
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC
        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="cn.happy.day01.entity">
    <class name="Student" table="STUDENT" schema="happy">
        <id name="sid"  column="sid">
            <!-- native   数据库方言的可移植性 -->
            <generator class="native"/>
        </id>
        <property name="sname" column="sname" />
        <property name="sage"  column="sage"/>
    </class>
</hibernate-mapping>

4.数据库使用的是Oracle

/*
Navicat Oracle Data Transfer
Oracle Client Version : 11.2.0.1.0

Source Server         : happy
Source Server Version : 110200
Source Host           : localhost:1521
Source Schema         : HAPPY

Target Server Type    : ORACLE
Target Server Version : 110200
File Encoding         : 65001

Date: 2017-12-24 22:31:33
*/


-- ----------------------------
-- Table structure for STUDENT
-- ----------------------------
DROP TABLE "HAPPY"."STUDENT";
CREATE TABLE "HAPPY"."STUDENT" (
"SID" NUMBER(10) NOT NULL ,
"SNAME" VARCHAR2(255 CHAR) NULL ,
"SAGE" NUMBER(10) NULL 
)
LOGGING
NOCOMPRESS
NOCACHE

;

-- ----------------------------
-- Records of STUDENT
-- ----------------------------
INSERT INTO "HAPPY"."STUDENT" VALUES (‘4‘, ‘小红‘, ‘12‘);

-- ----------------------------
-- Indexes structure for table STUDENT
-- ----------------------------

-- ----------------------------
-- Checks structure for table STUDENT
-- ----------------------------
ALTER TABLE "HAPPY"."STUDENT" ADD CHECK ("SID" IS NOT NULL);

-- ----------------------------
-- Primary Key structure for table STUDENT
-- ----------------------------
ALTER TABLE "HAPPY"."STUDENT" ADD PRIMARY KEY ("SID");

5.使用的jar包

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <parent>
        <artifactId>Y2166</artifactId>
        <groupId>cn.happy</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>hibernate</artifactId>
    <packaging>war</packaging>
    <name>hibernate Maven Webapp</name>
    <url>http://maven.apache.org</url>
    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.3</version>
            <scope>test</scope>
        </dependency>
        <!--oracle jdbc-->
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.1.0</version>
        </dependency>
        <!--hibernate核心jar-->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.0.6.Final</version>
        </dependency>
        <!--oracle 事务-->
        <dependency>
            <groupId>javax.transaction</groupId>
            <artifactId>jta</artifactId>
            <version>1.1</version>
        </dependency>
        <!--mysql数据库驱动-->
        <dependency>
            <groupId>org.wisdom-framework</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.34_1</version>
        </dependency>

    </dependencies>
    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.*</include>
                </includes>
            </resource>
        </resources>
    </build>
</project>

6.测试类

数据库修改前:

技术分享图片

测试修改

package day02hql;

import cn.happy.day01.entity.Student;
import cn.happy.day01.util.HibernateUtil;
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 java.util.List;

/**
 * 脏检查和缓存机制探讨
 */
public class mytest20171224_02 {
    Configuration cfg;
    SessionFactory factory;
    Session session;
    Transaction tx;
    @Before
    public void before(){
        cfg=new Configuration().configure();
        factory=cfg.buildSessionFactory();
        session=factory.getCurrentSession();
        tx=session.beginTransaction();
    }


    @Test
    public void t1(){
        //使用update更新数据
        Student student = session.load(Student.class, 4);
        student.setSname("小贱贱");
        session.update(student);
        tx.commit();
        //在数据同步情况下更新数据
        Student student1 = HibernateUtil.getSession().load(Student.class, 4);
        student1.setSname("小军");
        HibernateUtil.closeSession();
    }
}

数据库运行后

技术分享图片

运行结果:

 可以看到数据库确实是更新了

但是控制台却打印出来两条查询语句

 

E:\jdk\bin\java -ea -Didea.launcher.port=7535 "-Didea.launcher.bin.path=E:\ideat\IntelliJ IDEA 2016.3.2\bin" -Dfile.encoding=UTF-8 -classpath "E:\ideat\IntelliJ IDEA 2016.3.2\lib\idea_rt.jar;E:\ideat\IntelliJ IDEA 2016.3.2\plugins\junit\lib\junit-rt.jar;E:\jdk\jre\lib\charsets.jar;E:\jdk\jre\lib\deploy.jar;E:\jdk\jre\lib\ext\access-bridge-32.jar;E:\jdk\jre\lib\ext\cldrdata.jar;E:\jdk\jre\lib\ext\dnsns.jar;E:\jdk\jre\lib\ext\jaccess.jar;E:\jdk\jre\lib\ext\jfxrt.jar;E:\jdk\jre\lib\ext\localedata.jar;E:\jdk\jre\lib\ext\nashorn.jar;E:\jdk\jre\lib\ext\sunec.jar;E:\jdk\jre\lib\ext\sunjce_provider.jar;E:\jdk\jre\lib\ext\sunmscapi.jar;E:\jdk\jre\lib\ext\sunpkcs11.jar;E:\jdk\jre\lib\ext\zipfs.jar;E:\jdk\jre\lib\javaws.jar;E:\jdk\jre\lib\jce.jar;E:\jdk\jre\lib\jfr.jar;E:\jdk\jre\lib\jfxswt.jar;E:\jdk\jre\lib\jsse.jar;E:\jdk\jre\lib\management-agent.jar;E:\jdk\jre\lib\plugin.jar;E:\jdk\jre\lib\resources.jar;E:\jdk\jre\lib\rt.jar;E:\Y2166\hibernate\target\test-classes;E:\Y2166\hibernate\target\classes;E:\mavon\repository\junit\junit\4.3\junit-4.3.jar;E:\mavon\repository\com\oracle\ojdbc6\11.2.0.1.0\ojdbc6-11.2.0.1.0.jar;E:\mavon\repository\org\hibernate\hibernate-core\5.0.6.Final\hibernate-core-5.0.6.Final.jar;E:\mavon\repository\org\jboss\logging\jboss-logging\3.3.0.Final\jboss-logging-3.3.0.Final.jar;E:\mavon\repository\org\hibernate\javax\persistence\hibernate-jpa-2.1-api\1.0.0.Final\hibernate-jpa-2.1-api-1.0.0.Final.jar;E:\mavon\repository\org\javassist\javassist\3.18.1-GA\javassist-3.18.1-GA.jar;E:\mavon\repository\antlr\antlr\2.7.7\antlr-2.7.7.jar;E:\mavon\repository\org\jboss\jandex\2.0.0.Final\jandex-2.0.0.Final.jar;E:\mavon\repository\dom4j\dom4j\1.6.1\dom4j-1.6.1.jar;E:\mavon\repository\xml-apis\xml-apis\1.0.b2\xml-apis-1.0.b2.jar;E:\mavon\repository\org\hibernate\common\hibernate-commons-annotations\5.0.1.Final\hibernate-commons-annotations-5.0.1.Final.jar;E:\mavon\repository\javax\transaction\jta\1.1\jta-1.1.jar;E:\mavon\repository\org\wisdom-framework\mysql-connector-java\5.1.34_1\mysql-connector-java-5.1.34_1.jar;E:\mavon\repository\org\apache\felix\org.apache.felix.ipojo.annotations\1.12.1\org.apache.felix.ipojo.annotations-1.12.1.jar;E:\mavon\repository\org\wisdom-framework\abstract-jdbc-driver\0.5\abstract-jdbc-driver-0.5.jar" com.intellij.rt.execution.application.AppMain com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 day02hql.mytest20171224_02,t1
十二月 24, 2017 10:36:14 下午 org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {5.0.6.Final}
十二月 24, 2017 10:36:14 下午 org.hibernate.cfg.Environment <clinit>
INFO: HHH000206: hibernate.properties not found
十二月 24, 2017 10:36:14 下午 org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
十二月 24, 2017 10:36:14 下午 org.hibernate.annotations.common.reflection.java.JavaReflectionManager <clinit>
INFO: HCANN000001: Hibernate Commons Annotations {5.0.1.Final}
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:orcl]
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=happy, password=****}
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 24, 2017 10:36:15 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 24, 2017 10:36:16 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
十二月 24, 2017 10:36:16 下午 org.hibernate.id.UUIDHexGenerator <init>
WARN: HHH000409: Using org.hibernate.id.UUIDHexGenerator which does not generate IETF RFC 4122 compliant UUID values; consider using org.hibernate.id.UUIDGenerator instead
十二月 24, 2017 10:36:17 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Hibernate: 
    select
        student0_.sid as sid1_1_0_,
        student0_.sname as sname2_1_0_,
        student0_.sage as sage3_1_0_ 
    from
        happy.STUDENT student0_ 
    where
        student0_.sid=?
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
WARN: HHH10001002: Using Hibernate built-in connection pool (not for production use!)
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001005: using driver [oracle.jdbc.driver.OracleDriver] at URL [jdbc:oracle:thin:@localhost:1521:orcl]
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001001: Connection properties: {user=happy, password=****}
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.DriverManagerConnectionProviderImpl buildCreator
INFO: HHH10001003: Autocommit mode: false
十二月 24, 2017 10:36:18 下午 org.hibernate.engine.jdbc.connections.internal.PooledConnections <init>
INFO: HHH000115: Hibernate connection pool size: 20 (min=1)
十二月 24, 2017 10:36:18 下午 org.hibernate.dialect.Dialect <init>
INFO: HHH000400: Using dialect: org.hibernate.dialect.Oracle10gDialect
十二月 24, 2017 10:36:18 下午 org.hibernate.tool.hbm2ddl.SchemaUpdate execute
INFO: HHH000228: Running hbm2ddl schema update
Hibernate: 
    select
        student0_.sid as sid1_1_0_,
        student0_.sname as sname2_1_0_,
        student0_.sage as sage3_1_0_ 
    from
        happy.STUDENT student0_ 
    where
        student0_.sid=?

Process finished with exit code 0

?待讨论

关于缓存一些问题

标签:jdk   info   value   repos   XML   数据库驱动   server   static   adl   

原文地址:http://www.cnblogs.com/lcycn/p/8099580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!