第一个springboot与Thymeleaf项目

创建springboot项目

(1)进入spring官网点击install下载项目包,使用idea打开
(2)直接使用idea创建springboot项目

springboot中,可以使用以下方式处理静态资源:

  • webjar (localhost:8080/webjas/)
  • public、static、/**、resource(这里可以直接映射到localhost:8080/)
    资源中优先级resource > static > public
  • public 存放公共资源
  • static放静态的资源比如图片
 //在templates目录下的所有页面,只能通过controller来跳转!!  相当于原来的webINF目录!
@Controller//表示有一个静态页面,会跳到一个页面,这个页面要放到templates下!
            //这个需要模版引擎的支持

thymeleaf模板

怎么引入呢,对于springboot来说,什么事情不都是一个start的事情嘛,我们去在项目中引入一下。有三个重要网址:
Thymeleaf 官网:https://www.thymeleaf.org/
Thymeleaf 在Github 的主页:https://github.com/thymeleaf/thymeleaf
Spring官方文档:找到我们对应的版本
https://docs.spring.io/spring-boot/docs/2.2.5.RELEASE/reference/htmlsingle/#using-boot-starter
找到对应的pom依赖:可以适当点进源码看下本来的包!

<!--thymeleaf-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Maven会自动下载jar包,我们可以去看下下载的东西;

模板使用步骤:

  • 首先导入thymeleaf依赖—>github上找,可以查看
    我们将html页面放在templates下
  • 在html页面导入约束:需要在html文件中导入命名空间的约束,方便提示。
    可以去官方文档的#3中看一下命名空间拿来过来:https://www.thymeleaf.org/
 xmlns:th="http://www.thymeleaf.org"

thymeleaf相关操作


编码:(环境搭建+业务代码开发)

- 环境搭建:

1. 创建一个springboot项目,项目名字:ems-thymeleaf
2. 修改配置文件为application.yml,pom.xml换成版本2.5.0,更换版本后启动看会不会出问题。
3. 修改端口号:9999,项目名:ems-thymeleaf
4. 整合thymeleaf使用
  • 引入依赖
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
  • 配置文件中指定thymeleaf相关配置
server:
  port: 9999
  servlet:
    context-path: /ems-thymeleaf

#清除thymeleaf中的缓存
spring:
  thymeleaf:
    cache: false
    prefix: classpath:/templates/
    suffix: .html
  • 编写控制器测试
5. springboot整合mybatis
  • 引入mybatis和mysql依赖,配置数据源类型,数据驱动
 <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.0</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.6</version>
        </dependency>
  • 配置文件中加入thymeleaf,resource,mybatis,logging相关配置
#    spring 数据源配置
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/demo?useUnicode=true&characterEncoding=utf-8
    username: root
    password: 123456

    # mybatis 本地位置
mybatis:
  mapper-locations: classpath:com/baizhi/mapper/*.xml
  type-aliases-package: com.baizhi.entity
#  日志配置
logging:
  level:
    root: info
    com.baizhi: debug
6. 导入项目页面(这里是大致的流程而已)
  • static :存放静态资源(css、js、img)
  • thymeleaf:存放html页面,这里要注意引入thymeleaf的空间约束以及修改成th:标签,还有注意引入的css和js的路径要修改正确!!
  • 引用模板后获取action到controller中得到相应请求
  • controller层调用业务
  • 实现接口方法
  • 到mapper接口中实现方法
  • mapper.xml文件中首先增加管理的mapper所在路径和名称
  • “id”属性填要实现的对应方法,参数类型与返回值两个属性也要加上
  • 然后编写要执行的select语句
    在这里插入图片描述
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用Spring Data JPA来访问两个数据库表,然后在Thymeleaf模板中分别展示它们的数据。 首先,在你的Spring Boot应用中,你需要配置两个数据源和实体管理器工厂。这个可以通过在application.properties文件中添加以下配置来完成: ``` # 第一个数据源 spring.datasource.url=jdbc:mysql://localhost:3306/db1 spring.datasource.username=root spring.datasource.password=password spring.datasource.driver-class-name=com.mysql.jdbc.Driver # 第二个数据源 datasource2.url=jdbc:mysql://localhost:3306/db2 datasource2.username=root datasource2.password=password datasource2.driver-class-name=com.mysql.jdbc.Driver # 配置JPA spring.jpa.hibernate.ddl-auto=create spring.jpa.show-sql=true spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect spring.jpa.properties.hibernate.hbm2ddl.auto=create spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true ``` 然后,你需要在你的应用中定义两个数据源的JdbcTemplate实例,以便你可以使用它们来执行查询。这可以通过像这样在你的应用中定义两个JdbcTemplate实例来完成: ``` @Configuration public class DataSourceConfig { @Bean(name = "jdbcTemplate1") public JdbcTemplate jdbcTemplate1(DataSource dataSource) { return new JdbcTemplate(dataSource); } @Bean(name = "jdbcTemplate2") public JdbcTemplate jdbcTemplate2(@Qualifier("dataSource2") DataSource dataSource) { return new JdbcTemplate(dataSource); } } ``` 接下来,你需要为每个数据库表定义一个实体类,并使用Spring Data JPA创建一个存储库来访问它们。例如,对于一个为“table1”的表,你可以定义一个为“Table1”的实体类和一个为“Table1Repository”的存储库: ``` @Entity @Table(name = "table1") public class Table1 { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; // 省略getter和setter } @Repository public interface Table1Repository extends JpaRepository<Table1, Long> { } ``` 然后,在你的控制器中,你可以注入这两个存储库,并使用它们来获取两个数据库表的数据。例如: ``` @Controller public class MyController { @Autowired private Table1Repository table1Repository; @Autowired @Qualifier("jdbcTemplate2") private JdbcTemplate jdbcTemplate2; @GetMapping("/") public ModelAndView index() { List<Table1> table1List = table1Repository.findAll(); List<Map<String, Object>> table2List = jdbcTemplate2.queryForList("SELECT * FROM table2"); ModelAndView modelAndView = new ModelAndView("index"); modelAndView.addObject("table1List", table1List); modelAndView.addObject("table2List", table2List); return modelAndView; } } ``` 最后,在Thymeleaf模板中,你可以使用类似以下的代码来展示这两个数据库表的数据: ``` <!-- 展示第一个数据库表的数据 --> <table> <thead> <tr> <th>id</th> <th>name</th> </tr> </thead> <tbody> <tr th:each="table1 : ${table1List}"> <td th:text="${table1.id}"></td> <td th:text="${table1.name}"></td> </tr> </tbody> </table> <!-- 展示第二个数据库表的数据 --> <table> <thead> <tr> <th>id</th> <th>name</th> <th>age</th> </tr> </thead> <tbody> <tr th:each="table2 : ${table2List}"> <td th:text="${table2.id}"></td> <td th:text="${table2.name}"></td> <td th:text="${table2.age}"></td> </tr> </tbody> </table> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值