Hibernate学习(1):查询demo

1.数据库(mysql)创建脚本

DROP TABLE IF EXISTS role;
 CREATE TABLE IF NOT EXISTS `role`(
  `id` INT(11) NOT NULL AUTO_INCREMENT,
  `rolename` VARCHAR(20) DEFAULT NULL,
  `note` VARCHAR(100) DEFAULT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
INSERT INTO role VALUES(NULL,'管理员','admin');
INSERT INTO role VALUES(NULL,'用户','user');

2.maven依赖

        <!-- hibernate core -->
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate</artifactId>
            <version>3.2.1.ga</version>
        </dependency>
        <!-- database mysql -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.18</version>
        </dependency>

实际生成jar包截图

3.创建Hibernate总配置文件

<?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="dialect">
            org.hibernate.dialect.MySQLDialect
        </property>
        <property name="connection.url">
            jdbc:mysql://localhost:3306/mysql
        </property>
        <property name="connection.username">root</property>
        <property name="connection.password">mysql</property>
        <property name="connection.driver_class">
            com.mysql.jdbc.Driver
        </property>
        <property name="myeclipse.connection.profile">mysql</property>
        <mapping resource="com/yan/pojo/Role.hbm.xml" />

    </session-factory>

</hibernate-configuration>

4.创建实体类Role.java

public class Role {

    private Integer id;
    private String rolename;
    private String note;

    // Constructors

    /** default constructor */
    public Role() {
    }

    /** minimal constructor */
    public Role(Integer id) {
        this.id = id;
    }

    /** full constructor */
    public Role(Integer id, String rolename, String note) {
        this.id = id;
        this.rolename = rolename;
        this.note = note;
    }

    // Property accessors

    public Integer getId() {
        return this.id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getRolename() {
        return this.rolename;
    }

    public void setRolename(String rolename) {
        this.rolename = rolename;
    }

    public String getNote() {
        return this.note;
    }

    public void setNote(String note) {
        this.note = note;
    }

}

5.创建实体类与数据库的映射文件

<?xml version="1.0" encoding="utf-8"?>
<!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.yan.pojo.Role" table="role" catalog="mysql">
        <id name="id" type="java.lang.Integer">
            <column name="id" />
            <generator class="assigned" />
        </id>
        <property name="rolename" type="java.lang.String">
            <column name="rolename" length="20" />
        </property>
        <property name="note" type="java.lang.String">
            <column name="note" length="100" />
        </property>
    </class>
</hibernate-mapping>

6.创建测试类并增加main方法

public static void main(String[] args) {
        Session session=new Configuration().configure().buildSessionFactory().openSession();
        String hql="from Role";
        Query query=session.createQuery(hql);
        @SuppressWarnings("unchecked")
        List<Role> list=query.list();
        System.out.println("角色编号"+"\t\t"+"角色姓名");
        for(Role role:list){
            System.out.println(role.getId()+"\t\t"+role.getRolename());
        }
    }

7.测试结果

posted @ 2017-07-05 21:52  舒山  阅读(243)  评论(0编辑  收藏  举报