jfinal 使用Tomcat JNDI

运行环境:Tomcat 7.0.x

(1) Tomcat 配置
 打开tomcat/conf/context.xml 在文件中增加
<Resource 
       name="jdbc/mysql" 
       auth="Container" 
       type="javax.sql.DataSource"
       maxActive="100" 
       maxIdle="30" 
       maxWait="10000"
       username="root" 
       password="" 
       driverClassName="com.mysql.jdbc.Driver"
       url="jdbc:mysql://127.0.0.1/test"
/>
解决mysql中中文乱码在url中增加:?useUnicode=true&amp;characterEncoding=utf-8
(2) web.xml 配置
  在项目中web.xml中增加以下内容:
   
  <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/mysql</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
  </resource-ref>


配置类

public class MyConfig extends JFinalConfig {


public void configConstant(Constants me) {
loadPropertyFile("survey.config.txt"); // 加载配置文件
me.setDevMode(getPropertyToBoolean("devMode", true)); // 设置开发者模式
me.setBaseViewPath("/WEB-INF/html"); // 设置视图基本路径
}


public void configRoute(Routes me) {
me.add("/", IndexController.class);// 设置路由
me.add("/sys", SysController.class);// 设置路由
}


public void configPlugin(Plugins me) {
/*
String jdbcUrl = getProperty("jdbcUrl");
String user = getProperty("user");
String password = getProperty("password").trim();
DruidPlugin druidPlugin = new DruidPlugin(jdbcUrl, user, password);
me.add(druidPlugin);
ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
arp.setShowSql(getPropertyToBoolean("showSql", false));
me.add(arp);// 接入ActiveRecord插件
// 管理系统 Model 
arp.addMapping("subject", Subject.class);
arp.addMapping("options", Options.class);
arp.addMapping("statistical", Statistical.class);
*/
final String JNDINAME = "java:comp/env/jdbc/mysql" ;
DataSource datasource;
try {
// datasource = (DataSource)new InitialContext().lookup("java:MySqlDs");
datasource = (DataSource)new InitialContext().lookup(JNDINAME);
Connection conn = datasource.getConnection() ;
System.out.println("connection auto commit info==="+conn.getAutoCommit());
ActiveRecordPlugin arp = new ActiveRecordPlugin(datasource);
// 管理系统 Model 
arp.addMapping("subject", Subject.class);
arp.addMapping("options", Options.class);
arp.addMapping("statistical", Statistical.class);

me.add(arp);
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}

}


public void configInterceptor(Interceptors me) {
me.add(new SessionInViewInterceptor(true));
// me.add(new SysInterceptor());// 后台全局拦截器
}


public void configHandler(Handlers me) {
me.add(new ContextPathHandler("BasePath"));
}


public static void main(String[] args) {
// JFinal.start("WebRoot", 80, "/", 5);
System.out.println(String.format("deregistering jdbc driver: %s",
new ContextPathHandler("BasePath")));
}
}


按照上面的配置就可以正常的使用Tomcat基于JNDI的数据源了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值