zoukankan      html  css  js  c++  java
  • SSH 整合- 3 - add - hibernate

    1. 流程图

     

    SSH <wbr>整合- <wbr>3 <wbr>- <wbr>add <wbr>- <wbr>hibernate

     

    2. 主要文件

     

    SSH <wbr>整合- <wbr>3 <wbr>- <wbr>add <wbr>- <wbr>hibernate
         

            你可以直接用Myeclipse把hibernate的jar添加进来(右键工程名—》MyEclipse —》Add Hibernate Capabilities, 之后按照向导选择需要的jar就可以了),也可以自己手动添加。在章节里没有提及的文件都是不用修改的,你可以在前面的文章里看到它们的内容。

     

    3. User.java

    package com.edu.ssh.model;

     

    import javax.persistence.Entity;

    import javax.persistence.GeneratedValue;

    import javax.persistence.Id;

    import javax.persistence.Table;

     

    @Entity

    @Table(name="tb_user")  //当实体和数据库的表名不一致时要注明

    public class User {

    private int id;

    private String username;

    private String password;

     

     

    @Id

    @GeneratedValue

    public int getId() {

    return id;

    }

     

     

    public void setId(int id) {

    this.id = id;

    }

     

     

    public String getUsername() {

    return username;

    }

     

     

    public void setUsername(String username) {

    this.username = username;

    }

     

     

    public String getPassword() {

    return password;

    }

     

     

    public void setPassword(String password) {

    this.password = password;

    }

    }

     

     

    4. 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>

        <!-- 

        当spring也整合进来后这些文件都可放在spring的配置文件里。

        hibernate.cfg.xml整个文件都不需要了,到时就可以删除了。

        hibernate全部的配置都可交给spring管理。

        -->

            <property name="connection.driver_class">com.mysql.jdbc.Driver</property>

            <property name="connection.url">jdbc:mysql://localhost:3306/ssh</property>

            <property name="connection.username">root</property>

            <property name="connection.password">mysql123</property>

            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

     

            <!-- JDBC connection pool (use the built-in) -->

            <property name="connection.pool_size">1</property>

     

            <!-- Enable Hibernate's automatic session context management -->

            <property name="current_session_context_class">thread</property>

     

            <!-- Disable the second-level cache  -->

            <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

     

            <!-- Echo all executed SQL to stdout -->

            <property name="show_sql">true</property>

            <property name="format_sql">true</property>

     

            <!-- Drop and re-create the database schema on startup

            <property name="hbm2ddl.auto">update</property>

    -->

     

            <mapping class="com.edu.ssh.model.User"/>

     

        </session-factory>

     

    </hibernate-configuration>

     

    5. HibernateUtil

    package com.edu.ssh.util;

     

    import org.hibernate.SessionFactory;

    import org.hibernate.cfg.AnnotationConfiguration;

     

     

    public class HibernateUtil {

    private static SessionFactory sf;

    static {

            // 用到了hibernate的注解,所有要用这个类AnnotationConfiguration

    sf = new AnnotationConfiguration().configure().buildSessionFactory();

    }

     

    public static SessionFactory getSessionFactory() {

    return sf;

    }

    }

     

    6. UserManager.java

    package com.edu.ssh.service;

     

    import org.hibernate.Session;

    import org.hibernate.SessionFactory;

     

    import com.edu.ssh.model.User;

    import com.edu.ssh.util.HibernateUtil;


    public class UserManager {

     

    public boolean exists(User user) {

    SessionFactory sf = HibernateUtil.getSessionFactory();

    Session session = sf.getCurrentSession();

    session.beginTransaction();

    long count = (Long) session.createQuery(

    "select count(*) from User u where u.username = :username")

    .setString("username", user.getUsername()).uniqueResult();

     

    session.beginTransaction().commit();

     

    if (count > 0)

    return true;

    return false;

    }

     

    public void insertOneUser(User user) {

    // save in database

    SessionFactory sf = HibernateUtil.getSessionFactory();

    Session session = sf.getCurrentSession();

    session.beginTransaction();

    session.save(user);

    session.beginTransaction().commit();

    }

    }

     

     

    7. UserManagerTest.java

    package com.edu.ssh.service;

     

    import static org.junit.Assert.*;

     

    import org.junit.Before;

    import org.junit.Test;

     

    import com.edu.ssh.model.User;

     

    public class UserManagerTest {

     

    private UserManager userManager = null;

     

    @Before

    public void setUp() {

    userManager = new UserManager();

    }

     

     

    @Test

    public void testExists() {

    User user = new User();

    user.setUsername("ee");

    assertTrue("userManager.exists(user) 失败了!!", userManager.exists(user));

    }

     

     

    @Test

    public void testInsertOneUser() {

    User user = new User();

    user.setUsername("username");

    user.setPassword("password");

    userManager.insertOneUser(user);

    }

     

    }

     

    8. 结果

     

    SSH <wbr>整合- <wbr>3 <wbr>- <wbr>add <wbr>- <wbr>hibernate

     

     

    SSH <wbr>整合- <wbr>3 <wbr>- <wbr>add <wbr>- <wbr>hibernate

    9. 简单分析

    在工程的dao层用hibernate实现,hibernate比起直接使用jdbc简化了很多,要写的代码比起jdbc少了很多很多,如果用jdbc实现,我们必须写很多重复的代码。当然使用jdbc性能比hibernate好很多。hibernate把jdbc封装了。我们只需对对象操作就可以了,不像使用jdbc直接对表结构操作。更接近人的思维。也是ssh整合进来的第一个框架。

    更多关于Hibernate教程看看这里:
    www.iteye.com/blogs/tag/Hibernate

    版权声明:本文为博主原创文章,未经博主允许不得转载。

  • 相关阅读:
    Windows内存布局 / MmPfnDataBase页帧数据库
    保护模式中的PDE与PTE
    保护模式101012分页机制
    Windows系统调用中的系统服务表描述符(SSDT)
    Windows系统调用中的系统服务表
    三环进入零环的细节(KiFastCallEntry函数分析)
    Windows系统调用中API从3环到0环(下)
    SQL反模式学习笔记5 外键约束【不用钥匙的入口】
    SQL反模式学习笔记3 单纯的树
    SQL反模式学习笔记2 乱穿马路
  • 原文地址:https://www.cnblogs.com/ubuntuvim/p/4796553.html
Copyright © 2011-2022 走看看