#测试javafx+MyBatis
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.example.domain.User;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class mybatisjavafxDemo extends Application {
@Override
public void start(Stage stage) throws Exception {
stage.setTitle("sqlDemo");
BorderPane borderPane =new BorderPane();
HBox hBox =new HBox(5);
Button button =new Button("查询");
hBox.getChildren().addAll(menu11(),button);
borderPane.setTop(hBox);
TextArea textArea =new TextArea();
final TextArea bottom =new TextArea();
//borderPane.setBottom(bottom);
borderPane.setCenter(bottom);
//创建查询事件
button.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
// 1.定义mybatis主配置文件名称,从类路径开始(target/clasess)
String resource = "mybatis-config.xml";
//读取mybatis-config.xml文件
InputStream inputStream = null;
try {
inputStream = Resources.getResourceAsStream(resource);
} catch (IOException e) {
throw new RuntimeException(e);
}
//创建SqlSessionFactory对象
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
//(关键)获取sqlSessionFactory对象,从SqlSessionFactory中获取sqlSession
SqlSession sqlSession =sqlSessionFactory.openSession();
//(关键)指定要执行的sql语句的标识。(sql映射文件的namesapce+id标识)
String Sqlmapper ="org.example.dao.UserDao.selectUsers";
//执行sql语句
List<User> userList = sqlSession.selectList(Sqlmapper);
//输出结果
//userList.forEach(users -> System.out.println(users));
for (User u:userList) {
System.out.println(u);
bottom.appendText(String.valueOf(u)+"\n");}
//释放资源
sqlSession.close();
}
});
Scene scene = new Scene(borderPane);
stage.setScene(scene);
stage.show();
}
public static void main(String[] args) {
launch();
}
public MenuBar menu11(){
MenuBar menuBar =new MenuBar();
Menu create = new Menu("新建");
MenuItem project =new MenuItem("项目");
create.getItems().addAll(project);
Menu system = new Menu("系统设置");
MenuItem aboutus =new MenuItem("关于我们");
MenuItem settings =new MenuItem("系统设置");
MenuItem exit =new MenuItem("退出");
exit.setOnAction(e->{
Platform.exit();
});
system.getItems().addAll(create,aboutus,settings,new SeparatorMenuItem(),exit);
Menu goodsManage = new Menu("商品管理");
MenuItem g1 =new MenuItem("q1");
MenuItem g2 =new MenuItem("q2");
CheckMenuItem t1 =new CheckMenuItem("1");
CheckMenuItem t2 =new CheckMenuItem("2");
Slider slider =new Slider();
CustomMenuItem customMenuItem =new CustomMenuItem(slider);
customMenuItem.setHideOnClick(false);
goodsManage.getItems().addAll(g1,g2,t1,t2,customMenuItem);
menuBar.getMenus().addAll(system,goodsManage);
return menuBar;
};
}