Skip to main content

Web Application Edition (Database Operations)

To make the common examples easy to demonstrate, you need to install a mysql database before starting.

Create the database and table:

CREATE DATABASE `user-demo` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
use user-demo;
CREATE TABLE `t_user` (
`id` int NOT NULL AUTO_INCREMENT COMMENT 'User ID',
`username` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Username',
`password` varchar(32) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Password',
`email` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL COMMENT 'Email',
`phone` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL COMMENT 'Phone number',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT 'Created at',
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'Updated at',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='User table';

1. Download and Run​

Download the sample dawdler-tutorial.zip.

After extracting it, go into the db-single-web directory.

Start the database and configure user_DataSource.properties.

Run the main method of UserWebApplication.java.

@DawdlerBootApplication
public class UserWebApplication {
public static void main(String[] args) throws Throwable {
DawdlerWebApplication.run(UserWebApplication.class, args);
}
}

1.1 Project Structure​

controller -> UserController.java entry point of the web api

service -> UserService.java service interface

service.impl -> UserServiceImpl.java service implementation layer

mapper -> UserMapper.java database access layer

entity -> User.java entity class

1.2 Configuration Files​

1.2.1 undertow.yml​

Common web container configuration

undertow configuration

1.2.2 web-conf.xml​

Web configuration - component scanning

Web configuration - transaction configuration file

Web configuration - mybatis configuration file

1.2.3 user_DataSource.properties​

Database configuration

1.3 Running as a Jar​

Run

mvn clean package

to get db-single-web-0.0.1-jdk25-SNAPSHOT-all.jar

Then run

java -jar db-single-web-0.0.1-jdk25-SNAPSHOT-all.jar

1.4 Verification​

After startup, visit http://localhost:8085/user/info?id=1.