一:常用th:標籤簡介:
我們再用spring-boot框架的時候,可能不會採用我們以往用的jsp頁面的方式,而是通過採用thymeleaf渲染的方式進行
前後臺數據的交互。常用的標籤有
th:href,用法:th:href="/brand/selectbrand",(用來指明要要跳轉的鏈接)
th:object,用法:th:object="${brand}",(用來接受後臺傳過來的對象)
樣例:
<form class="form form-horizontal" id="form-admin-add" action="#" th:action="@{/brand/updatebyid}" th:object="${brand}">
th:field,用法:th:field="*{name}",(用來綁定後臺對象和表單數據)
th:value,用法:th:value="${brand.name}",(用對象對name值替換value屬性)
總結:th:field
樣例:
<form class="form form-horizontal" id="form-admin-add" action="#" th:action="@{/brand/updatebyid}" th:object="${brand}">
<input type="text" class="input-text" value="" th:value="*{id}" name="id" />
<input type="text" class="input-text" value="" th:field="*{code}" placeholder="" id="code"/>
th:field和th:value的小結:
thymeleaf裏的th:field等同於th:name和th:value,瀏覽器在解析th:field的時候,會解析成name="${th:field}"的值。
然後後臺就可以接收到從前臺傳過來的值。而th:value可以接受到後臺的的值,後臺則可以根據name獲取到前臺的值。
th:field和th:value都有兩種從後臺接受值的方式:1、${obj.name} 2、*{name}。需要注意的是,th:field需要有th:object
指定前臺傳過來的參數,否則瀏覽器在解析的時候會出現錯誤。
th:each,用法:th:each="brand:${pageInfo.list}",(後臺把對象放在了pageinfo的分頁插件的List裏,然後用th:each遍
歷後臺傳過來的對象)。
樣例:
<tr class="text-c" th:each="order:${pageInfo.list}">
<td th:text="${order.id}"></td>
<td th:text="${order.dicMethodDescription}"></td>
<td th:text="${order.productBrandName}"></td>
<td th:text="${order.productModelName}"></td>
th:if,用法:th:if="${pageInfo.list.size() == 0}",(判斷傳過來的對象是否爲空,如果爲空,則執行...)
th:unless,用法:th:unless="${pageInfo.list.size() == 0}"(判斷傳過來的對象是否爲空,如果不爲空,則執行...)
標籤只有在th:if中條件成立時才顯示,th:unless於th:if恰好相反,只有表達式中的條件不成立,纔會顯示其內容。
樣例:
<td th:if="${order.productStateName}==部分退款" th:text="交易成功" class="validate"></td>
<td th:unless="${order.productStateName}==部分退款" th:text="${order.productStateName}"></td>
三:thymeleaf實現動態訪問controller:
我們一般鏈接地址都是寫死了的,這樣其實間接的降低了系統的靈活性,我們可以在th:href的鏈接裏添加參數,這樣
就可以動態的訪問controller,舉個栗子:
//static頁面層:
<a title="編輯信息" href="javascript:;" th:οnclick="'javascript:brand_edit(\'編輯品牌信息\',\'/brand'+@{/updatebyid}+'/'+${brand.id}+'\',800,400)'" class="ml-5" style="text-decoration:none"><i class="Hui-iconfont"></i></a>
//controller層:
//根據品牌名更改品牌的名稱和編號信息
@RequestMapping(value = "/updatebyid/{id}")
public String updatebyidBefore(Model model, @ModelAttribute(value = "brand") ProductBrand brand,
@PathVariable("id") Long id) {
// 根據ID查詢用戶 和對應的角色
brand = operatebrand.selectByPrimaryKey(id);
// 查詢對應用戶的角色
model.addAttribute("brand", brand);
return "maintenance-operate/editor-brand";
}
四:對數據庫的數據靈活篩選:
一般我們在訪問數據庫的時候,可能在數據庫中查詢的時候寫出我們固定需要的值,比如我們想要查的字段需要在某一
範圍內,例如需要查出狀態表的狀態在(1,3,5,7)狀態裏。這些狀態可以在狀態的字典表裏查出。
你可能想到會這樣寫:select id ,name,code from table where state in(1,3,5,7)。但是別人也想查對應的字段,但是篩選
的狀態不同,比如他篩選的是2,4,6,8。這時候他可能又需要寫一套select語句。則這樣也失去的代碼靈活性。
我們可以傳一個狀態數組進去,這時候就會根據狀態數組的值來進行篩選。我們來看看實現。
ServiceImpl層:
List<OrderInfoForm> result = null;
result=orderMapper.selectOrdersByStatesSelective(order, new Integer[] {3,4,6,7,8});
dao層:
List<OrderInfoForm> selectOrdersByStatesSelective(@Param(value="order")Order order,
@Param(value="states")Integer[] states);
mapper層:
<select id="selectOrdersByStatesSelective" resultMap="AllResultMap" >
select
<include refid="All_Column_List" />
from order_list
LEFT JOIN product_method ON product_method.`code` = order_list.purchase_method
LEFT JOIN product_color ON product_color.`code` = order_list.color
LEFT JOIN product_guarantee ON product_guarantee.`code` = order_list.guarantee
LEFT JOIN product_info ON order_list.product_id = product_info.id
LEFT JOIN product_model ON product_info.model = product_model.`code`
LEFT JOIN product_standard ON product_info.standard = product_standard.`code`
LEFT JOIN product_state ON product_state.`code` = order_list.order_state
LEFT JOIN product_apperance ON product_apperance.`code` = order_list.apperance
LEFT JOIN product_brand ON product_brand.`code` = product_info.brand
<where>
<if test="order.orderNum != null " >
order_num like "%"#{order.orderNum,jdbcType=VARCHAR}"%"
</if>
<if test="order.operator != null " >
and operator like "%"#{order.operator,jdbcType=VARCHAR}"%"
</if>
<if test="order.purchaseTime != null" >
and purchase_time = #{order.purchaseTime,jdbcType=DATE}
</if>
<if test="order.orderState != null" >
and order_state = #{order.orderState,jdbcType=VARCHAR}
</if>
<if test="order.serialNum != null" >
and serial_num like "%"#{order.serialNum,jdbcType=VARCHAR}"%"
</if>
<if test="states != null and states.length >0">
<foreach collection="states" item="state" separator="," open=" and order_state in (" close=")">
#{state,jdbcType=BIGINT}
</foreach>
</if>
</where>
</select>
注意最後的test的條件:
解釋一下各個的參數:
1、foreach collection="傳過來的狀態數組名稱"
2、item="state"用於遍歷每個狀態
3、separator=",":用於分割各個狀態
4、open=" 用於在對應位置添加雙引號
5、and order_state in (" close=")"用於在對應結束位置添加雙引號