jetty,tomcat maven 插件配置数据源

jetty插件配置数据源,首先在src/main/resources目录下创建一个名为jetty-env.xml的文件,文件内容如下:
<?xml version="1.0"?>
<!DOCTYPE Configure PUBLIC "-//Mort Bay Consulting//DTD Configure//EN" "http://jetty.mortbay.org/configure.dtd">

<Configure class="org.mortbay.jetty.webapp.WebAppContext">
    <New id="DataSource" class="org.mortbay.jetty.plus.naming.Resource">
        <Arg>jdbc/test</Arg>

        <Arg>
            <New class="org.apache.commons.dbcp.BasicDataSource">
                <Set name="driverClassName">com.mysql.jdbc.Driver</Set>
                <Set name="url">jdbc:mysql://localhost:3306/appfuse?createDatabaseIfNotExist=true&amp;useUnicode=true&amp;characterEncoding=utf-8</Set>
                <Set name="username">root</Set>
                <Set name="password">root</Set>
                <Set name="maxActive">50</Set>
            </New>
        </Arg>
    </New>
</Configure>


tomcat插件配置数据,首先在src/main/resources目录下创建一个名为contextFile.xml的文件,内容如下:
<Context>
    <Resource name="jdbc/test" auth="Container"
              type="javax.sql.DataSource"
              username="root" password="root"
              driverClassName="com.mysql.jdbc.Driver"
              url="jdbc:mysql://localhost:3306/appfuse"/>
</Context>


web.xml中的内容如下:
<resource-ref>
        <description>ConnectionPool DataSource Reference</description>
        <res-ref-name>jdbc/test</res-ref-name>
        <res-type>javax.sql.DataSource</res-type>
        <res-auth>Container</res-auth>
</resource-ref>


JSP测试代码如下:
<%@ page contentType="text/html; charset=UTF-8" %>
<%@ page import="javax.naming.InitialContext,javax.sql.DataSource" %>
<html>
    <head>
        <title>JNDI datasource test</title>
    </head>
    <body>
        <h2>数据源初始化成功!</h2>
        <%
        
            InitialContext context = new InitialContext();
            DataSource dataSource = (DataSource) context.lookup("java:comp/env/jdbc/test");
        %>
    </body>
</html>

你可能感兴趣的:(java,maven,tomcat,mysql,jdbc)