討厭鬼最大的動力來源

若還沒看過這三篇的話~請先去看Struts2 架構配置教學Struts2+Spring架構配置教學(Spring)Struts2+Spring+Hibernate架構配置教學(Hibernate)上

雖然討厭鬼本身沒有很厲害

但還是想在開始前跟大家小講一下小觀念

我在這邊用的是 Action <> Service <> DAO

所以action layer是不會直接用到DAO layer

DAO是用來對資料庫做事的

所以Service layer 是不會直接存取資料庫

而在Spring 的設定檔中 我會用三個設定檔

分別用來管理 Action 、Service、DAO

這樣看起來是比較清楚~也比較好debug

也因為今天要講的Hibernate會用到資料庫的東西

所以我會將設定放在dao-context.xml

可是今天會只做到跟資料庫連線~~~~

那討厭鬼就開始了!!!!!

 

首先先建立一個SessionFactory

在這邊呢~我們會參照到一個dataSource所以等等會設定一個dataSource

hibernateProperties是指Hibernate裡的一些設定,還可以指定是否要show出sql之類的等等

而annotatedClasses這個是指你要跟資料庫連線的entity

如果在這邊沒有加的話~之後用Hibernate存取資料庫時會失敗

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>nerdy.entity.LeatherType</value>
            </list>
        </property>
</bean>

 

當然連線方式是看各位客倌的習慣是要使用JDBC還是JNDI

討厭鬼這邊是使用JNDI

 

在這邊使用value中的"java:comp/env/"是前導字必填~後面是name

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" >
        <property name="jndiName" value="java:comp/env/jdbc/leather"></property>
</bean>

 

 

下面是我的配置

SpringDAO1

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop-3.1.xsd ">


    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="hibernateProperties">
            <value>
                hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
            </value>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>nerdy.entity.LeatherType</value>
            </list>
        </property>
    </bean>
    
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean" >
        <property name="jndiName" value="java:comp/env/jdbc/leather"></property>
    </bean>
</beans>

 

 

 

而在這邊要宣告連線方式有兩種

一個是在tomcat的Server.xml的<context>裡宣告

一個是在META-INF裡加入一個context.xml中宣告

我用的是後者~比較方便

所以就在META-INF裡加入一個context.xml並加上

<context>
    <Resource name="jdbc/leather" auth="Container" type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.113:3306/leather"
        username="leather" password="123456" maxWait="10000" maxIdle="30" maxActive="100" />
</context>

 

下面是我的配置

SpringDAO2

 

<?xml version="1.0" encoding="UTF-8"?>
<context>
    <Resource name="jdbc/leather" auth="Container" type="javax.sql.DataSource"
        driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://192.168.1.113:3306/leather"
        username="leather" password="123456" maxWait="10000" maxIdle="30" maxActive="100" />
</context>

 

 

 

基本上Struts2+Spring+Hibernate的架構設定已經完成

下次會發一篇Spring + Hibernate的用法~不是架構了喔   

文章標籤
創作者介紹

討厭鬼教學

討厭鬼 發表在 痞客邦 PIXNET 留言(7) 人氣()


留言列表 (7)

發表留言
  • 訪客
  • 在這邊使用value中的"java:comp/env/"是前導字必填~後面是name

    請問這是什麼的name?
  • 討厭鬼
  • 就是自定的~象是這邊的name為jdbc/leather
    對應到的就是在context.xml裡面的Resource name
  • 派翠鳥
  • 版主大大不好意思
    我依照前面配置的結果,啟動TOMCAT時
    會出現如下的錯誤訊息
    google了很久還是無法解決
    才上來求救 希望大大幫忙看看囉
    感謝您

    八月 06, 2013 12:04:06 上午 org.apache.catalina.core.StandardContext listenerStart
    SEVERE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean] for bean with name 'sessionFactory' defined in class path resource [patrick/config/dao-context.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1262)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.predictBeanType(AbstractAutowireCapableBeanFactory.java:576)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:1331)
    at org.springframework.beans.factory.support.AbstractBeanFactory.isFactoryBean(AbstractBeanFactory.java:897)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:566)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:383)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4206)
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:4705)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardHost.start(StandardHost.java:840)
    at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1057)
    at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:463)
    at org.apache.catalina.core.StandardService.start(StandardService.java:525)
    at org.apache.catalina.core.StandardServer.start(StandardServer.java:754)
    at org.apache.catalina.startup.Catalina.start(Catalina.java:595)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
    at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
    Caused by: java.lang.ClassNotFoundException: org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:258)
    at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:417)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doResolveBeanClass(AbstractBeanFactory.java:1283)
    at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1254)
    ... 24 more
  • 派翠鳥
  • 大大抱歉
    我少抓了jar檔
    已經沒問題了
    耍笨了Orz
  • 小Z
  • 請問 我照著做了
    為什麼沒辦法run呢?
  • 小Z
  • 請問 我照著做了
    為什麼沒辦法run呢?
  • 有錯誤訊息嗎?沒有的話討厭鬼也沒有辦法幫忙

    討厭鬼 於 2015/12/16 17:37 回覆

  • 阿貴
  • 無法Run +1

    剛Google ,找不太到是錯在哪,是少了哪個動作嗎..T__T

    嚴重: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in class path resource [nerdy/config/dao-context.xml]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:997)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:943)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:589)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:388)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:293)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
    Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean]: Constructor threw exception; nested exception is java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:162)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:87)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
    ... 22 more
    Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/AnnotationConfiguration
    at org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean.<init>(AnnotationSessionFactoryBean.java:99)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:147)
    ... 24 more
    Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.AnnotationConfiguration
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1718)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1569)
    ... 30 more
  • 需要提供設定檔討厭鬼才能找問題喔

    討厭鬼 於 2016/06/21 14:52 回覆