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

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

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

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

小狐狸崽子OvO

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

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

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

打赏作者

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

抵扣说明:

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

余额充值