springboot+mybatis+bootstrap+thymeleaf前后端框架搭建

大家对thymeleaf框架比较不熟悉,其实thymeleaf是spring官方推荐的前台框架,今天就教大家如何使用thymeleaf整合到springboot中。
创建maven项目,pom.xml文件添加springboot需要的依赖包

  <dependencies>
  	
  	<dependency>
	        <groupId>org.springframework.bootgroupId>
	        <artifactId>spring-boot-starterartifactId>
	        <version>1.5.6.RELEASEversion>
	 dependency>
	 
	 <dependency>
			<groupId>org.springframework.bootgroupId>
			<artifactId>spring-boot-starter-tomcatartifactId>
			<version>1.5.6.RELEASEversion>
			<scope>providedscope>
		dependency>
	
	<dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-thymeleafartifactId>
		<version>1.5.6.RELEASEversion>
	 dependency>
	 
	 <dependency>
		<groupId>org.springframework.bootgroupId>
		<artifactId>spring-boot-starter-jdbcartifactId>
		<version>1.5.6.RELEASEversion>
	 dependency>
	 
	 <dependency>
    	<groupId>org.springframework.bootgroupId>
   		 <artifactId>spring-boot-devtoolsartifactId>
   		 <version>1.5.6.RELEASEversion>
   		 <optional>trueoptional>
	dependency>
	
	    <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <version>5.1.43version>
        dependency>
        
		<dependency>
			<groupId>com.zaxxergroupId>
			<artifactId>HikariCPartifactId>
        dependency>
        
	 <dependency>
	        <groupId>org.mybatis.spring.bootgroupId>
	        <artifactId>mybatis-spring-boot-starterartifactId>
	        <version>1.3.0version>
	 dependency>
  dependencies>

在src目录下建立main文件夹下,再建立java和resources文件夹,java文件夹存放java文件,resources文件夹存放springboot配置文件,thymeleaf模板文件存放于resources下templates,js和css等静态文件存放于resources下static,文件目录如下图
springboot+mybatis+bootstrap+thymeleaf前后端框架搭建_第1张图片
创建springboot配置文件名为application.yml或application.properties
yml文件格式如下

server:
  port: 8081
  context-path: /test
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/single-login??useUnicode=true&characterEncoding=UTF-8
    username: root
    password: ******
    driverClassName: com.mysql.jdbc.Driver
    type: com.zaxxer.hikari.HikariDataSource
  devtools:
    restart:
      enabled: true
mybatis:
  configLocation: classpath:mybatis-config.xml
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.po

properties文件格式如下
server.port=8081
server.context-path=/test
spring.datasource.url=jdbc:mysql://localhost:3306/single-login??useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.devtools.restart.enabled=true
mybatis.configLocation=classpath:mybatis-config.xml
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.po
在templates文件夹下创建html文件并引入thymeleaf和bootstrap,代码如下

<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8" />
    <title>首页title>
    <link th:href="@{bootstrap/css/bootstrap.min.css}" rel="stylesheet" />
    <link th:href="@{bootstrap/css/bootstrap-theme.min.css}" rel="stylesheet" />
head>

在html文件中能通过thymeleaf标签自由显示Control层的数据,就如同jsp页面一样,代码如下


<span th:text="${singlePerson.username}">span>

<li class="list-group-item" th:each="p:${people}">
                    <span th:text="${p.username}">span>
                    <span th:text="${p.password}">span>
li>

后台Control层使用org.springframework.ui.Model类封装数据,代码如下:

@RequestMapping("/list")
	public String list(Model model) {
     
		Myuser single = new Myuser();
		single.setUsername("admin");
		single.setPassword("123");
		List<Myuser> list = new ArrayList<Myuser>();
		list.add(new Myuser("scjg","465"));
		list.add(new Myuser("gjj","755"));
		list.add(new Myuser("yxf","1111"));
		model.addAttribute("singlePerson",single);
		model.addAttribute("people", list);
		return "list";
	}

通过springboot注解方式替代xml配置方式来配置,代码如下

@Configuration
@ComponentScan(basePackages = {
     "com.service.impl"})
public class AppConfig {
     
	@Bean(name="xxx")
	public Xxxx getXxx(){
     ...}
}

@Configuratio注解表示该Java类是Spring的xml文件配置类
@ComponentScan(basePackages = {“com.service.impl”})注解表示扫描com.service.impl包下的类名,
相当于xml中配置
@Bean注解相当于xml配置元素

接下来需要创建SpringBoot启动Java类,代码如下:

@SpringBootApplication
@MapperScan(basePackages = "com.dao")
@EnableTransactionManagement
public class Application extends SpringBootServletInitializer{
     
	public static void main(String[] args) {
     
		SpringApplication.run(Application.class, args); 
	}
}

@SpringBootApplication表示是Springboot启动类
@MapperScan(basePackages = “com.dao”)表示扫描mybatis层
@EnableTransactionManagement表示启用注解Transaction,可以写在service层任意类或者任意方法名上

最后启动主程序运行项目。
希望这些对初次接触SpringBoot和thymeleaf的开发人员有很大的帮助

你可能感兴趣的:(springboot,java,spring)