JFinal配置c3p0連線池
阿新 • • 發佈:2018-11-27
1.c3p0.properties:連線資料庫
driver=com.mysql.jdbc.Driver
jdbcUrl = jdbc:mysql://localhost:3306/test
username = root
password = root
2.必須定義一個類整合JFinalConfig類
import com.jfinal.config.*; import com.jfinal.kit.PropKit; import com.jfinal.plugin.activerecord.ActiveRecordPlugin; import com.jfinal.plugin.c3p0.C3p0Plugin;import com.jfinal.render.ViewType; public class MyJfinalConfig extends JFinalConfig { /** * 配置常量 * @param constants */ @Override public void configConstant(Constants constants) { constants.setViewType(ViewType.JSP); //讀取外部檔案 PropKit.use("c3p0.properties"); }/** * 配置訪問路由 * @param routes */ @Override public void configRoute(Routes routes) { routes.add("/users",UsersController.class); } /** * 配置外掛 * @param plugins */ @Override public void configPlugin(Plugins plugins) { //配置連線池 //輸入庫連線池C3p0Plugin c3p0Plugin = new C3p0Plugin(PropKit.get("jdbcUrl"), PropKit.get("username"), PropKit.get("password")); //ORM Activerecord ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin); arp.setShowSql(true); arp.addMapping("users", Users.class); plugins.add(c3p0Plugin); plugins.add(arp); } /** * 全域性攔截器 * @param interceptors */ @Override public void configInterceptor(Interceptors interceptors) { } /** * 控制器 * @param handlers */ @Override public void configHandler(Handlers handlers) { } }
3.測試
import com.jfinal.core.Controller;public class UsersController extends Controller { /** * 訪問該類時,預設訪問index方法 */ public void index(){
render("index.jsp"); } }