一、 在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);
}