轉義的 HTML 字符進行轉義。
JavaScript
const unescapeHTML = str =>
str.replace(
/&|<|>|'|"/g,
tag =>
({
'&': '&',
'<': '<',
'>': '>',
''': "'",
'"': '"'
}[tag] || tag)
);
示例代碼:
unescapeHTML('<a href="#">Me & you</a>');
// '<a href="#">Me & you</a>'
更多內容請訪問我的網站:https://www.icoderoad.com
在項目開發過程中,有很多業務模塊的代碼是具有一定規律性的,例如controller控制器、service接口、service實現類、mapper接口、model實體類等等,這部分代碼可以使用代碼生成器生成,我們就可以將更多的時間放在業務邏輯上。
傳統的開發步驟:
創建數據庫和表 根據表設計實體類 ? 編寫mapper接口 ? 編寫service接口和實現類 ? 編寫controller控制器 ? 編寫前端頁面 ? 前后端聯調
基于代碼生成器開發步驟:
創建數據庫和表 ? 使用代碼生成器生成實體類、mapper、service、controller、前端頁面 ? 將生成好的代碼拷貝到項目中并做調整 ? 前后端聯調
我們只需要知道數據庫和表相關信息,就可以結合模版生成各個模塊的代碼,減少了很多重復工作,也減少出錯概率,提高效率。
(1)需要對數據庫表解析獲取到元數據,包含表字段名稱、字段類型等等
(2)將通用的代碼編寫成模版文件,部分數據需使用占位符替換
(3)將元數據和模版文件結合,使用一些模版引擎工具(例如freemarker)即可生成源代碼文件
FreeMarker 是一款 模板引擎: 即一種基于模板和要改變的數據, 并用來生成輸出文本(HTML網頁,電子郵件,配置文件,源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發產品的組件。
模板編寫為FreeMarker Template Language (FTL)。它是簡單的,專用的語言, 在模板中,你可以專注于如何展現數據, 而在模板之外可以專注于要展示什么數據。
(1)動態頁面
freemarker可以作為springmvc一種視圖格式,像jsp一樣被瀏覽器訪問。
(2)頁面靜態化
對于一些內容比較多,更新頻率很小,訪問又很頻繁的頁面,可以使用freemarker靜態化,減少DB的壓力,提高頁面打開速度。
(3)代碼生成器
根據配置生成頁面和代碼,減少重復工作,提高開發效率。
(1)創建freemarker-demo模塊,并導入相關依賴
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.itheima</groupId>
<artifactId>freemarker-demo</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.1.RELEASE</version>
</parent>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- freemarker -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
(2)application.yml相關配置
server:
port: 8881 #服務端口
spring:
application:
name: freemarker-demo #指定服務名
freemarker:
cache: false #關閉模板緩存,方便測試
settings:
template_update_delay: 0 #檢查模板更新延遲時間,設置為0表示立即檢查,如果時間大于0會有緩存不方便進行模板測試
suffix: .ftl #指定Freemarker模板文件的后綴名
(3)創建啟動類
package com.heima.freemarker;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class FreemarkerDemotApplication {
public static void main(String[] args) {
SpringApplication.run(FreemarkerDemotApplication.class,args);
}
}
(4)創建Student模型類
package com.itheima.freemarker.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.Date;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
private Integer id;
private String name;//姓名
private Integer age;//年齡
private Date birthday;//生日
private Float money;//錢包
}
(5)創建StudentController
package com.itheima.freemarker.controller;
import com.itheima.freemarker.entity.Student;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Date;
@Controller
@RequestMapping("student")
public class StudentController {
@GetMapping("index")
public String index(Model model){
//1.純文本形式的參數
model.addAttribute("name", "Freemarker");
//2.實體類相關的參數
Student student = new Student();
student.setName("黑馬");
student.setAge(18);
model.addAttribute("stu", student);
return "01-index";
}
}
(6)在resources/templates下創建01-index.ftl模版文件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>首頁</title>
</head>
<body>
<b>普通文本 String 展示:</b><br/>
Hello ${name} <br>
<hr>
<b>對象Student中的數據展示:</b><br/>
姓名:${stu.name}<br/>
年齡:${stu.age}
<hr>
</body>
</html>
(7)測試
瀏覽器訪問 http://localhost:8881/student/index
效果如下
(1)注釋,即<#-- -->,介于其之間的內容會被freemarker忽略
<#--我是一個freemarker注釋-->
(2)插值(Interpolation):即 ${..} 部分,freemarker會用真實的值代替${..}
Hello ${name}
(3)FTL指令:和HTML標記類似,名字前加#予以區分,Freemarker會解析標簽中的表達式或邏輯。
<# >FTL指令</#>
(4)文本,僅文本信息,這些不是freemarker的注釋、插值、FTL指令的內容會被freemarker忽略解析,直接輸出內容。
<#--freemarker中的普通文本-->
我是一個普通的文本
if 指令即判斷指令,是常用的FTL指令,freemarker在解析時遇到if會進行判斷,條件為真則輸出if中間的內容,否則跳過內容不再輸出。
格式如下
<#if condition>
....
<#elseif condition2>
...
<#elseif condition3>
...
<#else>
...
</#if>
需求:根據年齡輸出所處的年齡段
童年:0歲—6歲(周歲,下同) 少年:7歲—17歲 青年:18歲—40歲 中年:41—65歲 老年:66歲以后
實例代碼:
(1)在01-index.ftl添加如下代碼
<#if stu.age <= 6>
童年
<#elseif stu.age <= 17>
少年
<#elseif stu.age <= 40>
青年
<#elseif stu.age <= 65>
中年
<#else>
老年
</#if>
(2)測試
瀏覽器訪問http://localhost:8881/student/index
效果如下
list指令時一個迭代輸出指令,用于迭代輸出數據模型中的集合
格式如下
<#list items as item>
${item_index + 1}------${item}-----<#if item_has_next>,</#if>
</#list>
迭代集合對象時,包括兩個特殊的循環變量: (1)item_index:當前變量的索引值。 (2)item_has_next:是否存在下一個對象
item_index 和 item_has_nex 中的item為<#list items as item> 中as后面的臨時變量
需求:遍歷學生集合,輸出序號,學生id,姓名,所處的年齡段,是否最后一條數據
(1)在StudentController中增加方法
@GetMapping("list")
public String list(Model model) throws ParseException {
List<Student> list = new ArrayList<>();
list.add(new Student(1001,"張飛",15, null, 1000.11F));
list.add(new Student(1002,"劉備",28, null, 5000.3F));
list.add(new Student(1003,"關羽",45, null, 9000.63F));
list.add(new Student(1004,"諸葛亮",62, null, 10000.99F));
list.add(new Student(1005,"成吉思汗",75, null, 16000.66F));
model.addAttribute("stus",list);
return "02-list";
}
(2)在resources/templates目錄下創建02-list.ftl模版
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>列表頁面</title>
<style>
table{
border-spacing: 0;/*把單元格間隙設置為0*/
border-collapse: collapse;/*設置單元格的邊框合并為1*/
}
td{
border:1px solid #ACBED1;
text-align: center;
}
</style>
</head>
<body>
<table>
<tr>
<td>序號</td>
<td>id</td>
<td>姓名</td>
<td>所處的年齡段</td>
<td>生日</td>
<td>錢包</td>
<td>是否最后一條數據</td>
</tr>
<#list stus as stu >
<tr>
<td>${stu_index + 1}</td>
<td>${stu.id}</td>
<td>${stu.name}</td>
<td>
<#if stu.age <= 6>
童年
<#elseif stu.age <= 17>
少年
<#elseif stu.age <= 40>
青年
<#elseif stu.age <= 65>
中年
<#else>
老年
</#if>
</td>
<td></td>
<td>${stu.money}</td>
<td>
<#if stu_has_next>
否
<#else>
是
</#if>
</td>
</tr>
</#list>
</table>
<hr>
</body>
</html>
(2)測試
瀏覽器訪問http://localhost:8881/student/list
效果如下
include指令的作用類似于JSP的包含指令,用于包含指定頁,include指令的語法格式如下
<#include filename [options]></#include>
(1)filename:該參數指定被包含的模板文件 (2)options:該參數可以省略,指定包含時的選項,包含encoding和parse兩個選項,encoding 指定包含頁面時所使用的解碼集,而parse指定被包含是否作為FTL文件來解析。如果省略了parse選項值,則該選項值默認是true
需求:"早上好,尊敬的 某某 用戶!" 這句話在很多頁面都有用到,請合理設計!
(1)在resources/templates目錄下創建00-head.ftl模版,內容如下
早上好,尊敬的 ${name} 用戶!
(2)在resources/templates目錄下創建03-include.ftl模版,使用include引入00-head.ftl模版,內容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>詳情頁</title>
</head>
<body>
<#include "00-head.ftl" />
<br>
歡迎來到黑馬程序員。
</body>
</html>
(3)在StudentController中增加方法
@GetMapping("include")
public String include(Model model) throws ParseException {
model.addAttribute("name", "黑馬");
return "03-include";
}
(4)測試
瀏覽器訪問http://localhost:8881/student/include
效果如下
它用于為該模板頁面創建或替換一個頂層變量
<#assign name = "zhangsan" />
(1)算數運算符
FreeMarker表達式中完全支持算術運算,FreeMarker支持的算術運算符包括:
(2)比較運算符
比較運算符注意
(3)邏輯運算符
邏輯運算符只能作用于布爾值,否則將產生錯誤 。
(1)缺失變量默認值使用 “!”
(2)判斷某變量是否存在使用 “??”
用法為:variable??,如果該變量存在,返回true,否則返回false
例:為防止stus為空報錯可以加上判斷如下:
<#if stus??>
<#list stus as stu>
......
</#list>
</#if>
內建函數語法格式: 變量+?+函數名稱
(1)求集合的大小
${集合名?size}
(2)日期格式化
顯示年月日: ${today?date} 顯示時分秒:${today?time} 顯示日期+時間:${today?datetime} 自定義格式化: ${today?string("yyyy年MM月")}
(3)內建函數c
model.addAttribute("point", 102920122);
point是數字型,使用${point}會顯示這個數字的值,每三位使用逗號分隔。
如果不想顯示為每三位分隔的數字,可以使用c函數將數字型轉成字符串輸出
${point?c}
(4)將json字符串轉成對象
一個例子:
其中用到了 assign標簽,assign的作用是定義一個變量。
<#assign text="{'bank':'工商銀行','account':'10101920201920212'}" />
<#assign data=text?eval />
開戶行:${data.bank} 賬號:${data.account}
(5)常見內建函數匯總
?html:html字符轉義
?cap_first: 字符串的第一個字母變為大寫形式
?lower_case :字符串的小寫形式
?upper_case :字符串的大寫形式
?trim:去掉字符串首尾的空格
?substring(from,to):截字符串 from是第一個字符的開始索引,to最后一個字符之后的位置索引,當to為空時,默認的是字符串的長度
?lenth: 取長度
?size: 序列中元素的個數
?int: 數字的整數部分(比如 -1.9?int 就是 -1)
?replace(param1,param2):字符串替換 param1是匹配的字符串 param2是將匹配的字符替換成指定字符
內建函數測試demo1
(1)在StudentController新增方法:
@GetMapping("innerFunc")
public String testInnerFunc(Model model) {
//1.1 小強對象模型數據
Student stu1 = new Student();
stu1.setName("小強");
stu1.setAge(18);
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
//1.2 小紅對象模型數據
Student stu2 = new Student();
stu2.setName("小紅");
stu2.setMoney(200.1f);
stu2.setAge(19);
//1.3 將兩個對象模型數據存放到List集合中
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
model.addAttribute("stus", stus);
// 2.1 添加日期
Date date = new Date();
model.addAttribute("today", date);
// 3.1 添加數值
model.addAttribute("point", 102920122);
return "04-innerFunc";
}
(2)在resources/templates目錄下創建04-innerFunc.ftl模版頁面:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>inner Function</title>
</head>
<body>
<b>獲得集合大小</b><br>
集合大小:${stus?size}
<hr>
<b>獲得日期</b><br>
顯示年月日: ${today?date} <br>
顯示時分秒:${today?time}<br>
顯示日期+時間:${today?datetime}<br>
自定義格式化: ${today?string("yyyy年MM月")}<br>
<hr>
<b>內建函數C</b><br>
沒有C函數顯示的數值:${point} <br>
有C函數顯示的數值:${point?c}
<hr>
<b>聲明變量assign</b><br>
<#assign text="{'bank':'工商銀行','account':'10101920201920212'}" />
<#assign data=text?eval />
開戶行:${data.bank} 賬號:${data.account}
<hr>
</body>
</html>
(3)測試
瀏覽器訪問http://localhost:8881/student/innerFunc
效果如下
內建函數測試demo2
需求:遍歷學生集合,顯示集合總條數,id不要逗號隔開,顯示學生的生日(只顯示年月日),錢包顯示整數并顯示單位元,用戶姓名做脫敏處理(如果是兩個字第二個字顯示為星號,例如張三顯示為張*,如果大于兩個字,中間字顯示為星號,例如成吉思汗顯示為成*汗,諸葛亮顯示為諸*亮)
(1)修改StudentController中的list方法,
@GetMapping("list")
public String list(Model model) throws ParseException {
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
List<Student> list = new ArrayList<>();
list.add(new Student(1001,"張三",15, dateFormat.parse("2007-10-01 10:00:00"), 1000.11F));
list.add(new Student(1002,"李四",28, dateFormat.parse("1994-10-01 10:00:00"), 5000.3F));
list.add(new Student(1003,"王五",45, dateFormat.parse("1977-10-01 10:00:00"), 9000.63F));
list.add(new Student(1004,"趙六",62, dateFormat.parse("1960-10-01 10:00:00"), 10000.99F));
list.add(new Student(1005,"孫七",75, dateFormat.parse("1947-10-01 10:00:00"), 16000.66F));
model.addAttribute("stus",list);
return "02-list";
}
(2)修改02-list.ftl模版
共${stus?size}條數據:輸出總條數
stu.id后面加?c:id不需要逗號分割
stu.birthday后面加?date:生日只輸出年月日
stu.money后面加?int:金額取整
姓名需要使用replace和substring函數處理
完整內容如下
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>列表頁面</title>
<style>
table{
border-spacing: 0;/*把單元格間隙設置為0*/
border-collapse: collapse;/*設置單元格的邊框合并為1*/
}
td{
border:1px solid #ACBED1;
text-align: center;
}
</style>
</head>
<body>
共${stus?size}條數據
<table>
<tr>
<td>序號</td>
<td>id</td>
<td>姓名</td>
<td>所處的年齡段</td>
<td>生日</td>
<td>錢包</td>
<td>是否最后一條數據</td>
</tr>
<#list stus as stu >
<tr>
<td>${stu_index + 1}</td>
<td>${stu.id?c}</td>
<td>
<#if stu.name?length=2>
${stu.name?replace(stu.name?substring(1), "*")}
<#else>
${stu.name?replace(stu.name?substring(1, stu.name?length-1), "*")}
</#if>
</td>
<td>
<#if stu.age <= 6>
童年
<#elseif stu.age <= 17>
少年
<#elseif stu.age <= 40>
青年
<#elseif stu.age <= 65>
中年
<#else>
老年
</#if>
</td>
<td>${stu.birthday?date}</td>
<td>${stu.money?int}元</td>
<td>
<#if stu_has_next>
否
<#else>
是
</#if>
</td>
</tr>
</#list>
</table>
<hr>
</body>
</html>
(3)測試
瀏覽器訪問http://localhost:8881/student/list
效果如下
(1)springboot整合freemarker靜態化文件用法
編寫springboot測試用例
package com.itheima.test;
import com.itheima.freemarker.FreemarkerDemoApplication;
import com.itheima.freemarker.entity.Student;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
@SpringBootTest(classes = FreemarkerDemoApplication.class)
@RunWith(SpringRunner.class)
public class FreemarkerTest {
//注入freemarker配置類
@Autowired
private Configuration configuration;
@Test
public void test() throws IOException, TemplateException {
Template template = configuration.getTemplate("04-innerFunc.ftl");
/**
* 靜態化并輸出到文件中 參數1:數據模型 參數2:文件輸出流
*/
template.process(getData(), new FileWriter("d:/list.html"));
/**
* 靜態化并輸出到字節輸出流中
*/
//StringWriter out = new StringWriter();
//template.process(getData(), out);
//System.out.println(out.toString());
}
private Map getData(){
Map<String,Object> map = new HashMap<>();
Student stu1 = new Student();
stu1.setName("小強");
stu1.setAge(18);
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
//小紅對象模型數據
Student stu2 = new Student();
stu2.setName("小紅");
stu2.setMoney(200.1f);
stu2.setAge(19);
//將兩個對象模型數據存放到List集合中
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
//向model中存放List集合數據
map.put("stus",stus);
//map數據
Map<String,Student> stuMap = new HashMap<>();
stuMap.put("stu1",stu1);
stuMap.put("stu2",stu2);
map.put("stuMap",stuMap);
//日期
map.put("today",new Date());
//長數值
map.put("point",38473897438743L);
return map;
}
}
(2)freemarker原生靜態化用法
package com.itheima.freemarker.test;
import com.itheima.freemarker.entity.Student;
import freemarker.cache.FileTemplateLoader;
import freemarker.cache.NullCacheStorage;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import freemarker.template.TemplateExceptionHandler;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;
public class FreemarkerTest {
public static void main(String[] args) throws IOException, TemplateException {
//創建配置類
Configuration CONFIGURATION = new Configuration(Configuration.VERSION_2_3_22);
//設置模版加載路徑
//ClassTemplateLoader方式:需要將模版放在FreemarkerTest類所在的包,加載模版時會從該包下加載
//CONFIGURATION.setTemplateLoader(new ClassTemplateLoader(FreemarkerTest.class,""));
String path = java.net.URLDecoder.decode(FreemarkerTest.class.getClassLoader().getResource("").getPath(),"utf-8");
//FileTemplateLoader方式:需要將模版放置在classpath目錄下 目錄有中文也可以
CONFIGURATION.setTemplateLoader(new FileTemplateLoader(new File(path)));
//設置編碼
CONFIGURATION.setDefaultEncoding("UTF-8");
//設置異常處理器
CONFIGURATION.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
//設置緩存方式
CONFIGURATION.setCacheStorage(NullCacheStorage.INSTANCE);
//加載模版
Template template = CONFIGURATION.getTemplate("templates/04-innerFunc.ftl");
/**
* 靜態化并輸出到文件中 參數1:數據模型 參數2:文件輸出流
*/
template.process(getModel(), new FileWriter("d:/list.html"));
/**
* 靜態化并輸出到字節輸出流中
*/
//StringWriter out = new StringWriter();
//template.process(getData(), out);
//System.out.println(out.toString());
}
public static Map getModel(){
Map map = new HashMap();
//1.1 小強對象模型數據
Student stu1 = new Student();
stu1.setName("小強");
stu1.setAge(18);
stu1.setMoney(1000.86f);
stu1.setBirthday(new Date());
//1.2 小紅對象模型數據
Student stu2 = new Student();
stu2.setName("小紅");
stu2.setMoney(200.1f);
stu2.setAge(19);
//1.3 將兩個對象模型數據存放到List集合中
List<Student> stus = new ArrayList<>();
stus.add(stu1);
stus.add(stu2);
map.put("stus", stus);
// 2.1 添加日期
Date date = new Date();
map.put("today", date);
// 3.1 添加數值
map.put("point", 102920122);
return map;
}
}
元數據(Metadata)是描述數據的數據。
數據庫元數據(DatabaseMetaData)就是指定義數據庫各類對象結構的數據。
在mysql中可以通過show關鍵字獲取相關的元數據
show status; 獲取數據庫的狀態
show databases; 列出所有數據庫
show tables; 列出所有表
show create database [數據庫名]; 獲取數據庫的定義
show create table [數據表名]; 獲取數據表的定義
show columns from <table_name>; 顯示表的結構
show index from <table_name>; 顯示表中有關索引和索引列的信息
show character set; 顯示可用的字符集以及其默認整理
show collation; 顯示每個字符集的整理
show variables; 列出數據庫中的參數定義值
也可以從 information_schema庫中獲取元數據,information_schema數據庫是MySQL自帶的信息數據庫,它提供了訪問數據庫元數據的方式。存著其他數據庫的信息。
select schema_name from information_schema.schemata; 列出所有的庫
select table_name FROM information_schema.tables; 列出所有的表
在代碼中可以由JDBC的Connection對象通過getMetaData方法獲取而來,主要封裝了是對數據庫本身的一些整體綜合信息,例如數據庫的產品名稱,數據庫的版本號,數據庫的URL,是否支持事務等等。
DatabaseMetaData的常用方法:
getDatabaseProductName:獲取數據庫的產品名稱
getDatabaseProductName:獲取數據庫的版本號
getUserName:獲取數據庫的用戶名
getURL:獲取數據庫連接的URL
getDriverName:獲取數據庫的驅動名稱
driverVersion:獲取數據庫的驅動版本號
isReadOnly:查看數據庫是否只允許讀操作
supportsTransactions:查看數據庫是否支持事務
(1)導入mysql依賴
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
(2)創建測試用例
package com.itheima.test;
import org.junit.Before;
import org.junit.Test;
import java.sql.*;
import java.util.Properties;
public class DataBaseMetaDataTest {
private Connection conn;
@Before
public void init() throws Exception {
Properties pro = new Properties();
pro.setProperty("user", "root");
pro.setProperty("password", "123456");
pro.put("useInformationSchema", "true");//獲取mysql表注釋
//pro.setProperty("remarksReporting","true");//獲取oracle表注釋
conn = DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/?useUnicode=true&characterEncoding=UTF8", pro);
}
}
(1)獲取數據庫元信息綜合信息
@Test
public void testDatabaseMetaData() throws SQLException {
//獲取數據庫元數據
DatabaseMetaData dbMetaData = conn.getMetaData();
//獲取數據庫產品名稱
String productName = dbMetaData.getDatabaseProductName();
System.out.println(productName);
//獲取數據庫版本號
String productVersion = dbMetaData.getDatabaseProductVersion();
System.out.println(productVersion);
//獲取數據庫用戶名
String userName = dbMetaData.getUserName();
System.out.println(userName);
//獲取數據庫連接URL
String userUrl = dbMetaData.getURL();
System.out.println(userUrl);
//獲取數據庫驅動
String driverName = dbMetaData.getDriverName();
System.out.println(driverName);
//獲取數據庫驅動版本號
String driverVersion = dbMetaData.getDriverVersion();
System.out.println(driverVersion);
//查看數據庫是否允許讀操作
boolean isReadOnly = dbMetaData.isReadOnly();
System.out.println(isReadOnly);
//查看數據庫是否支持事務操作
boolean supportsTransactions = dbMetaData.supportsTransactions();
System.out.println(supportsTransactions);
}
(2)獲取數據庫列表
@Test
public void testFindAllCatalogs() throws Exception {
//獲取元數據
DatabaseMetaData metaData = conn.getMetaData();
//獲取數據庫列表
ResultSet rs = metaData.getCatalogs();
//遍歷獲取所有數據庫表
while (rs.next()) {
//打印數據庫名稱
System.out.println(rs.getString(1));
}
//釋放資源
rs.close();
conn.close();
}
(3)獲取某數據庫中的所有表信息
@Test
public void testFindAllTable() throws Exception {
//獲取元數據
DatabaseMetaData metaData = conn.getMetaData();
//獲取所有的數據庫表信息
ResultSet rs = metaData.getTables("庫名", "%", "%", new String[]{"TABLE"});
//拼裝table
while (rs.next()) {
//所屬數據庫
System.out.println(rs.getString(1));
//所屬schema
System.out.println(rs.getString(2));
//表名
System.out.println(rs.getString(3));
//數據庫表類型
System.out.println(rs.getString(4));
//數據庫表備注
System.out.println(rs.getString(5));
System.out.println("--------------");
}
}
(4)獲取某張表所有的列信息
@Test
public void testFindAllColumns() throws Exception {
//獲取元數據
DatabaseMetaData metaData = conn.getMetaData();
//獲取所有的數據庫某張表所有列信息
ResultSet rs = metaData.getColumns("庫名", "%", "表名","%");
while(rs.next()) {
//表名
System.out.println(rs.getString("TABLE_NAME"));
//列名
System.out.println(rs.getString("COLUMN_NAME"));
//類型碼值
System.out.println(rs.getString("DATA_TYPE"));
//類型名稱
System.out.println(rs.getString("TYPE_NAME"));
//列的大小
System.out.println(rs.getString("COLUMN_SIZE"));
//小數部分位數,不適用的類型會返回null
System.out.println(rs.getString("DECIMAL_DIGITS"));
//是否允許使用null
System.out.println(rs.getString("NULLABLE"));
//列的注釋信息
System.out.println(rs.getString("REMARKS"));
//默認值
System.out.println(rs.getString("COLUMN_DEF"));
//是否自增
System.out.println(rs.getString("IS_AUTOINCREMENT"));
//表中的列的索引(從 1 開始
System.out.println(rs.getString("ORDINAL_POSITION"));
System.out.println("--------------");
}
}
參數元數據(ParameterMetaData):是由PreparedStatement對象通過getParameterMetaData方法獲取而 來,主要是針對PreparedStatement對象和其預編譯的SQL命令語句提供一些信息,ParameterMetaData能提供占位符參數的個數,獲取指定位置占位符的SQL類型等等 以下有一些關于ParameterMetaData的常用方法:
getParameterCount:獲取預編譯SQL語句中占位符參數的個數
@Test
public void testParameterMetaData() throws Exception {
String sql = "select * from health.t_checkgroup where id=? and code=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "7");
pstmt.setString(2, "0003");
//獲取ParameterMetaData對象
ParameterMetaData paramMetaData = pstmt.getParameterMetaData();
//獲取參數個數
int paramCount = paramMetaData.getParameterCount();
System.out.println(paramCount);
}
結果集元數據(ResultSetMetaData):是由ResultSet對象通過getMetaData方法獲取而來,主要是針對由數據庫執行的SQL腳本命令獲取的結果集對象ResultSet中提供的一些信息,比如結果集中的列數、指定列的名稱、指 定列的SQL類型等等,可以說這個是對于框架來說非常重要的一個對象。 以下有一些關于ResultSetMetaData的常用方法:
getColumnCount:獲取結果集中列項目的個數
getColumnType:獲取指定列的SQL類型對應于Java中Types類的字段
getColumnTypeName:獲取指定列的SQL類型
getClassName:獲取指定列SQL類型對應于Java中的類型(包名加類名
@Test
public void testResultSetMetaData() throws Exception {
String sql = "select * from health.t_checkgroup where id=?";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setString(1, "7");
//執行sql語句
ResultSet rs = pstmt.executeQuery();
//獲取ResultSetMetaData對象
ResultSetMetaData metaData = rs.getMetaData();
//獲取查詢字段數量
int columnCount = metaData.getColumnCount();
System.out.println("字段總數量:"+ columnCount);
for (int i = 1; i <= columnCount; i++) {
//獲取表名稱
System.out.println(metaData.getColumnName(i));
//獲取java類型
System.out.println(metaData.getColumnClassName(i));
//獲取sql類型
System.out.println(metaData.getColumnTypeName(i));
System.out.println("----------");
}
}
創建maven工程并導入以下依賴
<properties>
<java.version>11</java.version>
<!-- 項目源碼及編譯輸出的編碼 -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- 項目編譯JDK版本 -->
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.10</version>
</dependency>
</dependencies>
目錄結構如下
義字符串以在 HTML 中使用。
JavaScript
const escapeHTML = str =>
str.replace(
/[&<>'"]/g,
tag =>
({
'&': '&',
'<': '<',
'>': '>',
"'": ''',
'"': '"'
}[tag] || tag)
);
示例
escapeHTML('<a href="#">我 & 你</a>');
// '<a href="#">我 & 你</a>'
更多內容請訪問我的網站:https://www.icoderoad.com
*請認真填寫需求信息,我們會在24小時內與您取得聯系。