利用springboot创建一个以javafx为前端页面展示,用mybatis作为沟通数据库的项目。
本例子只完成登录功能。
1.创建目录
Application:javafx程序主入口
Controller:前端页面控制层
mapper:mybatis 查询接口和xml
pojo:实体类
view:javafx视图
application.properties: 配置文件
2.导入pom文件
导入javafx
UTF-8
zh_CN
1.8
${java.version}
2.1.1.RELEASE
2.1.7
org.springframework.boot
spring-boot-starter-actuator
${spring.boot.version}
org.springframework.boot
spring-boot-starter
${spring.boot.version}
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
${spring.boot.version}
de.roskenet
springboot-javafx-support
${springboot-javafx-support.version}
导入数据库驱动
mysql
mysql-connector-java
5.1.45
org.mybatis.spring.boot
mybatis-spring-boot-starter
1.1.1
3.建表并添加一条语句
-- ----------------------------
-- Table structure for `login_user_info`
-- ----------------------------
DROP TABLE IF EXISTS `login_user_info`;
CREATE TABLE `login_user_info` (
`userId` varchar(20) NOT NULL COMMENT '用户id',
`loginName` varchar(20) NOT NULL COMMENT '用户名',
`userName` varchar(20) NOT NULL COMMENT '真实姓名',
`password` varchar(20) NOT NULL COMMENT '登录密码',
`accountType` varchar(20) NOT NULL COMMENT '账号类型',
`creatDate` datetime DEFAULT NULL COMMENT '创建时间',
PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO login_user_info
VALUES (‘1c98af14-7027-40a2-’, ‘11’, ‘张三’, ‘1’, ‘管理员’, ‘2019-03-23 13:36:00’);
4.配置文件
spring.datasource.url=jdbc:mysql://localhost:3306/hand?useUnicode=true&characterEncoding=utf8
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.username=
spring.datasource.password=
5.编写pojo
public class LoginUserInfo {
private String userId;
private String loginName;
private String password;
private String userName;
private String accountType;
private String creatDate;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAccountType() {
return accountType;
}
public void setAccountType(String accountType) {
this.accountType = accountType;
}
public String getCreatDate() {
return creatDate;
}
public void setCreatDate(String creatDate) {
this.creatDate = creatDate;
}
}
6.编写查询接口和对应xml
package mapper;
public interface queryMapper {
public LoginUserInfo queryLoginInfo(@Param("loginName") String loginName , @Param("password")String password);
public Integer queryLoginInfoByLoginName(@Param("loginName") String loginName);
}
namespace:对应接口的全类名
7.编写fxml前端页面
fx:id:唯一标识 fx:controller:控制层
8.编写Controller
package controller;
import de.felixroske.jfxsupport.FXMLController;
import de.felixroske.jfxsupport.GUIState;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.control.Alert;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import mapper.QueryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import pojo.LoginUserInfo;
import java.net.URL;
import java.util.ResourceBundle;
@FXMLController
public class LoginController implements Initializable {
@FXML
private GridPane login;
@FXML
private TextField userName;
@FXML
private PasswordField passwordField;
@Autowired
private QueryMapper query;
public void initialize(URL location, ResourceBundle resources) {
}
/**
* 登录
*
*/
@FXML
private void handleSubmitButtonAction(){
String userNameStr = userName.getText().trim();
String passwordStr = passwordField.getText().trim();
Alert alert = new Alert(Alert.AlertType.INFORMATION);
alert.setHeaderText(null);
String str = "";
if (userNameStr == null && passwordStr == null) {
str = "用户名和密码不能为空!";
} else if (userNameStr == null) {
str = "用户名不能为空!";
} else if (userNameStr == null) {
str = "密码不能为空!";
}
if (str != null) {
alert.setContentText(str);
alert.showAndWait();
return;
}
int num = query.queryLoginInfoByLoginName(userNameStr);
if (num < 1) {
alert.setContentText("用户名错误,请检查用户名!");
alert.showAndWait();
return;
} else {
LoginUserInfo info = checkLogin(userNameStr, passwordStr);
// 登录
if (info != null) {
GUIState.getScene().setRoot(successpage());
GUIState.getStage().setWidth(1000);
GUIState.getStage().setHeight(700);
GUIState.getStage().setX(150);
GUIState.getStage().setY(50);
GUIState.getStage().setResizable(true);
} else {
alert.setContentText("请输入正确的密码!");
alert.showAndWait();
}
}
}
public Parent successpage(){
HBox hbox = new HBox();
Label label = new Label("登录成功");
hbox.getChildren().add(label);
hbox.setAlignment(Pos.CENTER);
return hbox;
}
/**
* 判断账号和密码是否正确
*/
private LoginUserInfo checkLogin(String userName, String password) {
return query.queryLoginInfo(userName, password);
}
}
9.编写view
package view;
import de.felixroske.jfxsupport.AbstractFxmlView;
import de.felixroske.jfxsupport.FXMLView;
@FXMLView(fxml = "路径\LayoutPage_login.fxml")
public class LayoutView extends AbstractFxmlView {
}
10.编写程序主入口
package Application;
import de.felixroske.jfxsupport.AbstractJavaFxApplicationSupport;
import javafx.application.Application;
import javafx.stage.Stage;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import view.LayoutView;
@SpringBootApplication
@MapperScan("main.**")
public class Main extends AbstractJavaFxApplicationSupport{
public static void main(String[] args) {
launch(Main.class , LayoutView.class ,args);
}
}