JFinal框架学习(二)----------MySQL数据库配置

1、创建数据表:

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for user
-- ----------------------------
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(20) NOT NULL,
  `pwd` varchar(20) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;

2、数据库配置:
①、在resources目录下创建“config.properties”文件:
在这里插入图片描述

//jdbc路径
jdbcUrl=jdbc:mysql://localhost:3306/vrapp?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
user=root
password=123456
//开发模式常量配置
devMode=true
showSql=true
maxActive=100
minIdle=5
initialSize=5

②、config目录下配置:
A、在configConstant中配置常量:
在这里插入图片描述

 public void configConstant(Constants constants) {
        //在调用getPropertyToBoolean之前需要先调用loadPropertyFile
        loadPropertyFile("config.properties");
        //设置jfinal的开发模式
        constants.setDevMode(getPropertyToBoolean("devMode",true));
    }

B、在configPlugin中配置获取连接池:
在这里插入图片描述

    public void configPlugin(Plugins plugins) {
    int initialSize = getPropertyToInt("initialSize");
    int minIdle = getPropertyToInt("minIdle");
    int maxActive = getPropertyToInt("maxActive");
    //获取jdbc连接池
    DruidPlugin druidPlugin = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password").trim());
    druidPlugin.set(initialSize, minIdle, maxActive);
    plugins.add(druidPlugin);
    //配置ActiveRecordPlugin插件
    ActiveRecordPlugin arp = new ActiveRecordPlugin(druidPlugin);
    //控制台显示sql语句
    arp.setShowSql(getPropertyToBoolean("showSql",true));
    plugins.add(arp);
    //controller对应的数据表 "user"对应的是数据库中表名,
    // User.class对应的是model中的User模型
    arp.addMapping("user", User.class);

}

C、model中配置User
在这里插入图片描述
3、在pom.xml中配置MySQL:
在这里插入图片描述

    <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>
    <dependency>
      <groupId>org.apache.commons</groupId>
      <artifactId>commons-lang3</artifactId>
      <version>3.3.2</version>
    </dependency>

4、配置完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值