使用JFinal框架完成社区demo,简单的连接数据库实现增删改查。

根据社区demo一步步做的,先贴效果图吧。

使用JFinal框架完成社区demo,简单的连接数据库实现增删改查。

点击登录后来到主页:

使用JFinal框架完成社区demo,简单的连接数据库实现增删改查。

前端使用了bootstrap,新增,修改,删除都是可以实现的。下面上代码:

1.项目结构:

使用JFinal框架完成社区demo,简单的连接数据库实现增删改查。

2.pom.xml文件跟着社区demo配就行,主要是jfinal和jetty-server。有个坑,在jetty-server那要配一个scope:

<scope>compile</scope>,不然启动会有问题。WEB-INF里web.xml根据demo写就行。

3.WebConfig类:

public class WebConfig extends JFinalConfig {

    //mian方法是用来启动jetty服务器的,类似于Run as JavaApplication,写在哪里都可以
    public static void main(String[] args) {
        JFinal.start("src/main/webapp", 80, "/");
    }

    public void configConstant(Constants constants) {
        constants.setEncoding("UTF-8");
        //设置为开发模式(如果是false,jfinal页面会缓存,修改页面后不能马上呈现)
        constants.setDevMode(true);
    }

    public void configRoute(Routes routes) {
        //统一设置映射访问路径,类似于SpringMVC的@RequestMapping
        routes.add("/user", UserController.class);
        routes.add("/news", NewsController.class);
    }

    public void configEngine(Engine engine) {
    }

    public void configPlugin(Plugins plugins) {

        PropKit.use("jdbc.properties");
        //数据库连接池插件
        DruidPlugin druidPlugin = new DruidPlugin(PropKit.get("jdbc.url"),
                PropKit.get("jdbc.username"), PropKit.get("jdbc.password"));
        druidPlugin.set(PropKit.getInt("initialSize"),
                PropKit.getInt("minIdle"), PropKit.getInt("maxActive"));
        plugins.add(druidPlugin);

        //实体类映射
        ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(druidPlugin);
        activeRecordPlugin.addMapping("news", "id", News.class);
        activeRecordPlugin.addMapping("user", "id", User.class);
        plugins.add(activeRecordPlugin);

    }

    public void configInterceptor(Interceptors interceptors) {
    }

    public void configHandler(Handlers handlers) {
    }
}

4.NewsController类:

public class NewsController extends Controller {

    private NewsService newsService = new NewsService();

    public void index() {
        setAttr("news", newsService.findAll());
        render("index.html");
    }

    public void edit() {
        setAttr("news", newsService.findById(getParaToInt()));
        render("edit.html");
    }

    public void save() {
        newsService.update(getParaToInt("id"), getPara("title"), getPara("content"));
        redirect("/news");
    }

    public void delete() {
        newsService.delete(getParaToInt());
        redirect("/news");
    }
    //什么也不写,会自动去访问webapp/news/add.html页面
    public void add() {

    }

    public void doAdd() {
        newsService.add(getPara("title"), getPara("content"));
        redirect("/news");
    }

}

5.NewsService类:

public class NewsService {

    public List<Record> findAll() {
        return Db.find("select * from news");
    }

    public void add(String title, String content) {
        Record news = new Record().set("title", title);
        news.set("content", content);
        Db.save("news", news);
    }

    public void delete(Integer id) {
        Db.deleteById("news", id);
    }

    public void update(Integer id, String title, String content) {
        Record news = Db.findById("news", id).set("title", title).
                set("content", content);
        Db.update("news", news);
    }

    public Record findById(Integer id) {
        return Db.findById("news",id);
    }
}

6.dao包中的实体类:News:

public class News extends Model<News> {
  //什么都不写也可以
}

7.index.html 主界面的前端代码:只贴了body,前面的省略。前端取值要根据jfinal的版本,我的应该是3

<body>
    <h2  align="center">新闻列表</h2>
<div class="container">
    <table class="table table-hover">
        <tbody>
            <thead class="info" >
                <th width="100" >ID</th>
                <th>标题</th>
                <th>内容</th>
                <th>操作</th>
            </thead>

            #for(n : news)
            <tr class="rowDetail">
                <td>#(n.id)</td>
                <td>#(n.title)</td>
                <td>#(n.content)</td>
                <td><a href="/news/edit/#(n.id)">修改</a> <a href="/news/delete/#(n.id)">删除</a></td>
            </tr>
            #end

        </tbody>
    </table>
        <a href="/news/add" class="btn btn-success " >新增新闻</a>
</div>
</body>

8.add.html和edit.html就是简单的表单,注意提交路径即可。路径映射为:/Controller配置路径/Controller中的方法名

9.jfinal提供访问数据库的方法很多,你可以选择合适的。比如使用model、dao,直接写sql等。登录那块代码也很简单,这里就不贴了。