因为是刚开始使用框架,连接数据库报了一堆错,所以上网搜了很多的相关教程,还是没解决问题,后来才发现是两个很简单的问题:一个是连接数据库的url的端口是3306,和tomcat的8080弄混了,一直写的8080;一个是MySQL的驱动包版本和本地安装的MySQL版本不一致。出现报错的道友可以考虑下这两个方面,下面贴完整代码。
1.pom文件的jar包(开发使用的maven项目):
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- jfinal框架 -->
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jfinal</artifactId>
<version>2.1</version>
</dependency>
<dependency>
<groupId>com.jfinal</groupId>
<artifactId>jetty-server</artifactId>
<version>8.1.8</version>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity</artifactId>
<version>1.7</version>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
<!-- mysql驱动包,注意这里的version要和本地数据库的版本保持一致 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
<scope>runtime</scope>
</dependency>
<!-- c3p0连接池的两个jar包 -->
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.1</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>mchange-commons-java</artifactId>
<version>0.2.11</version>
</dependency>
<!-- 下面的依赖仅在使用 JSP 时才需要 -->
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-jsp</artifactId>
<version>9.2.26.v20180806</version>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<finalName>jfinal_maven</finalName>
</build>
2.DemoConfig类,这个类用来启动项目:
package demo;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.c3p0.C3p0Plugin;
public class DemoConfig extends JFinalConfig{
public static void main(String[] args) {
//启动项目,配置路径,启动时不用加上项目名,直接localhost/blog/save,访问BlogController的save方法
JFinal.start("src/main/webapp", 80, "/", 5);
}
public void configConstant(Constants me) {
//加载properties文件
loadPropertyFile("c3p0db.properties");
me.setDevMode(true);
}
public void configRoute(Routes me) {
//配置类的访问路径
me.add("/blog", BlogController.class);
}
public void configPlugin(Plugins me) {
//连接数据库
C3p0Plugin c3p0Plugin = new C3p0Plugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"),getProperty("driver"));
me.add(c3p0Plugin);
ActiveRecordPlugin arp = new ActiveRecordPlugin(c3p0Plugin);
//配置数据库表与类的映射
arp.addMapping("blog",Blog.class);
me.add(arp);
}
public void configInterceptor(Interceptors me) {
// TODO Auto-generated method stub
}
public void configHandler(Handlers me) {
// TODO Auto-generated method stub
}
}
3. Blog类:
package demo;
import com.jfinal.plugin.activerecord.Model;
public class Blog extends Model<Blog>{
public static final Blog dao=new Blog();
}
4.BlogController类:
package demo;
import com.jfinal.core.Controller;
public class BlogController extends Controller{
public void save() {
//我这里没有写操作数据库的,主要解释连接数据库的问题
Blog blog = getModel(Blog.class);
blog = getModel(Blog.class,"otherName");
renderText(blog.toString());
}
}
5.index.jsp(该文件放在src/main/webapp下面,通过form表单提交数据):
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<html>
<head>
<title>jfinal</title>
<meta charset="utf-8">
</head>
<body>
<form action="/blog/save" method="post">
<input type="text" name="blog.title">
<input type="text" name="blog.content">
<input type="submit" value="提交">
</form>
</body>
</html>
最后启动DemoConfig类,访问http://localhost/index.jsp,提交数据在控制台可以看到打印信息。