数据库
首页 > 数据库> > 保姆级搭建springboot+web+mysql+hibernate+thymeleaf+druid项目

保姆级搭建springboot+web+mysql+hibernate+thymeleaf+druid项目

作者:互联网

搭建springboot+web+mysql+hibernate+thymeleaf+druid项目

1.进入spring initializr:http://start.spring.io/

2.选择需要用到的配置:

3.下载后导入开发工具(我用的是eclipse)

4.右击项目选择BuildPath-Configure Build Path

5.删除报错的JRE包

6.重新添加一个JRE包

7.在左侧选择:Java Compiler

8:将JDK选择自己对应的版本

9:在启动类中的@SpringBootApplication后加入(exclude={DataSourceAutoConfiguration.class,HibernateJpaAutoConfiguration.class})

10:将application.properties改成application.yaml,在当中写以下配置(复制请把数据库账号密码、数据库表名改成自己的)

#配置端口
server:
  port: 8089

spring:

  #配置数据源
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true&useSSL=false
    username: root
    password: 123456
    type: com.alibaba.druid.pool.DruidDataSource
    
  #配置jpa持久层,hibernate
  jpa:  
    hibernate:
     ddl-auto: update
    show-sql: true
    database: mysql
    
  #配置thymeleaf模板引擎
  thymeleaf:
    cache: false
    mode: LEGACYHTML5
    encoding: UTF-8  
    prefix: classpath:/templates/
    suffix: .html


11.导入pom依赖(之前在spring initializr导入后还要添加2个依赖,我用的是druid数据连接池,processor是 一个注解处理器,在编译阶段干活的,一般在maven的声明都是 ,optional 为true )

<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.24</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>

12.写controller、dao、pojo、service层

  package com.springboot.huyitest.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.springboot.huyitest.pojo.userPojo;
import com.springboot.huyitest.service.userService;

@Controller
public class logoinController {
	@Autowired
	private userService userservice;
	
	@RequestMapping("/logoin")
	public String logoin(ModelMap map) {
		userPojo user = new userPojo();
		user.setId(1);
		user.setAge(3);
		user.setUserName("huyi");
		user.setPassword("12312");
		userservice.addUser(user);
		
		System.err.println(user.getPassword()+""+user.getUserName());
		map.put("users", user);
		return "logoin";
	}
}
  

13:在resources-templates下创建一个html页面

14.在 头中加入 lang="en" xmlns:th="http://www.thymeleaf.org",随便写一个thymeleaf测试

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
这里是登录页面
	<div th:each="user:${users}">
		<a th:text="${user.UserName}"></a>
	</div> 	 
</body>
</html>

15.在启动类上加一个注解@EnableAutoConfiguration与yaml文件中的show-sql: true对应实现数据库自动创表

16.右键Run As-Spring Boot App运行项目,这个项目就搭建完啦!!!*

请各位大佬点个关注再走呗~~

标签:web,hibernate,springboot,huyitest,user,import,com,public
来源: https://www.cnblogs.com/Huyi-1208/p/16339473.html