ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

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

2022-06-03 03:00:24  阅读:227  来源: 互联网

标签:web hibernate springboot huyitest user import com public


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

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

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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有