Experience JBOOT (2)--Database

content:

  1. Experience JBOOT (1) -- Hello, Jboot
  2. Experience JBOOT (2)--Database
  3. Experience JBOOT (3)--RPC
  4. Experience JBOOT (4) -- jboot-admin articles

 

  1. Create a database (in the example, the database name is demo, the username and password are also demo, and there is a table in the database called user)
  2. Create a jboot.properties file under resources with the following configuration:

    jboot.datasource.type=mysql
    
    jboot.datasource.url=jdbc:mysql://localhost/demo?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull
    
    jboot.datasource.user=demo
    
    jboot.datasource.password=demo

     

  3. Modify MyController.java created in the previous section and change it to: 
    package io.jboot.hello;
    
    import com.jfinal.plugin.activerecord.Db;
    import com.jfinal.plugin.activerecord.Record;
    import io.jboot.Jboot;
    import io.jboot.web.controller.JbootController;
    import io.jboot.web.controller.annotation.RequestMapping;
    
    import java.util.List;
    
    /**
     * Created by Tom on 2018/3/13.
     */
    @RequestMapping("/")
    public class HelloController extends JbootController {
        public void index(){
            List<Record> users = Db.find("select * from user");
            setAttr("users", users);
            render("index.htlm");
        }
    
        public static void main(String[] args){
            Jboot.run(args);
        }
    }
    

     

  4. Create an index.html template file in the resource directory:
     

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>用户信息</title>
    </head>
    <body>
    <h3>用户信息:</h3>
    
    #for (user : users)
        #(user.username) <br>
    #end
    </body>
    </html>

  5. Rerun the program to see the results

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324440703&siteId=291194637