zoukankan      html  css  js  c++  java
  • Hibernate HelloWorld

       Hibernate是ORM 框架,采用面向对象的方式来操作关系数据库,消除冗长的 SQL代码。

       使用Hibernate4版本和MySQL数据库做测试

    1.导jar包

    • antlr-2.7.7.jar
    • dom4j-1.6.1.jar
    • hibernate-c3p0-4.2.4.Final.jar
    • hibernate-commons-annotations-4.0.2.Final.jar
    • hibernate-core-4.2.4.Final.jar
    • hibernate-jpa-2.0-api-1.0.1.Final.jar
    • javassist-3.15.0-GA.jar
    • jboss-logging-3.1.0.GA.jar
    • jboss-transaction-api_1.1_spec-1.0.1.Final.jar
    • mysql-connector-java-5.1.7-bin.jar

    2.配置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.username">root</property>
    		<property name="connection.password">000111</property>
    		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
    		<property name="connection.url">jdbc:mysql://127.0.0.1:3306/test</property>
    		<!-- 配置 hibernate 的基本信息 -->
    		<!-- hibernate 所使用的数据库方言 -->
    		<property name="dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>	
    		<!-- 执行操作时是否在控制台打印 SQL -->
    		<property name="show_sql">true</property>
    		<!-- 是否对 SQL 进行格式化 -->
    		<property name="format_sql">true</property>
    		<!-- 指定自动生成数据表的策略 -->
    		<property name="hbm2ddl.auto">update</property>
    		
    		<!-- 指定关联的 .hbm.xml 文件 -->
    		<mapping resource="com/demo/hibernate/test/News.hbm.xml"/>
    	
    	</session-factory>
    </hibernate-configuration>
    

    3.编写Bean与映射文件

    package com.demo.hibernate.test;
    
    import java.sql.Blob;
    import java.util.Date;
    
    
    public class News {
    	
    	private Integer id;
    	private String title;
    	private String author;
    	
    	private Date date;
    
    	//该属性值为: title: author
    	private String desc;
    	
    	//大文本
    	private String content;
    	//二进制数据
    	private Blob image;
    	
    	
    	
    	public String getContent() {
    		return content;
    	}
    
    	public void setContent(String content) {
    		this.content = content;
    	}
    
    	public Blob getImage() {
    		return image;
    	}
    
    	public void setImage(Blob image) {
    		this.image = image;
    	}
    
    	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 getAuthor() {
    		return author;
    	}
    
    	public void setAuthor(String author) {
    		this.author = author;
    	}
    
    	public Date getDate() {
    		return date;
    	}
    
    	public void setDate(Date date) {
    		this.date = date;
    	}
    
    
    	public String getDesc() {
    		return desc;
    	}
    
    	public void setDesc(String desc) {
    		this.desc = desc;
    	} 
    
    	public News(String title, String author, Date date) {
    		super();
    		this.title = title;
    		this.author = author;
    		this.date = date;
    	}
    	
    	public News() {
    		 
    	}
    
    	@Override
    	public String toString() {
    		return "News [id=" + id + ", title=" + title + ", author=" + author
    				+ ", date=" + date + "]";
    	}
    
    }
    

    News.hbm.xml  

    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    
    <hibernate-mapping package="com.demo.hibernate.test">
    
        <class name="News" table="NEWS" dynamic-insert="true">
        	
            <id name="id" type="java.lang.Integer">
                <column name="ID" />
                <!-- 指定主键的生成方式, native: 使用数据库本地方式 -->
                <generator class="native" />
            </id>
        
            <property name="title" not-null="true" unique="true"
            	index="news_index" length="50"
            	type="java.lang.String" column="TITLE" >
            </property>
            
            <property name="author" type="java.lang.String"
            	index="news_index">
                <column name="AUTHOR" />
            </property>
            
            <property name="date" type="date">
                <column name="DATE" />
            </property>
            
            <property name="desc" 
            	formula="(SELECT concat(title, ',', author) FROM NEWS n WHERE n.id = id)"></property>
            	
    		<!-- 映射大对象 -->
            <!-- 若希望精确映射 SQL 类型, 可以使用 sql-type 属性. -->
    		<property name="content">
    			<column name="CONTENT" sql-type="text"></column>
    		</property>
    		
    		<property name="image" column="PICTURE" type="blob"></property>
    		
        </class>
        
    </hibernate-mapping>
    

    4.测试

    package com.demo.hibernate.test;
    
    import java.util.Date;
    
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.Transaction;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.hibernate.service.ServiceRegistryBuilder;
    import org.junit.After;
    import org.junit.Before;
    import org.junit.Test;
    
    public class TestNews {
    	
    	private SessionFactory sessionFactory;
    	private Session session;
    	private Transaction transaction;
    
    	/**
    	 * 测试方法之前执行
    	 */
    	@Before
    	public void init() {
    		Configuration configuration = new Configuration().configure();
    		ServiceRegistry serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties())
    				.buildServiceRegistry();
    		sessionFactory = configuration.buildSessionFactory(serviceRegistry); 
    		session = sessionFactory.openSession();
    		transaction = session.beginTransaction();
    	}
    
    	/**
    	 * 测试方法之后执行
    	 */
    	@After
    	public void destroy() {
    		transaction.commit();
    		session.close();
    		sessionFactory.close();
    	}
    	
    	/**
    	 * 删除对象记录
    	 */
    	@Test
    	public void testDelete() { 
    		News news = (News) session.get(News.class, 1);
    		session.delete(news); 
    		System.out.println(news);
    	}
    	
    	/**
    	 * 修改对象记录
    	 */
    	@Test
    	public void testUpdate() { 
    		News news1 = (News) session.get(News.class, 1);
    		news1.setContent("test");
    		session.update(news1);
    	}
    	
    	/**
    	 * 获取对象记录
    	 */
    	@Test
    	public void testGet() {
    		News news = (News) session.get(News.class, 1); 
    		System.out.println(news);
    	}
    	
    	
    	/**
    	 * 添加一条记录,主键由数据库负责
    	 */
    	@Test
    	public void testSave() {
    		News news = new News();
    		news.setTitle("CC");
    		news.setAuthor("cc");
    		news.setDate(new Date()); 
    		System.out.println(news); 
    		session.save(news); 
    		System.out.println(news); 
    	}
    }
    
  • 相关阅读:
    URAL 2080 莫队
    Codeforces Round #361 (Div. 2) C D
    UVALive 7297 bfs
    UVALive 7472
    HDU 5773 最长上升子序列
    递归求解最大值和最小值
    数字方阵旋转问题
    实现循环队列的各种基本运算
    实现顺序栈的各种基本运算
    使用两个栈来实现队列
  • 原文地址:https://www.cnblogs.com/walkwithmonth/p/10922685.html
Copyright © 2011-2022 走看看