zoukankan      html  css  js  c++  java
  • 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("角色编号"+"		"+"角色姓名");
            for(Role role:list){
                System.out.println(role.getId()+"		"+role.getRolename());
            }
        }

    7.测试结果

  • 相关阅读:
    Ajax
    Linux安装SmartSVN及破解
    JQuery异步提交
    动画效果
    事件
    表单选择器
    DOM操作
    JQuery基础
    PHP环境配置
    DP--钢条切割
  • 原文地址:https://www.cnblogs.com/yw0219/p/7123666.html
Copyright © 2011-2022 走看看