一、项目构建
1.项目结构
2.创建工程
在IDEA中点击文件->新建->项目->选择maven工程并输入项目名称
二、项目代码
1.pom.xml文件内容
<?xml version="1.0" encoding="UTF-8"?>
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.1
com.ls
boot-mybatis-javafx
0.0.1-SNAPSHOT
boot-mybatis-javafx
boot-mybatis-javafx
2.application.yml内容
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: root
url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8&serverTimezone=Asia/Shanghai
mybatis:
mapper-locations: classpath:com/ls/mapper/*.xml
3.启动类BootMybatisJavafxApplication.java内容
由于@FXMLController控制器无法由spring管理,所用@Autowire无法自动注入,写了一个静态方法getContext获取上下文,再通过getBean()获取指定的bean
package com.ls.bootmybatisjavafx;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Screen;
import javafx.stage.Stage;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;
@SpringBootApplication
@MapperScan(value = {“com.ls.bootmybatisjavafx.mapper”}) // mybatis扫包
public class BootMybatisJavafxApplication extends Application {
}
4.Login.fxml内容(此文件放在resources目录下)
通过idea右键创建后 鼠标移动到文件 右键 Open In SceneBuilder 打开后在SceneBuilder 画图保存后会在当前文件自动生成代码(前提是吧SceneBuilder整合进idea)
<?xml version="1.0" encoding="UTF-8"?> <?import javafx.scene.control.Button?> <?import javafx.scene.control.PasswordField?> <?import javafx.scene.control.TextField?> <?import javafx.scene.layout.AnchorPane?> <?import javafx.scene.text.Font?> <?import javafx.scene.text.Text?>
import com.ls.bootmybatisjavafx.BootMybatisJavafxApplication;
import com.ls.bootmybatisjavafx.mapper.UserMapper;
import com.ls.bootmybatisjavafx.vo.User;
import de.felixroske.jfxsupport.FXMLController;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonType;
import javafx.scene.control.DialogPane;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationContext;
import java.net.URL;
import java.util.ResourceBundle;
@FXMLController
public class UserController implements Initializable {
}
6.User.java内容
package com.ls.bootmybatisjavafx.vo;
import lombok.Data;
import java.io.Serializable;
@Data
public class User implements Serializable {
}
7.UserMapper内容
package com.ls.bootmybatisjavafx.mapper;
import com.ls.bootmybatisjavafx.vo.User;
import org.apache.ibatis.annotations.Mapper;
import java.util.List;
@Mapper
public interface UserMapper {
User login(User tmp);
int register(User tmp);
int selectByName(User tmp);
}
三、运行截图
四、数据库sql文件内容
create table demo.user
(
user_id int(32) auto_increment comment ‘用户id’
primary key,
user_name varchar(32) null comment ‘用户名称’,
user_password varchar(32) null comment ‘用户密码’,
);
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:https://bianchenghao.cn/bian-cheng-ri-ji/70286.html