zoukankan      html  css  js  c++  java
  • [Hibernate]

    1)解压Hibernate,在eclipse中导入jar包,其中lib equired里的jar包是必需包括在里头的。这里用的是sql server,所以要导入sqljdbc4.jar

    2)在src根目录下新建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.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
            <property name="connection.url">jdbc:sqlserver://127.0.0.1;DatabaseName=DBNAME;integratedSecurity=True;</property>  
            <property name="connection.username"></property>
            <property name="connection.password"></property>
    
            <property name="connection.pool_size">2</property>
            <property name="dialect">org.hibernate.dialect.SQLServer2008Dialect</property>
            <property name="show_sql">true</property>
    
            <mapping resource="com/my/test/mapping/Account.hbm.xml" />
        </session-factory>
    </hibernate-configuration>

    3)加入Account.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>
        <class name="com.my.bean.Account" table="PPM_Account">
            <id name="accountId" type="java.lang.String">
                <column name="AccountId" />
                <generator class="assigned" />
            </id>
            <property name="targetType" type="java.lang.String" length="30">
                <column name="TargetType" />
            </property>
        </class>
    </hibernate-mapping>

    4)加入bean:Account.java

    package com.my.bean;
    
    public class Account {
        
        private int id;
        public int getId() {
            return id;
        }
    
        public void setId(int id) {
            this.id = id;
        }
        
        public String accountId;
        public String getAccountId() {
            return accountId;
        }
    
        public void setAccountId(String accountId) {
            this.accountId = accountId;
        }
    
        private String targetType;
        public String getTargetType() {
            return targetType;
        }
    
        public void setTargetType(String targetType) {
            this.targetType = targetType;
        }
    
    }

    5)测试Hibernate:

    package com.my.test;
    
    import java.util.List;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    import com.my.bean.Account;
    
    public class TestHibernate {
    
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            Configuration cfg = new Configuration();
            @SuppressWarnings("deprecation")
            SessionFactory factory = cfg.configure().buildSessionFactory();
            Session session = factory.openSession();
            org.hibernate.Transaction trans = session.beginTransaction();
    
            String hql = "from Account";
            Query query = session.createQuery(hql);
            List<Account> list = query.list();
    
            trans.commit();
            session.close();
    
            for (Account account : list) {
                System.out.println(account.getTargetType());
            }
    
        }
    
    }

    也可以使用HSQL的Select来写:

    package com.my.test;
    
    import java.util.List;
    
    import org.hibernate.Query;
    import org.hibernate.Session;
    import org.hibernate.SessionFactory;
    import org.hibernate.cfg.Configuration;
    
    import com.my.bean.Account;
    
    public class TestHibernate {
    
        @SuppressWarnings("unchecked")
        public static void main(String[] args) {
            Configuration cfg = new Configuration();
            @SuppressWarnings("deprecation")
            SessionFactory factory = cfg.configure().buildSessionFactory();
            Session session = factory.openSession();
            org.hibernate.Transaction trans = session.beginTransaction();
    
            String hql = "select new Account(accountId, targetType) from Account";
            Query query = session.createQuery(hql);
            List<Account> list = query.list();
    
            trans.commit();
            session.close();
    
            for(Account account : list){
                System.out.println(account.getAccountId());
            }
    
        }
    
    }

    HSQL的Where条件可以这样写:

            String hql = "select new Account(accountId, targetType) from Account where accountId=:accountId";
            Query query = session.createQuery(hql);
            query.setParameter("accountId", "7AC8352C-9F6B-4B06-A481-FFEFAC7B3E7D");
            List<Account> list = query.list();
  • 相关阅读:
    jQuery工具函数
    jqXHR对象
    跨域获取
    Ajax :六个全局事件
    表单序列化
    Ajax : $. get()和$.post() $.getScript $.getJSON
    Ajax : load()
    列队动画
    关于MindManager显示不同级别的控制
    Mybatis 查询传多个参数(3中方法)
  • 原文地址:https://www.cnblogs.com/HD/p/3688473.html
Copyright © 2011-2022 走看看