在springboot集成前端页面的thymleaf方法&SQLite的简单使用

一、 在springboot集成前端页面的thymleaf方法

有时候想验证自己的某些想法时需要写一些前端界面,但是又不想写vue那么重的框架。
怎么办,采用thymleaf呀!
放在springboot下面的的static下面的文件是可以直接访问的
例如:目录结构
在这里插入图片描述
这里的resources/static/index/css/GL.css
这种static下的静态文件,要访问GL.cc的话只需要http://localhost:10000/index/css/GL.css
就可以了。

如果访问templates下的文件就要用thymleaf
在这里插入图片描述
第一步导包:

  <!--模板引擎-->
        <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,依赖不会传递,该项目依赖devtools;
			之后依赖boot项目的项目如果想要使用devtools,需要重新引入 -->
<!--            <optional>true</optional>-->
        </dependency>

此时可以直接通过ip+端口访问到templates下的index.html
例如:localhost:10000 就可以访问到index.html
如果要通过代码访问就要配置controller,比如要传参数或者不叫index.html就必须通过视图解析器。

  /**
     * 要往前端带数据就放进Model对象里面
     * @param model
     * @return
     */
    @GetMapping({"/","/index.html"})
    public String IndexPage(Model model){
        //todo 查询所有一级分类
        List<CategoryEntity> categoryEntities = categoryService.getLevel1Categorys();
        /**
         * 在yml里面可配置前缀和后缀。不配就有默认的
         * 前缀:classpath:/templates/
         * 后缀:.html
         */
        model.addAttribute("categorys",categoryEntities);
        return "index";
    }

这样就可以访问到templates 下的index.html
这里的templates和.html是默认的也可以根据自己需要去yml里配置。index是 返回的。
这里返回给前端页面的参数绑定方法是通过Model。

东西虽小,积跬步至千里,积小流成江海

二、springboot集成SQLite实现增删改查

1.导包

		<!--SQLite的包-->
  		<dependency>
            <groupId>org.xerial</groupId>
            <artifactId>sqlite-jdbc</artifactId>
            <version>3.8.9.1</version>
        </dependency>
        <!-- MyBatis-Plus -->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.3.4</version>
        </dependency>

2.建一个数据文件

我是在项目resources下新建一个文件夹db,在db里建一个agent.db
resources/db/agent.db

3.配置连接(注意这里的路径)

server:
  port: 10086
spring:
  application:
    name: docker-file
  datasource:
  	# 重点注意这里的路径
    url: jdbc:sqlite:src/main/resources/db/agent.db
    driver-class-name: org.sqlite.JDBC
  jpa:
    database-platform: org.hibernate.dialect.SQLiteDialect
mybatis-plus:
  mapper-locations: classpath:/mappers/**/*.xml

4.简单的增删改查

	//增
  @Override
    public Employee saveEmployee() {

        Employee e = new Employee();
        e.setId(new Random().nextInt(1000));
        e.setAddress("北京");
        e.setGender("女");
        e.setName("美人");
        //employeeMapper.insertEmployee(e);
        employeeMapper.insert(e);
        return e;
    }
	//查
    @Override
    public List<Employee> queryEmployeeList() {

        List<Employee> employees = employeeMapper.selectList(null);

        return employees;
    }
	//删
    @Override
    public void remove() {
        employeeMapper.deleteById(299);
    }
	//改
    @Override
    public void update() {
        Employee e = new Employee();
        e.setId(96);
        e.setAddress("北京");
        e.setGender("女");
        e.setName("美人女");

        employeeMapper.updateById(e);
    }
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

神雕大侠mu

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值