全局的datasource,要求把数据源配置在jetty.xml文件中,但是使用maven的插件,需要在pom.xml文件中指定jetty.xml文件在哪。同时,由于是全局的数据源,jetty会找不到数据库驱动,也需要在pom.xml文件中写上对数据库驱动的依赖。
此外,还需要配置一个webdefault.xml文件,这个文件是在web.xml文件加载之前加载的,可以把数据源的引用放在webdefault.xml文件中。同上,也需要在pom.xml文件中指定这个文件在哪。
1. 配置pom.xml文件
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.0.v20130308</version>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
</dependencies>
<configuration>
<scanIntervalSeconds>5</scanIntervalSeconds>
<jettyXml>src/test/resources/jetty.xml</jettyXml>
<webApp>
<defaultsDescriptor>src/test/resources/webdefault.xml</defaultsDescriptor>
</webApp>
</configuration>
</plugin>
2. 配置jetty.xml
<New id="mysql" class="org.eclipse.jetty.plus.jndi.Resource">
<Arg><Ref refid="Server"/></Arg>
<Arg>jdbc/mysql</Arg>
<Arg>
<New class="com.mysql.jdbc.jdbc2.optional.MysqlConnectionPoolDataSource">
<Set name="Url">jdbc:mysql://localhost:3306/uns</Set>
<Set name="User">root</Set>
<Set name="Password">rachel</Set>
</New>
</Arg>
</New>
3. 配置webdefault.xml
如果把数据源的引用配置在webdefault.xml文件中,那么就不需要在web.xml文件中配置了。如果不在webdefault.xml文件中引用数据源,那么就需要在每一个webapp中的web.xml文件中对其进行引用!
<resource-ref>
<description>MySQL DataSource Reference</description>
<res-ref-name>jdbc/mysql</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
配好了!