在IDE上的設置
首先下載的IntelliJ IDEA的x(社區版)因為目前它是最好的Scala的IDE然後選擇File |設置|插件選擇可用的選項卡來安裝Scala插件
創建Maven項目
在IntelliJ IDEA中選擇文件|新建項目然後選擇Maven的模塊創建一個Maven項目然後修改如下所示的pomxml添加所有你需要的設置建立Scala的匯編語言
<project xmlnsxmlns=xmlns:xsi=instance
xsi:schemaLocation=v__xsd>
<modelVersion></modelVersion>
<groupId>comfoo</groupId>
<artifactId>myapp</artifactId>
<packaging>war</packaging>
<version>SNAPSHOT</version>
<name>myapp Java EE Webapp</name>
<url>;/url>
<properties>
<springversion>RELEASE</springversion>
</properties>
<repositories>
<repository>
<id></id>
<name>Repository hosting the jee artifacts</name>
<url>;/url>
</repository>
<repository>
<id></id>
<name>Scalatools Maven Repository</name>
<url>;/url>
</repository>
<repository>
<id>wptrelease</id>
<url>repo/releases</url>
</repository>
<repository>
<id>wptsnapshot</id>
<url>repo/snapshots</url>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id></id>
<name>Scalatools Maven Repository</name>
<url>;/url>
</pluginRepository>
</pluginRepositories>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>springcontext</artifactId>
<version>${springversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>springcore</artifactId>
<version>${springversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>springweb</artifactId>
<version>${springversion}</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>springorm</artifactId>
<version>${springversion}</version>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>comhdatabase</groupId>
<artifactId>h</artifactId>
<version></version>
</dependency>
<dependency>
<groupId>orghibernate</groupId>
<artifactId>hibernateentitymanager</artifactId>
<version>GA</version>
</dependency>
<dependency>
<groupId>orgspringframework</groupId>
<artifactId>springorm</artifactId>
</dependency>
<dependency>
<groupId>dev</groupId>
<artifactId>wptcore</artifactId>
<version>SNAPSHOT</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>dev</groupId>
<artifactId>wptruntimespring</artifactId>
<version>SNAPSHOT</version>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaeewebapi</artifactId>
<version></version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version></version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>orgscalalang</groupId>
<artifactId>scalalibrary</artifactId>
<version>RC</version>
</dependency>
<dependency>
<groupId>orgapachewicket</groupId>
<artifactId>wicket</artifactId>
<version></version>
</dependency>
<dependency>
<groupId>orgslfj</groupId>
<artifactId>slfjlogj</artifactId>
<version></version>
</dependency>
</dependencies>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>orgapachemavenplugins</groupId>
<artifactId>mavencompilerplugin</artifactId>
<version></version>
</plugin>
<plugin>
<groupId>orgscalatools</groupId>
<artifactId>mavenscalaplugin</artifactId>
<version></version>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>orgapachemavenplugins</groupId>
<artifactId>mavencompilerplugin</artifactId>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>compile</goal>
</goals>
</execution>
</executions>
<configuration>
<source></source>
<target></target>
</configuration>
</plugin>
<plugin>
<groupId>orgapachemavenplugins</groupId>
<artifactId>mavenwarplugin</artifactId>
<version>beta</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>orgscalatools</groupId>
<artifactId>mavenscalaplugin</artifactId>
<executions>
<execution>
<id>scalacompilefirst</id>
<phase>processresources</phase>
<goals>
<goal>addsource</goal>
<goal>compile</goal>
</goals>
</execution>
<execution>
<id>scalatestcompile</id>
<phase>processtestresources</phase>
<goals>
<goal>testCompile</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<finalName>myapp</finalName>
</build>
</project>
webxml中設置
接下來修改main/webapp/WEBINF/webxml從而確立了Wicket的過濾器打開Spring的JPA實體管理器和Spring監聽器進行Spring的初始化
<?xml version= encoding=UTF?>
<webapp version=xmlns=xmlns:xsi=instance
xsi:schemaLocation=app__xsd>
<filter>
<filtername>f</filtername>
<filterclass>orgspringframeworkormjpasupportOpenEntityManagerInViewFilter</filterclass>
</filter>
<filter>
<filtername>f</filtername>
<filterclass>orgapachewicketprotocolhttpWicketFilter</filterclass>
<initparam>
<paramname>applicationClassName</paramname>
<paramvalue>comfoomyappMyApp</paramvalue>
</initparam>
</filter>
<filtermapping>
<filtername>f</filtername>
<urlpattern>/app/*</urlpattern>
</filtermapping>
<filtermapping>
<filtername>f</filtername>
<urlpattern>/*</urlpattern>
</filtermapping>
<contextparam>
<paramname>contextConfigLocation</paramname>
<paramvalue>classpath:/beansxml</paramvalue>
</contextparam>
<listener>
<listenerclass>orgsprntextContextLoaderListener</listenerclass>
</listener>
</webapp>
設置Spring bean訪問和調用的數據庫
要定義這些Spring bean創建main/resources的beansxml以及一下的內容
<?xml version= encoding=UTF ?>
<beansxmlnsbeansxmlns=
xmlns:xsi=instance
xmlns:context=
xmlns:tx=
xsi:schemaLocation=
beansxsd
contextxsd
txxsd>
<context:componentscan basepackage=comfoomyapp/>
<bean id=emfclass=orgspringframeworkormjpaLocalContainerEntityManagerFactoryBean>
<property name=persistenceXmlLocationvalue=classpath:/METAINF/mypersistencexml/>
</bean>
<bean id=transactionManagerclass=orgspringframeworkormjpaJpaTransactionManager>
<property name=entityManagerFactory ref=emf/>
</bean>
<tx:annotationdriven />
</beans>
讀取數據庫配置中persistencexml文件在這裡您將獲得一個主目錄中名為MyApp的H數據庫因此創建main/resources/METAINF文件的內容如下
<persistencexmlnspersistencexmlns=
xmlns:xsi=instance
xsi:schemaLocation=__xsd
version=>
<persistenceunit name=myapp transactiontype=RESOURCE_LOCAL>
<provider>orghibernateejbHibernatePersistence</provider>
<properties>
<propertynamepropertyname=nnectiondriver_classvalue=orghDriver/>
<property name=nnectionurlvalue=jdbc:h:tcp://localhost/~/myapp/>
<propertynamepropertyname=nnectionusername value=sa/>
<propertynamepropertyname=nnectionpassword value=/>
<property name=hibernatedialectvalue=orghibernatedialectHDialect/>
<property name=hibernatehbmddlautovalue=create/>
</properties>
</persistenceunit>
/persistence>
創建Wicket的頁面
在這裡您將創建一個Wicket頁面顯示從數據庫加載一些內容因此創建主/Scala文件夾右鍵單擊它並選擇新建|Scala類命名為我的頁面類和投入comfoomyapp包中如下所示
package comfoomyapp
import orgapacl_
import basicLabel
import list{ListItem ListView}
importorgapachewicketspringinjectionannotSpringBean
import orgapachewicketmodelCompoundPropertyModel
class MyPage extends WebPage {
@SpringBean
var ps: ProductService = _
val productListView = newListView[Product](productListView psgetAll) {
def populateItem(item: ListItem[Product]) = {
itemsetModel(newCompoundPropertyModel[Product](itemgetDefaultModelObject))
itemadd(new Label(name))
itemadd(new Label(price))
}
}
add(productListView)
}
請注意在創建ProductService對象加載的文件之後使用此外請注意該字段被分配到一個下劃線(_)Scala編譯為不可初始化的而是留在默認狀態在你將覆蓋作為編譯之前執行Spring bean的頁面構造現在創建在src/main/resources/com/foo/myapp的的l文件
<!DOCTYPE HTML PUBLIC //WC//DTD HTML Transitional//EN>
<html>
<table border=>
<tr wicket:id=productListView>
<td wicket:id=name></td>
<td wicket:id=price></td>
</tr>
</table>
</html>
創建ProductService
右鍵單擊在in the src/main/scala文件夾中的comfoomyapp包並選擇New | Scala的類然後選擇創建ProductService
package comfoomyapp
import javautilList
trait ProductService {
def getAll: List[Product]
}
這是接口對創造一個DefaultProductService的Scala在同一個包內進行執行操作
package comfoomyapp
import javaxpersistence{PersistenceContext EntityManager}
import orgspringframeworkstereotype{Service}
importorgspringframeworktransactionannotationTransactional
importorgspringframeworkbeansfactoryannotationAutowired
@Service
@Transactional
class DefaultProductService extends ProductService {
@Autowired
var products: Products = _
def getAll = productsgetAll
}
請注意把它使用類型文件命名為DAO
創建文件的DAO
要創建的DAO創建一個名為在同一文件夾
package comfoomyapp
import javautilList
trait Products {
def getAll: List[Product]
}
然後創建在同一文件夾中的執行Scala的DefaultProducts文件
package comfoomyapp
import javaxpersistence{PersistenceContext EntityManager}
import orgspringframeworkstereotypeRepository
import javautilList
@Repository
class DefaultProducts extends Products {
@PersistenceContext
var em: EntityManager = _
def getAll = {
emcreateQuery(select p from Product p)getResultListasInstanceOf[List[Product]]
}
}
創建entity程序
創建entity程序並將其映射到數據庫
package comfoomyapp
import javaxpersistence{GeneratedValue Id Entity}
@Entity
class Product {
@Id
@GeneratedValue
var id: Long = _
var name: String = _
var price: Double = _
}
創建Wicket的應用程序
最後創建在同一個MyApp的Scala程序
package comfoomyapp
import orgapachewicketprotocolhttpWebApplication
devwicketpagetestMockableSpringBeanInjector
class MyApp extends WebApplication {
def getHomePage = classOf[MyPage]
override def init = {
MockableSpringBeanInjectorinstallInjector(this)
}
}
在這裡您指定為首頁我的目錄植入Spring Bean中
運行應用程序
隨著IntelliJ IDEA的版本不斷改進您將嵌入到應用服務器運行您的應用程序這非常容易在剛剛創建的測試comfoomyapp的ManualTest在剛剛創建的測試comfoomyapp包一類ManualTest/scala文件(不是main/scala文件)以下內容是
package comfoomyapp
devwicketpagetest{WebAppJettyConfiguration WicketAppJettyLauncher}
object ManualTest {
def main(args: Array[String]) {
val l = new WicketAppJettyLauncher
lstartAppInJetty(new WebAppJettyConfiguration)
}
}
要運行的應用程序首先需要運行數據庫服務器上的H程序所以在下載和解壓然後變成h/bin和運行hbat(或在Linux上hsh)ManualTest是在IDEA的編輯器上在IDEA選擇運行DEA它將運行ManualTestJetty的運行您的應用程序
為了測試它請嘗試通過在浏覽//localhost:/app
不知道各位感覺如何感謝各位支持!謝謝!
原文名Getting started with Scala Spring Hibernate & Wicket
From:http://tw.wingwit.com/Article/program/Java/hx/201311/26175.html