Thymeleaf的學習與項目整合

1. Thymeleaf語法詳解-字符串操作

(1) th:text的作用是什麼?
在頁面中輸出值
(2) th:value的作用是什麼?
可以將一個值放入input標籤的value中
(3) 什麼是Thymeleaf的內置對象?
模板引擎提供一組功能性內置對象,可以在模板中直接使用這些對象的功能方法
(4) 內置對象的語法是什麼?
調用內置對象一定要用#
大部分的內置對象都以 s 結尾 strings、numbers、dates

2. Thymeleaf語法詳解-日期轉換操作

(1) 在Thymeleaf中使用哪個內置對象轉換日期?
#dates
(2) 常見的處理日期函數有哪些?
format:轉換時間輸出格式
year:獲取年
month:獲取月
day:獲取日

3. Thymeleaf語法詳解-條件判斷

(1) th:if的作用是什麼?
用作條件判斷
(2) th:switch的作用是什麼?
用作等值條件判斷

4. Thymeleaf語法詳解-迭代遍歷

(1) th:each的作用是什麼?
用作循環,迭代遍歷集合
(2) th:each中可以獲取哪些狀態變量?
1,index:當前迭代器的索引 從 0 開始
2,count:當前迭代對象的計數 從 1 開始
3,size:被迭代對象的長度
4,even/odd:布爾值,當前循環是否是偶數/奇數 從 0 開始
5,first:布爾值,當前循環的是否是第一條,如果是返回 true 否則返回 false
6,last:布爾值,當前循環的是否是最後一條,如果是則返回 true 否則返回 false

5. Thymeleaf語法詳解-獲取作用域對象中的數據

(1) 在Thymeleaf中如何獲取HttpServletRequest對象中的值?
${#httpServletRequest.getAttribute(“key”)}
(2) 在Thymeleaf中如何獲取HttpSession中的值?
${session.key}
(3) 在Thymeleaf中如何獲取ServletContext中的值?
${application.key}

6. Thymeleaf語法詳解-URL表達式

(1) URL表達式的語法是什麼?
@{}
(2) 在URL表達式中可以給定幾種URL格式?
th:href
th:src
(3) 如何在URL表達式中傳遞參數?
有兩種方式,一種爲@{/url(參數1=值1,參數2=值2…)}
第二種方式:@{/url/{參數1}/{參數2}/…}
(4) 如何在URL表達式中通過RESTful風格傳遞參數?
@{/url/{參數1}/{參數2}/…}

7. Spring Boot整合MyBatis-創建項目

(1) Spring MVC+Spring Boot+MyBatis整合需要添加哪些座標?

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
</parent>

<dependencies>
    <!-- springBoot的啓動器 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <!-- thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <!-- thymeleaf熱部署 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
        <scope>true</scope>
    </dependency>
    <!-- Mybatis 啓動器 -->
    <dependency>
        <groupId>org.mybatis.spring.boot</groupId>
        <artifactId>mybatis-spring-boot-starter</artifactId>
        <version>1.1.1</version>
    </dependency>
    <!-- mysql 數據庫驅動 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <!-- druid 數據庫連接池 -->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.1.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>5.1.10.RELEASE</version>
        <scope>compile</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot</artifactId>
    </dependency>

    <!--JDBC-->
    <!--<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>-->
</dependencies>

<build>
    <resources>
        <resource>
            <directory>src/main/java</directory>
            <includes>
                <include>**/*.properties</include>
                <include>**/*.xml</include>
            </includes>
            <filtering>false</filtering>
        </resource>
    </resources>
</build>

(2) Spring Boot整合MyBatis時如何在全局配置文件中配置數據源?

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm?useUnicode=true&characterEncoding=UTF-8&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root

(3) Spring Boot整合MyBatis時如何在全局配置文件中配置數據庫連接池?

spring.datasource.type=com.alibaba.druid.pool.DruidDataSource 

(4) Spring Boot整合MyBatis時如何在全局配置文件中配置MyBatis的包別名?

mybatis.type-aliases-package=an.cxy.pojo

8. SpringBoot整合MyBatis完成添加用戶

(1) @MapperScan(" ")註解的作用是什麼?
用來掃描mybatis中的mapper文件

9. SpringBoot整合MyBatis完成用戶查詢

(1) 在項目中編寫一個查詢所有用戶的案例。

UserMapper.xml:
<select id="findAll" resultType="users">
    select * from users
</select>

UserMapper接口:
List<Users> findAll();

UserService接口:
List<Users> findAll();

UserService實現類:
@Override
public List<Users> findAll() {
    return userMapper.findAll();
}

UserController:
@RequestMapping("/findAll")
public String findAll(Model model){
    List<Users> list = userService.findAll();
    model.addAttribute("list",list);
    return "find";
}

10. SpringBoot整合MyBatis完成用戶修改-數據回顯

(1) 在項目中編寫一個預更新查詢用戶的案例。

UserMapper.xml:
<select id="findOne" resultType="users" parameterType="int">
    select * from users where userid=#{0}
</select>

UserMapper接口:
Users findOne(int userid);

UserService接口:
Users findOne(int userid);

UserService實現類:
@Override
public Users findOne(int userid) {
    return userMapper.findOne(userid);
}

UserController:
@RequestMapping("/findOne")
public String findOne(int userid,Model model){
    Users user = userService.findOne(userid);
    model.addAttribute("user",user);
    return "edit";
}

11. SpringBoot整合MyBatis完成用戶修改-更新用戶

(1) 在項目中編寫一個更新用戶的案例。

UserMapper.xml:
<update id="editUser" parameterType="users">
    update users set username=#{username},userage=#{userage} where userid=#{userid}
</update>

UserMapper接口:
int editUser(Users users);

UserService接口:
void editUser(Users users);

UserService實現類:
@Override
public void editUser(Users users) {
    userMapper.editUser(users);
}

UserController:
@RequestMapping("/editUser")
public String editUser(Users users){
    userService.editUser(users);
    return "redirect:/user/findAll";
}

12. SpringBoot整合MyBatis完成用戶刪除

(1) 在項目中編寫一個刪除用戶的案例。

UserMapper.xml:
<delete id="deleteUser" parameterType="int">
    delete from users where userid=#{0}
</delete>

UserMapper接口:
int deleteUser(int userid);

UserService接口:
void deleteUser(int userid);

UserService實現類:
@Override
public void deleteUser(int userid) {
    userMapper.deleteUser(userid);
}

UserController:
@RequestMapping("/deleteUser")
public String deleteUser(int userid){
    userService.deleteUser(userid);
    return "redirect:/user/findAll";
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章