电脑知识|欧美黑人一区二区三区|软件|欧美黑人一级爽快片淫片高清|系统|欧美黑人狂野猛交老妇|数据库|服务器|编程开发|网络运营|知识问答|技术教程文章 - 好吧啦网

您的位置:首頁技術(shù)文章
文章詳情頁

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

瀏覽:32日期:2023-02-06 10:13:11

本文主要介紹了SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn),分享給大家,具體如下:

放個效果圖:

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

準(zhǔn)備項目

首先在MySql控制臺輸入一下sql語句創(chuàng)建student 數(shù)據(jù)庫和student。

create databse student;use student;CREATE TABLE `student` ( `stu_id` bigint(20) NOT NULL, `stu_name` varchar(45) DEFAULT NULL, `stu_sex` varchar(6) DEFAULT NULL, `date` varchar(45) DEFAULT NULL, `room` int(2) DEFAULT NULL, `acadimy` varchar(45) DEFAULT NULL, PRIMARY KEY (`stu_id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

SpringBoot

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

修改項目名稱,點擊next

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

這里直接點next

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

第一次打開會很慢打開后刪除用不到的文件

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

連接MySql

修改 application.properties 為 application.yml

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

插入一下代碼要修改的內(nèi)容: url: jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&&characterEncoding=utf-8中的student改為自己的數(shù)據(jù)庫名稱

spring: #配置 數(shù)據(jù)庫 datasource: username: root #用戶名 password: akbar #密碼 #下一行中student 改為 自己建的database url: jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&&characterEncoding=utf-8 driver-class-name: com.mysql.cj.jdbc.Driver# 配置JSP 路徑 mvc: view: prefix: / suffix: .jsp#mybatis-plus 打印日志 不需要手寫sql 可查看把我們完成的sql mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl# 設(shè)置端口號server: port: 8001

pom.xml 依賴包

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency><!-- MYsql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency><!-- mybatis-plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.0.5</version> </dependency> <!-- 模板引擎 --> <dependency> <groupId>org.apache.velocity</groupId> <artifactId>velocity-engine-core</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> </dependency> <!-- servlet依賴的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- servlet依賴的jar包start --> <!-- jsp依賴jar包start --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <!-- jsp依賴jar包end --> <!--jstl標(biāo)簽依賴的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions><exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId></exclusion> </exclusions> </dependency> </dependencies>

IDEA 鏈接數(shù)據(jù)庫

IDEA 鏈接本地MySql數(shù)據(jù)庫 (可以確定Mysql能正常訪問 ,方便我們調(diào)試)1.點擊屏幕右側(cè)Database2.點擊如下如的加號3.DataSource4.選擇Mysql

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

**如上圖所示表示成功連接,如果報錯,檢查用戶名,密碼,數(shù)據(jù)庫名稱 **

常見問題:時區(qū)(time zone)相關(guān)的報錯Mysql控制臺寫下面的代碼 重新Test Connection 。

set global time_zone=’+8:00’;

連接成功可以看到剛才見的數(shù)據(jù)庫

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

為了方便我們測試點擊加號“+”增加兩條記錄 增加完成后點擊如下圖DB的小圖標(biāo)(如果沒看到鼠標(biāo)移到大概位置會顯示別出來)

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

代碼生成器(不用我們自己寫實體類,controller ,mapper,service等) 在下圖目錄下測試類新建一個類GenerateCode

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

代碼如下:

需要修改的地方:1.這里修改成你自己的

pg.setParent('com.example.xxxx');

2.改稱自己的昵稱

gc.setAuthor('艾科');

3.把下邊的student 改為自己建的數(shù)據(jù)庫名稱

dsc.setUrl('jdbc:mysql://localhost:3306/studentuseSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8');`

4.// 版本8.0以下去掉中間的cj

dsc.setDriverName('com.mysql.cj.jdbc.Driver'); //8.0dsc.setDriverName('com.mysql.jdbc.Driver'); //8.0以下

5.數(shù)據(jù)庫用戶名和密碼

dsc.setUsername('root'); dsc.setPassword('root');

6.最后一個也是最重要的:這里是自己的數(shù)據(jù)不哭表

strategy.setInclude('student');

代碼如下:

public class GenerateCode { public static void main(String[] args) { AutoGenerator ag=new AutoGenerator();// 全局配置 GlobalConfig gc=new GlobalConfig(); String projectPath=System.getProperty('user.dir'); //獲取項目根目錄 gc.setOutputDir(projectPath+'/src/main/java'); //設(shè)置輸出目錄 gc.setAuthor('艾科'); //代碼注解 gc.setOpen(false); gc.setFileOverride(false); //是否覆蓋(選否)不然會覆蓋掉寫過的代碼 gc.setServiceName('%sService'); gc.setIdType(IdType.ID_WORKER); // 可以根據(jù)需求改成IdType.AUTO 或者其他 gc.setDateType(DateType.ONLY_DATE); //Date 類型 只使用 java.util.date 代替 ag.setGlobalConfig(gc);// 設(shè)置數(shù)據(jù)源 DataSourceConfig dsc=new DataSourceConfig(); //不要忘了修改數(shù)據(jù)庫名稱 dsc.setUrl('jdbc:mysql://localhost:3306/student?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8'); dsc.setDriverName('com.mysql.cj.jdbc.Driver');//8.0用com.mysql.cj.jdbc.Driver 5.7用com.mysql.jdbc.Driver dsc.setUsername('root'); dsc.setPassword('root'); dsc.setDbType(DbType.MYSQL); //數(shù)據(jù)庫類型 ag.setDataSource(dsc);// 包的配置 PackageConfig pg=new PackageConfig();// pg.setModuleName('') pg.setParent('com.example.xxxx'); //把xxx 改成你自己的 pg.setEntity('entity'); //實體類創(chuàng)建目錄 pg.setMapper('mapper');//mapper pg.setController('controller');//controoler ag.setPackageInfo(pg); StrategyConfig strategy = new StrategyConfig(); strategy.setNaming(NamingStrategy.underline_to_camel); //代碼風(fēng)格駝峰結(jié)構(gòu) strategy.setColumnNaming(NamingStrategy.underline_to_camel); strategy.setEntityLombokModel(false); strategy.setRestControllerStyle(true); strategy.setInclude('student'); // table 名稱 ,根據(jù)table 名稱生成 實體類,controller,service, mmapper // strategy.setInclude('student,user,class'); // 多個表用都逗號分開 strategy.setControllerMappingHyphenStyle(true); ag.setStrategy(strategy); ag.execute(); }

改完了執(zhí)行該類

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

MyBatis Plus

把下圖目錄中的xxxxApplication 加上 @MapperScan(“com.xxxx.xx.mapper”) mapper 包名r如下圖所示(改成你自己的mapper 的包名)

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

**如果怕敲錯可以復(fù)制StudentMpaper 中的packege **

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

@MapperScan('com.example.student.mapper')@SpringBootApplicationpublic class StudentApplication { public static void main(String[] args) { SpringApplication.run(StudentApplication.class, args); }}

MyBatis Plus 簡單查詢 (這個可以留到最后寫作業(yè)的時候?qū)W PS:肯定會用到)

@Autowired StudentMapper studentMapper; // Mybatis plus 查詢 student 表中的數(shù)據(jù) 返回List 類型// 相當(dāng)于: SELECT stu_id,stu_name,stu_sex,date,room,acadimy FROM student List<Student> list = studentMapper.selectList(null); list.forEach(System.out::println);// 通過id 查詢 相當(dāng)于:SELECT stu_id,stu_name,stu_sex,date,room,acadimy FROM student WHERE stu_id=1 Student student1 = studentMapper.selectById(1);// 條件查詢 查詢單個 相當(dāng)于:SELECT stu_id,stu_name,stu_sex,date,room,acadimy FROM student WHERE stu_name = ? AND stu_sex = ? QueryWrapper<Student> wrapper = new QueryWrapper<>(); wrapper.eq('stu_name', '小明'); wrapper.eq('stu_sex', '男'); Student student2 = studentMapper.selectOne(wrapper); // 條件查詢 查詢列表 相當(dāng)于:SELECT stu_id,stu_name,stu_sex,date,room,acadimy FROM student WHERE stu_id > 1 QueryWrapper<Student> wrapper1 = new QueryWrapper<>(); wrapper1.gt('stu_id', 1); Student student3 = studentMapper.selectOne(wrapper1); SimpleDateFormat simpleDateFormat=new SimpleDateFormat('yyyy-MM-dd'); String date=simpleDateFormat.format(System.currentTimeMillis());// insert 相當(dāng)于 :// INSERT INTO student ( stu_id, stu_name, stu_sex, date, room, acadimy ) VALUES ( ?, ?, ?, ?, ?, ? ) //==> Parameters: 1280830334286217217(Long), aike(String), 男(String), 2020-07-08(String), 226(Integer), 計算機(String) Student student=new Student(); student.setStuName('aike'); student.setStuSex('男'); student.setDate(date); student.setRoom(226); student.setAcadimy('計算機'); studentMapper.insert(student);

更多復(fù)雜查詢查詢官網(wǎng)-----> MyBatis-Plus 官網(wǎng)

訪問JSP頁面

之前在pom.xml 中導(dǎo)入了相關(guān)的依賴包了

在mian 目錄下創(chuàng)建 webapp 文件夾

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

在webapp 目錄下創(chuàng)建 student.jsp文件

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

student.jsp文件內(nèi)容如下 把瞎下面的文件放到 student.jsp

<%@ page language='java' contentType='text/html; charset=utf-8' pageEncoding='utf-8' %><%@ taglib prefix='c' uri='http://java.sun.com/jsp/jstl/core' %><!DOCTYPE html PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'><html><head> <meta http-equiv='Content-Type' content='text/html; charset=utf-8'> <title>學(xué)生信息</title> <link rel='external nofollow' rel='stylesheet'> <script type='text/javascript'> inserrtStudent= function() { console.log('新增學(xué)生') alert('新增學(xué)生') } inserrtRoom = function () { alert('新增宿舍') } updateRoom =function ( ) { alert('修改宿舍') } updateRecord =function (stu) { alert('查詢記錄:'${stu.stu_name}) } </script></head><body><div class='row'> <div class='col-md-6'> <table class='table table-striped'> <tr><th>ID</th><th>姓名</th><th>性別</th><th>學(xué)院</th><th>入學(xué)時間</th><th>宿舍號</th><td><button onclick='return inserrtStudent()' >新增學(xué)生</button></td><td><button onclick=' return inserrtRoom()'>新增宿舍</button></td> </tr> <c:if test='${not empty students}'><c:forEach items='${students}' var='stu'> <tr> <td>${stu.stuId}</td> <td>${stu.stuName}</td> <td>${stu.stuSex}</td> <td>${stu.acadimy}</td> <td>${stu.date}</td> <td>${stu.room}</td> <td><button onclick='return updateRoom(${stu})'>修改宿舍</button></td> <td><button onclick='return updateRecord()'>查詢記錄</button></td> </tr></c:forEach> </c:if> </table> </div></div><script src='https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js'></script><!-- 加載 Bootstrap 的所有 JavaScript 插件。你也可以根據(jù)需要只加載單個插件。 --><script src='https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js'></script></body></html>

StudentControoler 代碼如下

注意:StudentController注解是 @Controller 而不是 RestController 。

/** * * @author 艾科 * @since 2020-07-08 */@Controller@RequestMapping('/student')public class StudentController { @Autowired StudentMapper studentMapper; @RequestMapping(value = 'findall') public String findAll(Model model) {// Mybatis plus 查詢 student 表中的數(shù)據(jù) 返回List 類型// 相當(dāng)于: SELECT stu_id,stu_name,stu_sex,date,room,acadimy FROM student List<Student> list = studentMapper.selectList(null); model.addAttribute('students', list); return 'student'; }}

運行結(jié)果(運行按鈕在右上角):localhost:你的端口號/student/findall

SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)

到此這篇關(guān)于SpringBoot+MybatisPlus+Mysql+JSP實戰(zhàn)的文章就介紹到這了,更多相關(guān)SpringBoot MybatisPlus Mysql JSP內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: Spring
相關(guān)文章:
主站蜘蛛池模板: 保镖公司-私人保镖-深圳保镖公司【环宇兄弟保镖】 | 广州展览制作工厂—[优简]直营展台制作工厂_展会搭建资质齐全 | 快速门厂家-快速卷帘门-工业快速门-硬质快速门-西朗门业 | 河南中专学校|职高|技校招生-河南中职中专网 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 山东钢衬塑罐_管道_反应釜厂家-淄博富邦滚塑防腐设备科技有限公司 | BAUER减速机|ROSSI-MERSEN熔断器-APTECH调压阀-上海爱泽工业设备有限公司 | 基本型顶空进样器-全自动热脱附解吸仪价格-AutoHS全模式-成都科林分析技术有限公司 | 北京租车公司_汽车/客车/班车/大巴车租赁_商务会议/展会用车/旅游大巴出租_北京桐顺创业租车公司 | 2025第九届世界无人机大会| 上海电子秤厂家,电子秤厂家价格,上海吊秤厂家,吊秤供应价格-上海佳宜电子科技有限公司 | 碳纤维复合材料制品生产定制工厂订制厂家-凯夫拉凯芙拉碳纤维手机壳套-碳纤维雪茄盒外壳套-深圳市润大世纪新材料科技有限公司 | 体检车_移动CT车_CT检查车_CT车_深圳市艾克瑞电气有限公司移动CT体检车厂家-深圳市艾克瑞电气有限公司 | 洁净实验室工程-成都手术室净化-无尘车间装修-四川华锐净化公司-洁净室专业厂家 | 立式矫直机_卧式矫直机-无锡金矫机械制造有限公司 | 杰福伦_磁致伸缩位移传感器_线性位移传感器-意大利GEFRAN杰福伦-河南赉威液压科技有限公司 | 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 清管器,管道清管器,聚氨酯发泡球,清管球 - 承德嘉拓设备 | 组织研磨机-高通量组织研磨仪-实验室多样品组织研磨机-东方天净 传递窗_超净|洁净工作台_高效过滤器-传递窗厂家广州梓净公司 | led全彩屏-室内|学校|展厅|p3|户外|会议室|圆柱|p2.5LED显示屏-LED显示屏价格-LED互动地砖屏_蕙宇屏科技 | AGV叉车|无人叉车|AGV智能叉车|AGV搬运车-江西丹巴赫机器人股份有限公司 | 杭州代理记账费用-公司注销需要多久-公司变更监事_杭州福道财务管理咨询有限公司 | 仓储笼_仓储货架_南京货架_仓储货架厂家_南京货架价格低-南京一品仓储设备制造公司 | jrs高清nba(无插件)直播-jrs直播低调看直播-jrs直播nba-jrs直播 上海地磅秤|电子地上衡|防爆地磅_上海地磅秤厂家–越衡称重 | 洗石机-移动滚筒式,振动,螺旋,洗矿机-青州冠诚重工机械有限公司 | 压砖机_电动螺旋压力机_粉末成型压力机_郑州华隆机械tel_0371-60121717 | 全自动包装秤_全自动上袋机_全自动套袋机_高位码垛机_全自动包装码垛系统生产线-三维汉界机器(山东)股份有限公司 | 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 免费分销系统 — 分销商城系统_分销小程序开发 -【微商来】 | 车充外壳,车载充电器外壳,车载点烟器外壳,点烟器连接头,旅行充充电器外壳,手机充电器外壳,深圳市华科达塑胶五金有限公司 | 北京森语科技有限公司-模型制作专家-展览展示-沙盘模型设计制作-多媒体模型软硬件开发-三维地理信息交互沙盘 | 华夏医界网_民营医疗产业信息平台_民营医院营销管理培训 | 包装机_厂家_价格-山东包装机有限公司 | 浴室柜-浴室镜厂家-YINAISI · 意大利设计师品牌 | 咿耐斯 |-浙江台州市丰源卫浴有限公司 | 一体化污水处理设备,一体化污水设备厂家-宜兴市福源水处理设备有限公司 | 皮带机_移动皮带机_大倾角皮带机_皮带机厂家 - 新乡市国盛机械设备有限公司 | 热回收盐水机组-反应釜冷水机组-高低温冷水机组-北京蓝海神骏科技有限公司 | 校园文化空间设计-数字化|中医文化空间设计-党建|法治廉政主题文化空间施工-山东锐尚文化传播公司 | 首页|光催化反应器_平行反应仪_光化学反应仪-北京普林塞斯科技有限公司 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 齿轮减速电机一体机_蜗轮蜗杆减速马达-德国BOSERL齿轮减速机带电机生产厂家 |