maven创建webhibernate项目(sts)

  1. 新建maven project(打包为war)
  2. path 修改为指定jdk版本
  3. 修改为web工程,添加webContent *右键项目,选择properties*  

    不勾选点击apply然后勾选最后点击标签修改后点击ok

  4. 配置pom.xml

      1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
      2   <modelVersion>4.0.0</modelVersion>
      3   <groupId>zb</groupId>
      4   <artifactId>text0627</artifactId>
      5   <version>0.0.1-SNAPSHOT</version>
      6   <packaging>war</packaging>
      7   
      8   
      9   <dependencies>
     10           <!-- 添加JUnit -->
     11         <dependency>
     12             <groupId>junit</groupId>
     13             <artifactId>junit</artifactId>
     14             <version>4.8</version>
     15             <scope>test</scope>
     16         </dependency>
     17 
     18         <!-- 添加Servlet -->
     19         <dependency>
     20             <groupId>javax.servlet</groupId>
     21             <artifactId>javax.servlet-api</artifactId>
     22             <version>3.1.0</version>
     23             <scope>provided</scope>
     24         </dependency>
     25 
     26 
     27         <dependency>
     28             <groupId>javax.servlet.jsp</groupId>
     29             <artifactId>jsp-api</artifactId>
     30             <version>2.2</version>
     31         </dependency>
     32 
     33         
     34         
     35         <dependency>
     36             <groupId>mysql</groupId>
     37             <artifactId>mysql-connector-java</artifactId>
     38             <version>5.1.36</version>
     39         </dependency>
     40 
     41         <dependency>
     42             <groupId>jstl</groupId>
     43             <artifactId>jstl</artifactId>
     44             <version>1.2</version>
     45         </dependency>
     46         
     47         <!-- Hibernate -->
     48         
     49 
     50       <dependency>
     51               <groupId>org.hibernate</groupId>
     52              <artifactId>hibernate-core</artifactId>
     53               <version>4.3.10.Final</version>
     54       </dependency>
     55 
     56       <!-- c3p0    dbcp-->
     57        <dependency>
     58             <groupId>com.mchange</groupId>
     59             <artifactId>c3p0</artifactId>
     60             <version>0.9.5-pre8</version>
     61      </dependency>
     62      <dependency>
     63             <groupId>com.mchange</groupId>
     64             <artifactId>mchange-commons-java</artifactId>
     65             <version>0.2.12</version>
     66      </dependency>
     67      
     68     <dependency>
     69         <groupId>net.sf.json-lib</groupId>
     70         <artifactId>json-lib</artifactId>
     71         <version>2.4</version>
     72          <classifier>jdk15</classifier>  
     73     </dependency>
     74     
     75 <dependency>
     76     <groupId>org.slf4j</groupId>
     77     <artifactId>slf4j-log4j12</artifactId>
     78     <version>1.7.25</version>
     79     <scope>test</scope>
     80 </dependency>
     81   </dependencies>
     82   
     83   
     84         <build>  
     85         <plugins>  
     86           
     87             <!-- Hibernate生成实体配置 -->  
     88             <plugin>  
     89                 <groupId>org.codehaus.mojo</groupId>  
     90                 <artifactId>hibernate3-maven-plugin</artifactId>  
     91                 <version>2.2</version>  
     92   
     93                 <dependencies>  
     94                     <dependency>  
     95                         <groupId>mysql</groupId>  
     96                         <artifactId>mysql-connector-java</artifactId>  
     97                         <version>5.1.21</version>  
     98                     </dependency>  
     99                     <dependency>  
    100                         <groupId>cglib</groupId>  
    101                         <artifactId>cglib</artifactId>  
    102                         <version>2.2</version>  
    103                     </dependency>  
    104                 </dependencies>  
    105   
    106                 <configuration>  
    107                     <components>  
    108                         <component>  
    109                             <name>hbm2java</name>  
    110                             <outputDirectory>src/main/java</outputDirectory>  
    111                             <!-- 主要用于反向控制数据库引擎通过JDBC连接数据 -->  
    112                             <implementation>jdbcconfiguration</implementation>  
    113                         </component>  
    114                     </components>  
    115                     <componentProperties>  
    116                         <!-- Hibernate配置文件 -->  
    117                         <configurationfile>src/main/resources/hibernate.cfg.xml </configurationfile>  
    118                         <ejb3>true</ejb3>  
    119                         <!-- 指明生成java类的包名 -->  
    120                         <packagename>com.oracle.entity</packagename>  
    121                     </componentProperties>  
    122                 </configuration>  
    123             </plugin>  
    124             <!-- Hibernate生成实体配置 -->  
    125               
    126         </plugins>  
    127     </build> 
    128 </project>
  5. 导入hibernate.cfg.xml

    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC
              "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
              "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <!-- Generated by MyEclipse Hibernate Tools. -->
    <hibernate-configuration>
    
        <session-factory>
            <property name="dialect">
                org.hibernate.dialect.MySQLDialect
            </property>
            <property name="connection.url">
                jdbc:mysql://localhost:3306/test2
            </property>
            <property name="connection.username">root</property>
            <property name="connection.password">root</property>
            <property name="connection.driver_class">
                com.mysql.jdbc.Driver
            </property>
            <property name="myeclipse.connection.profile">zzsql</property>
            <property name="show_sql">true</property>
            <property name="format_sql">true</property>
    
    
    
            <property name="hibernate.c3p0.max_size">20</property>
    
            <!-- 最小连接数 -->
            <property name="hibernate.c3p0.min_size">5</property>
    
            <!-- 获得连接的超时时间,如果超过这个时间,会抛出异常,单位毫秒 -->
            <property name="hibernate.c3p0.timeout">120</property>
    
            <!-- 最大的PreparedStatement的数量 -->
            <property name="hibernate.c3p0.max_statements">100</property>
    
            <!-- 每隔120秒检查连接池里的空闲连接 ,单位是秒 -->
            <property name="hibernate.c3p0.idle_test_period">120</property>
    
            <!-- 当连接池里面的连接用完的时候,C3P0一下获取的新的连接数 -->
            <property name="hibernate.c3p0.acquire_increment">2</property>
    
            <!-- 每次都验证连接是否可用 -->
            <property name="hibernate.c3p0.validate">true</property>
            <!-- Hibernate使用的 -->
            <property name="hibernate.c3p0.provider_class">org.hibernate.connection.C3P0ConnectionProvider</property>
            <mapping class="com.oracle.entity.Admins"/>
        </session-factory>
    
    </hibernate-configuration>
  6. 执行hibernate3:hbm2java映射实体类  右键pom.xml   run as     maven build       goals为hibernate3:hbm2java            。。。。。。。。。。

  7.  设置实体类中的set泛型 hibernate.cfg.xml中添加实体类的引用

  8. 导入HibernateSessionFactory.java到dao
    package com.oracle.dao;
    
    import org.hibernate.HibernateException;
    import org.hibernate.Session;
    import org.hibernate.cfg.Configuration;
    import org.hibernate.service.ServiceRegistry;
    import org.hibernate.service.ServiceRegistryBuilder;
    
    /**
     * Configures and provides access to Hibernate sessions, tied to the
     * current thread of execution.  Follows the Thread Local Session
     * pattern, see {@link http://hibernate.org/42.html }.
     */
    public class HibernateSessionFactory {
    
        /** 
         * Location of hibernate.cfg.xml file.
         * Location should be on the classpath as Hibernate uses  
         * #resourceAsStream style lookup for its configuration file. 
         * The default classpath location of the hibernate config file is 
         * in the default package. Use #setConfigFile() to update 
         * the location of the configuration file for the current session.   
         */
        private static final ThreadLocal<Session> threadLocal = new ThreadLocal<Session>();
        private static org.hibernate.SessionFactory sessionFactory;
        
        private static Configuration configuration = new Configuration();
        private static ServiceRegistry serviceRegistry; 
    
        static {
            try {
                configuration.configure();
                serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
                System.err.println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
        private HibernateSessionFactory() {
        }
        
        /**
         * Returns the ThreadLocal Session instance.  Lazy initialize
         * the <code>SessionFactory</code> if needed.
         *
         *  @return Session
         *  @throws HibernateException
         */
        public static Session getSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
    
            if (session == null || !session.isOpen()) {
                if (sessionFactory == null) {
                    rebuildSessionFactory();
                }
                session = (sessionFactory != null) ? sessionFactory.openSession()
                        : null;
                threadLocal.set(session);
            }
    
            return session;
        }
    
        /**
         *  Rebuild hibernate session factory
         *
         */
        public static void rebuildSessionFactory() {
            try {
                configuration.configure();
                serviceRegistry = new ServiceRegistryBuilder().applySettings(configuration.getProperties()).buildServiceRegistry();
                sessionFactory = configuration.buildSessionFactory(serviceRegistry);
            } catch (Exception e) {
                System.err.println("%%%% Error Creating SessionFactory %%%%");
                e.printStackTrace();
            }
        }
    
        /**
         *  Close the single hibernate session instance.
         *
         *  @throws HibernateException
         */
        public static void closeSession() throws HibernateException {
            Session session = (Session) threadLocal.get();
            threadLocal.set(null);
    
            if (session != null) {
                session.close();
            }
        }
    
        /**
         *  return session factory
         *
         */
        public static org.hibernate.SessionFactory getSessionFactory() {
            return sessionFactory;
        }
        /**
         *  return hibernate configuration
         *
         */
        public static Configuration getConfiguration() {
            return configuration;
        }
    
    }
    扫描二维码关注公众号,回复: 1771820 查看本文章

猜你喜欢

转载自www.cnblogs.com/zhangdabiao/p/9233118.html