zoukankan      html  css  js  c++  java
  • hibernate简单注释(一)

    *****************************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">com.mysql.jdbc.Driver</property>
    <property name="connection.url">jdbc:mysql://localhost:3306/hibernate</property>
    <property name="connection.username">root</property>
    <property name="connection.password">123456</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</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">create</property>

    <!-- Names the annotated entity class -->
    <mapping class="com.ij34.dao.New"/>

    </session-factory>

    </hibernate-configuration>

    ****************************************************************************************

    package com.ij34.dao;

    import javax.persistence.*;

    @Entity
    @Table(name="test01")
    public class New {
    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private int id;
    private String title;
    private String content;
    public int getId() {
    return id;
    }
    public void setId(int id) {
    this.id = id;
    }
    public String getTitle() {
    return title;
    }
    public void setTitle(String title) {
    this.title = title;
    }
    public String getContent() {
    return content;
    }
    public void setContent(String content) {
    this.content = content;
    }


    }

    *********************************************

    package com.ij34.web;

    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.*;

    import com.ij34.dao.New;

    public class test01 {
    public static void main(String[] args)throws Exception {
    //实例化Configuration
    Configuration conf=new Configuration().configure();
    ServiceRegistry SR=new StandardServiceRegistryBuilder().applySettings(conf.getProperties()).build();
    // 以Configuration实例创建SessionFactory实例
    SessionFactory SF=conf.buildSessionFactory(SR);
    //create session
    Session session=SF.openSession();
    //start 事务
    Transaction tx=session.beginTransaction();
    New n=new New();
    n.setTitle("hello");
    n.setContent("hello world");
    session.save(n);
    tx.commit();
    session.close();
    SF.close();
    }
    }


    附连sqlserver,hibernate5

    <?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">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <!-- 指定连接数据库的url,其中hibernate是本应用连接的数据库名 -->
            <property name="connection.url">jdbc:sqlserver://localhost:1433;DatabaseName=hibernatedb</property>
            <!-- 指定连接数据库的用户名 -->
            <property name="connection.username">sa</property>
            <!-- 指定连接数据库的密码 -->
            <property name="connection.password">123456</property>
            <!-- 指定连接池里最大连接数 -->
            <property name="hibernate.c3p0.max_size">20</property>
            <!-- 指定连接池里最小连接数 -->
            <property name="hibernate.c3p0.min_size">1</property>
            <!-- 指定连接池里连接的超时时长 -->
            <property name="hibernate.c3p0.timeout">5000</property>
            <!-- 指定连接池里最大缓存多少个Statement对象 -->
            <property name="hibernate.c3p0.max_statements">100</property>
            <property name="hibernate.c3p0.idle_test_period">3000</property>
            <property name="hibernate.c3p0.acquire_increment">2</property>
            <property name="hibernate.c3p0.validate">true</property>
            <!-- 指定数据库方言 -->
            <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
            <property name="hibernate.show_sql">true</property>
            <!-- 根据需要自动创建数据表 -->
            <property name="hbm2ddl.auto">update</property><!---->
            <!-- 显示Hibernate持久化操作所生成的SQL -->
            <property name="show_sql">true</property>
            <!-- 将SQL脚本进行格式化后再输出 -->
            <property name="hibernate.format_sql">true</property>
            <!-- 罗列所有持久化类的类名 -->
            <mapping class="com.ij34.dao.News"/>
        </session-factory>
    </hibernate-configuration>
    package com.ij34.dao;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    
    
    @Entity(name="news")
    public class News {
        
        @Id
        @GeneratedValue(strategy=GenerationType.IDENTITY)
        private Integer id;
        @Column(name="title")
        private String title;
        @Column(name="content")
        private String content;
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public String getTitle() {
            return title;
        }
        public void setTitle(String title) {
            this.title = title;
        }
        public String getContent() {
            return content;
        }
        public void setContent(String content) {
            this.content = content;
        }
        
    
    }
    package com.ij34.test;
    
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.boot.MetadataSources;
    import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
    import org.hibernate.service.ServiceRegistry;
    
    import com.ij34.dao.News;
    
    
    
    public class Test {
    
        public static void main(String[] args) {
            // TODO Auto-generated method stub
           
    /*        Configuration cfg = new Configuration()
                    .configure();*/
            ServiceRegistry sr = new StandardServiceRegistryBuilder().configure().build();
            SessionFactory sessionfactory = new MetadataSources(sr).buildMetadata().buildSessionFactory();
            //SessionFactory sessionfactory = cfg.buildSessionFactory(serviceregistry);
            Session session = sessionfactory.openSession();
            Transaction tx = session.beginTransaction();
            News news = new News();
            news.setTitle("2018标题");
            news.setContent("201820182018内容内容");
            session.save(news);
            tx.commit();
            session.close();
            sessionfactory.close();
                    
        }
    
    }
    <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">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.ij34</groupId>
      <artifactId>HibernateQS</artifactId>
      <packaging>war</packaging>
      <version>0.0.1-SNAPSHOT</version>
      <name>HibernateQS Maven Webapp</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        
            <dependency> 
          <groupId>org.hibernate</groupId>
          <artifactId>hibernate-core</artifactId>
          <version>5.1.0.Final</version>
        </dependency>
        
            <dependency>
          <groupId>log4j</groupId>
          <artifactId>log4j</artifactId>
          <version>1.2.17</version>
        </dependency>
        
                <dependency>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-nop</artifactId>
                <version>1.6.5</version>
            </dependency>
     
            <!-- 添加javassist -->
            <dependency>
                <groupId>javassist</groupId>
                <artifactId>javassist</artifactId>
                <version>3.12.0.GA</version>
            </dependency>
            
            <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>sqljdbc4</artifactId>
        <version>4.0</version>
       </dependency>
    
          <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.1</version>
        </dependency>
        
        <dependency>
            <groupId>dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>1.6.1</version>
        </dependency>
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>ejb3-persistence</artifactId>
        <version>3.3.2.Beta1</version>
    </dependency>
    
    <dependency>
        <groupId>org.hibernate.javax.persistence</groupId>
        <artifactId>hibernate-jpa-2.1-api</artifactId>
        <version>1.0.2.Final</version>
    </dependency>
    
    <dependency>
        <groupId>org.hibernate.common</groupId>
        <artifactId>hibernate-commons-annotations</artifactId>
        <version>5.0.1.Final</version>
    </dependency>
    
    <dependency>
        <groupId>antlr</groupId>
        <artifactId>antlr</artifactId>
        <version>2.7.7</version>
    </dependency>
    
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-osgi</artifactId>
        <version>5.1.0.Final</version>
    </dependency>
    
    <dependency>
        <groupId>hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>3.4.0.GA</version>
        <type>pom</type>
    </dependency>
    
      </dependencies>
      <build>
        <finalName>HibernateQS</finalName>
      </build>
        <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
      </properties>
    </project>

  • 相关阅读:
    python第三十二课——队列
    python第三十二课——栈
    python提示警告InsecureRequestWarning
    关于requests.exceptions.SSLError: HTTPSConnectionPool
    python第三十一课--递归(3.递归的弊端)
    01 redis特点及安装使用
    22 nginx配置与集群
    21-nginx单机1W并发优化
    20-ab压力测试及nginx性能统计模块
    19 大网站的优化思路
  • 原文地址:https://www.cnblogs.com/tk55/p/5955953.html
Copyright © 2011-2022 走看看