第一篇《JFinal搭建》:http://blog.csdn.net/qq_35823302/article/details/76596136
这里是第二篇,主要实现对数据库的连接。
- 创建实体
说明: 这里要继承JFinal 中的 Model类,里面封装的基础的增删改方法
package com.gx.test.dao;
import com.jfinal.plugin.activerecord.Model;
/**
* 用户实体
* @title
* @filename User.java
* @author L-z
* @date 2017年8月2日 上午9:49:27
* @version 1.0
*/
public class User extends Model<User> {
/**
* @title 序列化
*/
private static final long serialVersionUID = -4507206116124248557L;
//在 User中声明的dao静态对象是为了方便查询操作而定义的,该对象并不是必须的
/**
* 用户对象
*/
public static final User dao=new User().dao();
}
- 添加数据库连接文件
说明:jdbc.properties文件 我放在了 resources 文件夹下
jdbcUrl = jdbc:mysql://127.0.0.1:3306/userpower?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
user = root
password = LiZhi
devMode = true
- 导入连接驱动Jar包
说明:这里要在pom.xml下 导入2个jar包
<!-- 导入Mysql数据库链接jar包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.30</version>
</dependency>
<!-- 阿里巴巴数据连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.29</version>
</dependency>
- 连接数据库
说明:在核心配置文件 BaseConfig类中的 configRoute 方法 下配置数据库连接
/**
* 数据库连接
*/
@Override
public void configPlugin(Plugins plugins) {
Prop p=PropKit.use("jdbc.properties");
//数据库连接
DruidPlugin dp = new DruidPlugin(p.get("jdbcUrl").trim(),
p.get("user").trim(), p.get("password").trim());
plugins.add(dp);
ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
plugins.add(arp);
//映射模型
arp.addMapping("pw_user","userId",User.class);
System.out.println("------------------------数据库加载成功---------------------------------------");
}
- 启动不报错就成功 - -
类的命名都是重第一篇的博文中介绍过,若有所不知,移步查看第一篇博文