SpringBoot+MybatisPlus+Mysql+JSP實(shí)戰(zhàn)
本文主要介紹了SpringBoot+MybatisPlus+Mysql+JSP實(shí)戰(zhàn),分享給大家,具體如下:
放個(gè)效果圖:
準(zhǔn)備項(xiàng)目
首先在MySql控制臺(tái)輸入一下sql語(yǔ)句創(chuàng)建student 數(shù)據(jù)庫(kù)和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
修改項(xiàng)目名稱(chēng),點(diǎn)擊next
這里直接點(diǎn)next
第一次打開(kāi)會(huì)很慢
打開(kāi)后刪除用不到的文件
連接MySql
修改 application.properties 為 application.yml
插入一下代碼
要修改的內(nèi)容: url: jdbc:mysql://localhost:3306/student?useSSL=false&serverTimezone=UTC&&characterEncoding=utf-8中的student改為自己的數(shù)據(jù)庫(kù)名稱(chēng)
spring: #配置 數(shù)據(jù)庫(kù) datasource: username: root #用戶(hù)名 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 打印日志 不需要手寫(xiě)sql 可查看把我們完成的sql mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl # 設(shè)置端口號(hào) server: port: 8001
pom.xml 依賴(lài)包
<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依賴(lài)的jar包start --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> </dependency> <!-- servlet依賴(lài)的jar包start --> <!-- jsp依賴(lài)jar包start --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>javax.servlet.jsp-api</artifactId> <version>2.3.1</version> </dependency> <!-- jsp依賴(lài)jar包end --> <!--jstl標(biāo)簽依賴(lài)的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ù)庫(kù)
IDEA 鏈接本地MySql數(shù)據(jù)庫(kù) (可以確定Mysql能正常訪(fǎng)問(wèn) ,方便我們調(diào)試)
1.點(diǎn)擊屏幕右側(cè)Database
2.點(diǎn)擊如下如的加號(hào)
3.DataSource
4.選擇Mysql
**如上圖所示表示成功連接,如果報(bào)錯(cuò),檢查用戶(hù)名,密碼,數(shù)據(jù)庫(kù)名稱(chēng) **
常見(jiàn)問(wèn)題:時(shí)區(qū)(time zone)相關(guān)的報(bào)錯(cuò)Mysql控制臺(tái)寫(xiě)下面的代碼 重新Test Connection 。
set global time_zone='+8:00';
連接成功可以看到剛才見(jiàn)的數(shù)據(jù)庫(kù)
為了方便我們測(cè)試點(diǎn)擊加號(hào)“+”增加兩條記錄 增加完成后點(diǎn)擊如下圖DB的小圖標(biāo)(如果沒(méi)看到鼠標(biāo)移到大概位置會(huì)顯示別出來(lái))
代碼生成器(不用我們自己寫(xiě)實(shí)體類(lèi),controller ,mapper,service等) 在下圖目錄下測(cè)試類(lèi)新建一個(gè)類(lèi)GenerateCode
代碼如下:
需要修改的地方:
1.這里修改成你自己的
pg.setParent("com.example.xxxx");
2.改稱(chēng)自己的昵稱(chēng)
gc.setAuthor("艾科");
3.把下邊的student 改為自己建的數(shù)據(jù)庫(kù)名稱(chēng)
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.0 dsc.setDriverName("com.mysql.jdbc.Driver"); //8.0以下
5.數(shù)據(jù)庫(kù)用戶(hù)名和密碼
dsc.setUsername("root"); dsc.setPassword("root");
6.最后一個(gè)也是最重要的:這里是自己的數(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"); //獲取項(xiàng)目根目錄 gc.setOutputDir(projectPath+"/src/main/java"); //設(shè)置輸出目錄 gc.setAuthor("艾科"); //代碼注解 gc.setOpen(false); gc.setFileOverride(false); //是否覆蓋(選否)不然會(huì)覆蓋掉寫(xiě)過(guò)的代碼 gc.setServiceName("%sService"); gc.setIdType(IdType.ID_WORKER); // 可以根據(jù)需求改成IdType.AUTO 或者其他 gc.setDateType(DateType.ONLY_DATE); //Date 類(lèi)型 只使用 java.util.date 代替 ag.setGlobalConfig(gc); // 設(shè)置數(shù)據(jù)源 DataSourceConfig dsc=new DataSourceConfig(); //不要忘了修改數(shù)據(jù)庫(kù)名稱(chēng) 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ù)庫(kù)類(lèi)型 ag.setDataSource(dsc); // 包的配置 PackageConfig pg=new PackageConfig(); // pg.setModuleName("") pg.setParent("com.example.xxxx"); //把xxx 改成你自己的 pg.setEntity("entity"); //實(shí)體類(lèi)創(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 名稱(chēng) ,根據(jù)table 名稱(chēng)生成 實(shí)體類(lèi),controller,service, mmapper // strategy.setInclude("student,user,class"); // 多個(gè)表用都逗號(hào)分開(kāi) strategy.setControllerMappingHyphenStyle(true); ag.setStrategy(strategy); ag.execute(); }
改完了執(zhí)行該類(lèi)
MyBatis Plus
把下圖目錄中的xxxxApplication 加上 @MapperScan(“com.xxxx.xx.mapper”) mapper 包名r如下圖所示(改成你自己的mapper 的包名)
**如果怕敲錯(cuò)可以復(fù)制StudentMpaper 中的packege **
@MapperScan("com.example.student.mapper") @SpringBootApplication public class StudentApplication { public static void main(String[] args) { SpringApplication.run(StudentApplication.class, args); } }
MyBatis Plus 簡(jiǎn)單查詢(xún) (這個(gè)可以留到最后寫(xiě)作業(yè)的時(shí)候?qū)W PS:肯定會(huì)用到)
@Autowired StudentMapper studentMapper; // Mybatis plus 查詢(xún) student 表中的數(shù)據(jù) 返回List 類(lèi)型 // 相當(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); // 通過(guò)id 查詢(xún) 相當(dāng)于:SELECT stu_id,stu_name,stu_sex,date,room,acadimy FROM student WHERE stu_id=1 Student student1 = studentMapper.selectById(1); // 條件查詢(xún) 查詢(xún)單個(gè) 相當(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); // 條件查詢(xún) 查詢(xún)列表 相當(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), 計(jì)算機(jī)(String) Student student=new Student(); student.setStuName("aike"); student.setStuSex("男"); student.setDate(date); student.setRoom(226); student.setAcadimy("計(jì)算機(jī)"); studentMapper.insert(student);
更多復(fù)雜查詢(xún)查詢(xún)官網(wǎng)-----> MyBatis-Plus 官網(wǎng)
訪(fǎng)問(wèn)JSP頁(yè)面
之前在pom.xml 中導(dǎo)入了相關(guān)的依賴(lài)包了
在mian 目錄下創(chuàng)建 webapp 文件夾
在webapp 目錄下創(chuàng)建 student.jsp文件
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("查詢(xún)記錄:"${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é)時(shí)間</th> <th>宿舍號(hào)</th> <td><button class="btn btn-success" onclick="return inserrtStudent()" >新增學(xué)生</button></td> <td><button class="btn btn-success" 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 class="btn btn-default" onclick="return updateRoom(${stu})">修改宿舍</button></td> <td><button class="btn btn-default" onclick="return updateRecord()">查詢(xún)記錄</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ù)需要只加載單個(gè)插件。 --> <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 查詢(xún) student 表中的數(shù)據(jù) 返回List 類(lèi)型 // 相當(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"; } }
運(yùn)行結(jié)果(運(yùn)行按鈕在右上角):localhost:你的端口號(hào)/student/findall
到此這篇關(guān)于SpringBoot+MybatisPlus+Mysql+JSP實(shí)戰(zhàn)的文章就介紹到這了,更多相關(guān)SpringBoot MybatisPlus Mysql JSP內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA中折疊所有Java代碼,再也不怕大段的代碼了,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-10-10Java實(shí)現(xiàn)表單提交(支持多文件同時(shí)上傳)
本文介紹了Java、Android實(shí)現(xiàn)表單提交(支持多文件同時(shí)上傳)的方法,具有一定的參考價(jià)值,下面跟著小編一起來(lái)看下吧2017-01-01SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對(duì)數(shù)據(jù)庫(kù)
這篇文章主要介紹了SpringBoot中使用com.alibaba.druid.filter.config.ConfigTools對(duì)數(shù)據(jù)庫(kù)密碼加密的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-01-01解決IDEA無(wú)法讀取maven鏡像,jar包下載失敗的問(wèn)題
這篇文章主要介紹了解決IDEA無(wú)法讀取maven鏡像,jar包下載失敗的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-10-10java時(shí)間戳與日期相互轉(zhuǎn)換工具詳解
這篇文章主要為大家詳細(xì)介紹了java各種時(shí)間戳與日期之間相互轉(zhuǎn)換的工具,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12mybatis的if判斷不要使用boolean值的說(shuō)明
這篇文章主要介紹了mybatis的if判斷不要使用boolean值的說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-11-11idea如何在service窗口中顯示多個(gè)服務(wù)
這篇文章主要介紹了idea如何在service窗口中顯示多個(gè)服務(wù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-09-09Java中的線(xiàn)程池如何實(shí)現(xiàn)線(xiàn)程復(fù)用
這篇文章主要介紹了Java中的線(xiàn)程池如何實(shí)現(xiàn)線(xiàn)程復(fù)用問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03MyBatisPlus實(shí)現(xiàn)自動(dòng)填充字段的實(shí)踐
MyBatis-Plus自動(dòng)填充功能可以在插入或更新數(shù)據(jù)時(shí)自動(dòng)填充字段,如創(chuàng)建時(shí)間和更新時(shí)間,本文就來(lái)詳細(xì)的介紹一下,具有一定的參考價(jià)值,感興趣的可以了解一下2024-10-10