6月5日 SSH 周二



<hibernate-configuration>
    <session-factory>
    
    <property name="connection.driver_class">com.mysql.jdbc.Driverproperty>
    
    <property name="connection.url">jdbc:mysql:///cms09property>
    
    <property name="connection.username">rootproperty>
    
    <property name="connection.password">rootproperty>
    
    <property name="dialect">org.hibernate.dialect.MySQLDialectproperty>

    
    

     <property name="hbm2ddl.auto">updateproperty>
     
     <property name="show_sql">trueproperty>
     
     <property name="format_sql">trueproperty>
     
     <mapping resource="com/dto/Cat.hbm.xml"/>

    session-factory>
hibernate-configuration>


    

<hibernate-mapping>
    
    <class name="com.dto.Cat" table="t_cat">
        
        <id name="id" column="cid" type="java.lang.Integer">
            <generator class="native">generator>
        id>
        <property name="name" column="cname" type="java.lang.String">property>
        <property name="sex">property>
        <property name="age">property>
    class>
hibernate-mapping>
package com.dto;

public class Cat {
    private int id;
    private String name;
    private String sex;
    private int age;
    public Cat() {
        super();
        // TODO Auto-generated constructor stub
    }
    public Cat(int id, String name, String sex, int age) {
        super();
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    @Override
    public String toString() {
        return "Cat [id=" + id + ", name=" + name + ", sex=" + sex + ", age="
                + age + "]";
    }
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getSex() {
        return sex;
    }
    public void setSex(String sex) {
        this.sex = sex;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }


}
package com.demo;

import java.util.List;

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

import com.dto.Cat;

public class Demo {
    public static void main(String[] args) {
        //加载hibernate.cfg.xml;默认加载的路径:/hibernate.cfg.xml
        Configuration config = new Configuration().configure();
        //用Confiiguration对象创建一个SessionFactory
        SessionFactory sessionFactory = config.buildSessionFactory();
        //从sessionfactory得到一个session对象
        //session是对Connection的高级封装
        Session session = sessionFactory.openSession();

        String hql = " from Cat ";
        //session创建一个Query对象
        Query query = session.createQuery(hql);
        //执行查询
        List list = query.list();
        for (Cat c : list) {
            System.out.println(c);
        }
    }
}

耐性是天才必不可少的素质之一
——爱默生

你可能感兴趣的:(2018)