SpringBoot中的Thymeleaf

学习主题:SpringBoot
学习目标:

1. Thymeleaf语法详解-字符串操作

(1)th:text的作用是什么?

在页面中输出值

(2)th:value的作用是什么?

可以将一个值放入到input标签的value中

(3)什么是Thymeleaf的内置对象?

${#strings.isEmpty(key)}
判断字符串是否为空,如果为空返回 true,否则返回 false

${#strings.contains(msg,‘T’)}
判断字符串是否包含指定的子串,如果包含返回 true,否则返回 false

${#strings.startsWith(msg,‘a’)}
判断当前字符串是否以子串开头,如果是返回 true,否则返回 false

${#strings.endsWith(msg,‘a’)}
判断当前字符串是否以子串结尾,如果是返回 true,否则返回 false
${#strings.length(msg)}
返回字符串的长度

${#strings.indexOf(msg,‘h’)}
查找子串的位置,并返回该子串的下标,如果没找到则返回-1

${#strings.substring(msg,13)}
${#strings.substring(msg,13,15)}
截取子串,用户与 jdk String 类下 SubString 方法相同

${#strings.toUpperCase(msg)}
${#strings.toLowerCase(msg)}
字符串转大小写。

(4)内置对象的语法是什么?

注意语法:
1,调用内置对象一定要用#
2,大部分的内置对象都以 s 结尾 strings、numbers、dates

2.Thymeleaf语法详解-日期转换操作

(1)在Thymeleaf中使用哪个内置对象转换日期?

#dates

(2)常见的处理日期函数有哪些?

${#dates.format(key)}
格式化日期,默认的以浏览器默认语言为格式化标准

${#dates.format(key,‘yyy/MM/dd’)}
按照自定义的格式做日期转换

${#dates.year(key)}
${#dates.month(key)}
${#dates.day(key)}
year:取年
Month:取月
Day:取日

3.Thymeleaf语法详解-条件判断

(1)th:if的作用是什么?

做条件判断,单个条件判断

(2)th:switch的作用是什么?

多个条件判断

4.Thymeleaf语法详解-迭代遍历

(1)th:each的作用是什么?

迭代遍历

(2)th:each中可以获取哪些状态变量?

@RequestMapping("/show3")
public String showinfo3(Model model){
    List<Users> list=new ArrayList<>();
    list.add(new Users(1,"糊糊",18));
    list.add(new Users(2,"嘿嘿",19));
    list.add(new Users(3,"哈哈",20));
    model.addAttribute("list",list);
    return "index3";
}

<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
<th>Index</th>
<th>Count</th>
<th>Size</th>
<th>Even</th>
<th>Odd</th>
<th>First</th>
<th>lase</th>
</tr>
<tr th:each="u,var : ${list}">
<td th:text="${u.userid}"></td>
<td th:text="${u.username}"></td>
<td th:text="${u.userage}"></td>
<td th:text="${var.index}"></td>
<td th:text="${var.count}"></td>
<td th:text="${var.size}"></td>
<td th:text="${var.even}"></td>
<td th:text="${var.odd}"></td>
<td th:text="${var.first}"></td>
<td th:text="${var.last}"></td>
</tr>
</table>

@RequestMapping("/show4")
public String showInfo4(Model model){
Map<String, Users> map = new HashMap<>();
map.put("u1", new Users(1,"张三",20));
map.put("u2", new Users(2,"李四",22));
map.put("u3", new Users(3,"王五",24));
model.addAttribute("map", map);
return "index4";
}
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="maps : ${map}">
<td th:text="${maps}"></td>
</tr>
</table>
<th/>
<table border="1">
<tr>
<th>ID</th>
<th>Name</th>
<th>Age</th>
</tr>
<tr th:each="maps : ${map}">
<td th:each="entry:${maps}"
th:text="${entry.value.userid}" ></td>
<td th:each="entry:${maps}"
th:text="${entry.value.username}"></td>
<td th:each="entry:${maps}"
th:text="${entry.value.userage}"></td>
</tr>
</table>

5.Thymeleaf语法详解-获取作用域对象中的数据

(1)在Thymeleaf中如何获取HttpServletRequest对象中的值?

request.setAttribute("req","HttpServletRequest");
Request:<span th:text="${#httpServletRequest.getAttribute('req')}"></span>

(2)在Thymeleaf中如何获取HttpSession中的值?

request.getSession().setAttribute("sess","HttpSession");
HttpSession;<span th:text="${session.sess}"></span><br/>

(3)在Thymeleaf中如何获取ServletContext中的值?

request.getSession().getServletContext().setAttribute("app","Application");
Application:<span th:text="${application.app}"></span>

6.Thymeleaf语法详解-URL表达式

(1)URL表达式的语法是什么?

@{}

(2)在URL表达式中可以给定几种URL格式?

绝对路径:
绝对路径

相对路径:
1、相对于当前项目的根,相对于项目的上下文的相对路径
相对路径

2、相对于服务器路径的根
相对于服务器的根

(3)如何在URL表达式中传递参数?

相对路径-传参

(4)如何在URL表达式中通过RESTful风格传递参数?

相 对 路 径 - 传 参-restful

7.Spring Boot整合MyBatis-创建项目

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

<!--指定父工程, 声明当前工程是一个SpringBoot工程-->
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.9.RELEASE</version>
</parent>

<dependencies>
    <!--添加依赖, 依赖web启动器, 用于加入web工程需要的各种依赖-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </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>

    <!--lombok-->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>

</dependencies>

(2)Spring Boot整合MyBatis时如何在全局配置文件中配置数据源?

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/ssm
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=com.bjsxt.pojo

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

(1)@MapperScan(" ")注解的作用是什么?

扫描mapper包下的mapper接口

9.SpringBoot整合MyBatis完成用户查询

(1)在项目中编写一个查询所有用户的案例。

public interface UserMapper {
       List<Users> selectAll();
   }
<mapper namespace="com.bjsxt.mapper.UserMapper">
       <select id="selectAll" resultType="users">
        select id,name,age from users
    </select>
    </mapper>
public interface UserService {
        List<Users> findAll();
   }

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public List<Users> findAll() {
        List<Users> list = userMapper.selectAll();
        return list;
    }

   }
/**
 * 查询所有
 */
@RequestMapping("/findAll")
public String findUsersAll(Model model){
    List<Users> list = userService.findAll();
    model.addAttribute("list",list);
    return "show";
}
<body>
<table border="1">
    <tr>
        <th>用户ID</th>
        <th>用户姓名</th>
        <th>用户年龄</th>
        <th>操作</th>
    </tr>
    <tr th:each="user : ${list}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.age}"></td>
        <td>
            <a th:href="@{/findById(id=${user.id})}">修改用户</a>
            <a th:href="@{/delete(id=${user.id})}">删除用户</a>
        </td>
    </tr>
</table>
</body>

10.SpringBoot整合MyBatis完成用户修改-数据回显

(1)在项目中编写一个预更新查询用户的案例。

public interface UserMapper {
       Users selectById(Integer id);
    void updateUser(Users users);
    }	
<mapper namespace="com.bjsxt.mapper.UserMapper">
       <select id="selectById" resultType="users">
        select id,name,age from users where id=#{0}
    </select>
      </mapper>	
public interface UserService {
       Users findById(Integer id);
       }	

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Override
    public Users findById(Integer id) {
        Users user = userMapper.selectById(id);
        return user;
    }
    }	
/**
 * 根据id查询数据
 */
@RequestMapping("/findById")
public String findById(Model model,Integer id){
    Users user = userService.findById(id);
    model.addAttribute("user",user);
    return "update";
}
	
<body>
    <form th:action="@{/updateUser}" method="post"  >
        <input type="hidden" name="id" th:field="${user.id}"/>
        用户姓名:<input type="text" name="name" th:field="${user.name}"/>
        用户年龄:<input type="text" name="age" th:field="${user.age}"/>
        <input type="submit" value="修改"/>
    </form>
</body>	

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

(1)在项目中编写一个更新用户的案例。

public interface UserMapper {
        void updateUser(Users users);
    }	
<mapper namespace="com.bjsxt.mapper.UserMapper">
          <update id="updateUser" parameterType="users">
        update users set name=#{name},age=#{age} where id=#{id}
    </update>
   </mapper>	
public interface UserService {
    void updateUsers(Users users);
   }	

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

       @Override
    public void updateUsers(Users users) {
        userMapper.updateUser(users);
    }
}	
/**
 * 更新数据
 */
@RequestMapping("/updateUser")
public String update(Users users){
    //System.out.println(users+"---------------------");
    userService.updateUsers(users);
    return "redirect:/findAll";
}	
<body>
    <form th:action="@{/updateUser}" method="post"  >
        <input type="hidden" name="id" th:field="${user.id}"/>
        用户姓名:<input type="text" name="name" th:field="${user.name}"/>
        用户年龄:<input type="text" name="age" th:field="${user.age}"/>
        <input type="submit" value="修改"/>
    </form>
</body>
</body>	

12.SpringBoot整合MyBatis完成用户删除

(1)在项目中编写一个删除用户的案例。

public interface UserMapper {
       void deleteById(Integer id);
}	
<mapper namespace="com.bjsxt.mapper.UserMapper">
       <delete id="deleteById" parameterType="int">
        delete from users where id=#{id}
    </delete>
</mapper>	
public interface UserService {
       void deleteUserById(Integer id);
}	

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

        @Override
    public void deleteUserById(Integer id) {
        userMapper.deleteById(id);
    }
}	
/**
 * 删除数据
 */
@RequestMapping("/delete")
public String delete(Integer id){
    userService.deleteUserById(id);
    return "redirect:/findAll";
}
	
<body>
<table border="1">
    <tr>
        <th>用户ID</th>
        <th>用户姓名</th>
        <th>用户年龄</th>
        <th>操作</th>
    </tr>
    <tr th:each="user : ${list}">
        <td th:text="${user.id}"></td>
        <td th:text="${user.name}"></td>
        <td th:text="${user.age}"></td>
        <td>
            <a th:href="@{/findById(id=${user.id})}">修改用户</a>
            <a th:href="@{/delete(id=${user.id})}">删除用户</a>
        </td>
    </tr>
</table>
</body>	
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值