Brainless operation: Eclipse + Maven + jFinal + MariaDB environment construction

Posted by gat on Sat, 11 May 2019 13:30:00 +0200

I. Development Environment

1. windows 7 Enterprise Edition

2,Eclipse IDE for Enterprise Java Developers  Version: 2019-03 (4.11.0)

3,JDK 1.8

4,Maven 3.5.2

5,MariaDB

6,Tomcat 8.5

II. Basic Configuration

1. The settings of Maven in Eclipse are as follows

2. The database uses the default test library to create table categories

1 CREATE TABLE category
2 (
3     categoryid INT AUTO_INCREMENT PRIMARY KEY,
4     categoryname VARCHAR(10) NOT NULL
5 );
6 
7 INSERT INTO category VALUES(NULL, 'books'), (NULL, 'Beauty makeup');
8 
9 SELECT * FROM category;

3. Environmental Construction

1. Creating Maven Project

2. Use default workspace

3. Selecting the Initial Type of Engineering webapp

4. Fill in the package name and project name

5. Click Finish to start the Maven project. When the creation is complete, the error that the Servlet package cannot be found will be prompted. This problem can be solved by importing Tomcat.

     

           

             

Delete the default index.jsp file and use the html page later. Create a new classes directory under the WEB-INF directory of the webapp directory. The final directory structure is as follows

6. Writing pom.xml file

 1 <project xmlns="http://maven.apache.org/POM/4.0.0"
 2     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <groupId>cn.temptation</groupId>
 6     <artifactId>jfinalDemo</artifactId>
 7     <packaging>war</packaging>
 8     <version>0.0.1-SNAPSHOT</version>
 9     <name>jfinalDemo Maven Webapp</name>
10     <url>http://maven.apache.org</url>
11     <dependencies>
12         <dependency>
13             <groupId>junit</groupId>
14             <artifactId>junit</artifactId>
15             <version>3.8.1</version>
16             <scope>test</scope>
17         </dependency>
18         <!-- Add to jfinal rely on -->
19         <dependency>
20             <groupId>com.jfinal</groupId>
21             <artifactId>jfinal</artifactId>
22             <version>3.8</version>
23         </dependency>
24         <!-- Add to jetty server rely on -->
25         <dependency>
26             <groupId>com.jfinal</groupId>
27             <artifactId>jetty-server</artifactId>
28             <version>2019.3</version>
29             <!-- Use IDEA When developing, scope Set to become compile,Otherwise, the prompt is missing. jar Pack, remember to change it back when packing provided -->
30             <scope>provided</scope>
31         </dependency>
32         <!-- mysql drive -->
33         <dependency>
34             <groupId>mysql</groupId>
35             <artifactId>mysql-connector-java</artifactId>
36             <version>5.1.37</version>
37         </dependency>
38         <!-- druid Connection pool -->
39         <dependency>
40             <groupId>com.alibaba</groupId>
41             <artifactId>druid</artifactId>
42             <version>1.0.29</version>
43         </dependency>
44     </dependencies>
45     <build>
46         <finalName>jfinalDemo</finalName>
47     </build>
48 </project>

7. Write web.xml configuration file, the main thing is to set up a filter

 1 <!DOCTYPE web-app PUBLIC
 2  "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 3  "http://java.sun.com/dtd/web-app_2_3.dtd" >
 4 
 5 <web-app>
 6     <filter>
 7         <filter-name>jFinal</filter-name>
 8         <filter-class>com.jfinal.core.JFinalFilter</filter-class>
 9         <init-param>
