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

2022/6/3 6:20:23

本文主要是介绍保姆级搭建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层

  • pojo

    package com.springboot.huyitest.pojo;
    
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Table;
    import javax.persistence.Id;
    
    @Entity
    @Table(name = "USER")
    public class userPojo {
    	@Id
    	@GeneratedValue(strategy = GenerationType.IDENTITY)
    	private Integer Id;
    	private String UserName;
    	private String password;
    	private Integer age;
    	
    	public Integer getId() {
    		return Id;
    	}
    	public void setId(Integer id) {
    		Id = id;
    	}
    	public Integer getAge() {
    		return age;
    	}
    	public void setAge(Integer age) {
    		this.age = age;
    	}
    	public String getUserName() {
    		return UserName;
    	}
    	public void setUserName(String userName) {
    		UserName = userName;
    	}
    	public String getPassword() {
    		return password;
    	}
    	public void setPassword(String password) {
    		this.password = password;
    	}
    }
    
    
  • dao

    package com.springboot.huyitest.dao;
    
    
    import org.springframework.data.jpa.repository.JpaRepository;
    import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
    import org.springframework.stereotype.Repository;
    import com.springboot.huyitest.pojo.userPojo;
    
    @Repository(value = "userPojo")
    public interface userDao extends JpaRepository<userPojo, Integer>,JpaSpecificationExecutor<userPojo>{
    
    }
    
    
  • service

    package com.springboot.huyitest.service;
    
    import java.util.List;
    
    import com.springboot.huyitest.pojo.userPojo;
    
    public interface userService {
    	List<userPojo> getUser();
    	void addUser(userPojo user);
    }
    
    
  • serviceImpl

    package com.springboot.huyitest.service.Impl;
    
    import java.util.List;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    import com.springboot.huyitest.dao.userDao;
    import com.springboot.huyitest.pojo.userPojo;
    import com.springboot.huyitest.service.userService;
    
    @Service
    public class userServiceImpl implements userService{
    	@Autowired
    	private userDao userdao;
    	
    	@Override
    	public List<userPojo> getUser() {
    		// TODO Auto-generated method stub
    		return userdao.findAll();
    	}
    
    	@Override
    	public void addUser(userPojo user) {
    		// TODO Auto-generated method stub
    		userdao.save(user);
    	}
    
    }
    
    
  • controller

  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运行项目,这个项目就搭建完啦!!!*

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



这篇关于保姆级搭建springboot+web+mysql+hibernate+thymeleaf+druid项目的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程