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

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


__EOF__

本文作者一只小狐疑
本文链接https://www.cnblogs.com/Huyi-1208/p/16339473.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   一只小狐疑  阅读(158)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示