10             <param-name>configClass</param-name>
11             <param-value>cn.temptation.config.MyConfig</param-value>
12         </init-param>
13     </filter>
14     <filter-mapping>
15         <filter-name>jFinal</filter-name>
16         <url-pattern>/*</url-pattern>
17     </filter-mapping>
18 </web-app>

8. Write the database connection configuration file db.properties. In order to read the content of the configuration file in the framework, put the file in the classes directory under the WEB-INF directory of the webapp directory created earlier.

1 jdbcUrl = jdbc:mysql://localhost/test?characterEncoding=utf8
2 user = root
3 password = sa
4 devMode = true
5 showSql = true

9. Write the entity class Category, which inherits from com.jfinal.plugin.activerecord.Model because ActiveRecord is used

1 package cn.temptation.bean;
2 
3 import com.jfinal.plugin.activerecord.Model;
4 
5 @SuppressWarnings("serial")
6 public class Category extends Model<Category> {
7 
8 }

10. Write configuration class MyConfig

 1 package cn.temptation.config;
 2 
 3 import com.jfinal.config.Constants;
 4 import com.jfinal.config.Handlers;
 5 import com.jfinal.config.Interceptors;
 6 import com.jfinal.config.JFinalConfig;
 7 import com.jfinal.config.Plugins;
 8 import com.jfinal.config.Routes;
 9 import com.jfinal.core.JFinal;
10 import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
11 import com.jfinal.plugin.druid.DruidPlugin;
12 import com.jfinal.template.Engine;
13 
14 import cn.temptation.bean.Category;
15 import cn.temptation.web.CategoryController;
16 
17 public class MyConfig extends JFinalConfig {
18     /**
19      * Const constant
20      */
21     @Override
22     public void configConstant(Constants me) {
23         // Load the database configuration file
24         loadPropertyFile("db.properties");
25         me.setDevMode(true);
26         // Open support annotations, support Controller,Interceptor Use in @Inject Inject the business layer and implement it automatically AOP
27         me.setInjectDependency(true);
28     }
29 
30     @Override
31     public void configRoute(Routes me) {
32         me.add("/", CategoryController.class);
33     }
34 
35     /**
36      * Configuration plug-ins
37      */
38     @Override
39     public void configPlugin(Plugins me) {
40         // To configure druid Connection pool
41         DruidPlugin db = new DruidPlugin(getProperty("jdbcUrl"), getProperty("user"), getProperty("password"));
42         me.add(db);
43         // ActiveRecord Be as JFinal Of Plugin It exists, so it needs to be used. JFinalConfig Middle configuration ActiveRecordPlugin
44         ActiveRecordPlugin activeRecordPlugin = new ActiveRecordPlugin(db);
45         activeRecordPlugin.addMapping("category", Category.class);
46         // Exhibition sql Sentence
47         activeRecordPlugin.setShowSql(true);
48         me.add(activeRecordPlugin);
49     }
50 
51     /**
52      * Configuring Global Interceptor
53      */
54     @Override
55     public void configInterceptor(Interceptors me) {
56 
57     }
58 
59     /**
60      * Configuration Processor
61      */
62     @Override
63     public void configHandler(Handlers me) {
64 
65     }
66 
67     /**
68      * Configuration template
69      */
70     @Override
71     public void configEngine(Engine me) {
72 
73     }
74 
75     /**
76      * Starting method
77      * 
78      * @param args
79      */
80     public static void main(String[] args) {
81         JFinal.start("src/main/webapp", 8080, "/", 5);
82     }
83 }

11. Write the controller Category Controller, inherited from com.jfinal.core.Controller

 1 package cn.temptation.web;
 2 
 3 import java.util.List;
 4 
 5 import com.jfinal.core.Controller;
 6 
 7 import cn.temptation.bean.Category;
 8 
 9 public class CategoryController extends Controller {
10     private Category categoryDao = new Category().dao();
11 
12     public void index() {
13         List<Category> categories = categoryDao.find("SELECT * FROM category");
14         setAttr("categories", categories);
15         render("category.html");
16     }
17 }

12. Create a new category.html in the webapp directory to present the category data obtained by the server.

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4 <meta charset="UTF-8">
 5 <title>Category list</title>
 6 <style>
 7 .tblCategory {
 8     border: 1px solid black;
 9     border-collapse: collapse;
10 }
11 </style>
12 </head>
13 <body>
14     <table border="1" class="tblCategory">
15         <tr>
16             <th width="100px">Category number</th>
17             <th width="150px">Category name</th>
18         </tr>
19         #for(category:categories)
20         <tr>
21             <td>#(category.categoryid)</td>
22             <td>#(category.categoryname)</td>
23         </tr>
24         #end
25     </table>
26 </body>
27 </html>

13. Start the program, run directly in MyConfig class (ctrl + F11) or debug program (F11), if everything goes well, you can see the following. This is a way to integrate jetty as a web container

 

14. Catalogue structure of the whole project

15. To publish a war package, it's easy to drop it into tomcat. Just right-click on the project to find Run AS - - > Maven build, enter clean package, and then click Run, you can make war package. Drop the generated war package into tomcat's webapps directory and start tomcat. Note that the path accessed under Tomcat defaults to the directory of the project. For example, the path accessed in this example should be http://localhost:8080/jfinalDemo/

           

               

Topics: Java Maven Tomcat Druid