springboot+thymeleaf+jpa一个简单的查询demo

每次做小demo的时候,在网上找例子,总感觉不是自己需要的例子,闲来无事,自己折腾一个,以后要需要添加新的功能直接在上面集成就ok了。话不多说,进入正题。
先整体看一下我的结构图
在这里插入图片描述

<?xml version="1.0" encoding="UTF-8"?>

<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>


<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

springboot的配置文件
spring.datasource.url=jdbc:mysql://localhost/test?useUnicode=true&characterEncoding=utf-8&serverTimezone=UTC&useSSL=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.username=你的用户名
spring.datasource.password=你的密码

spring.jpa.properties.hibernate.hbm2ddl.auto=create
pring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.show-sql= true
spring.thymeleaf.cache=false

编写repository接口

public interface UserRespository extends JpaRepository<User,Integer> {

 User findUserById(int id);

 List<User> findAll();

}

service
public interface UserService {

User findUserById(int id);

List<User> findAll();

}

实现类

@Service
public class UserServiceImpl implements UserService {

@Autowired
private UserRespository userRespository;

 public User findUserById(int id){
    return userRespository.findUserById(id);
}

public List<User> findAll(){
     return userRespository.findAll();
}

}
controller

@Controller
public class UserController {

@Autowired
private UserService userService;

@RequestMapping("/find")
public String findUserById(Integer id, Model model){
   User user= userService.findUserById(id);
   model.addAttribute("user",user);
   return "userEdit";
}

@RequestMapping("/findAll")
public String findAll(Model model){
    List<User> list=userService.findAll();
    model.addAttribute("list",list);
    return "add";
}

}
实体类User

@Data
@Entity
public class User implements Serializable {

@Id @GeneratedValue
private Integer id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private Integer age;

}
其中@Data注解是lombok,有兴趣的同学可以自己看一下
@Entity,@Id等注解的作用我就不叙述了 有兴趣的同学自己可以看一下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
首先,需要配置好 Spring Boot、Hibernate 和 Thymeleaf 的环境,具体步骤可以参考官方文档或者其他教程。 接下来,我们按照 DAO -> Entity -> Service -> Controller 的顺序来实现。 1. DAO 首先,我们需要创建一个 UserRepository 接口,继承 JpaRepository,并且定义一个名为 findByRole 的方法,用于查询指定角色的用户。 ```java public interface UserRepository extends JpaRepository<User, Long> { Page<User> findByRole(String role, Pageable pageable); } ``` 2. Entity 接着,我们需要创建一个 User 实体类,用于映射 MySQL 中的用户表。 ```java @Entity @Table(name = "user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Long user_id; private String user_name; private String account; private String password; private String phone; private String address; private String role; // 省略 getter 和 setter } ``` 3. Service 然后,我们需要创建一个 UserService 接口和一个 UserServiceImpl 类。在 UserServiceImpl 类中,我们需要注入 UserRepository,然后调用其方法来查询用户。 ```java public interface UserService { Page<User> findByRole(String role, Pageable pageable); } @Service public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public Page<User> findByRole(String role, Pageable pageable) { return userRepository.findByRole(role, pageable); } } ``` 4. Controller 最后,我们需要创建一个 UserController 类,用于处理用户的 HTTP 请求。在 UserController 类中,我们需要注入 UserService,并且定义一个名为 list 的方法,用于查询所有用户并返回 user-check.html 页面。 ```java @Controller public class UserController { @Autowired private UserService userService; @GetMapping("/user-check") public String list(@RequestParam(defaultValue = "0") int page, @RequestParam(defaultValue = "10") int size, Model model) { Pageable pageable = PageRequest.of(page, size); Page<User> users = userService.findByRole("user", pageable); model.addAttribute("users", users); return "user-check"; } } ``` 最后,我们需要在 resources/templates 目录下创建一个名为 user-check.html 的 Thymeleaf 模板,用于展示用户列表。具体的页面实现可以根据需求自行设计。 这样,我们就完成了分页查询所有用户的功能。整个项目的目录结构和代码可以参考下面的示例。 ```bash ├── src │ ├── main │ │ ├── java │ │ │ ├── com │ │ │ │ ├── example │ │ │ │ │ ├── demo │ │ │ │ │ │ ├── controller │ │ │ │ │ │ │ └── UserController.java │ │ │ │ │ │ ├── dao │ │ │ │ │ │ │ └── UserRepository.java │ │ │ │ │ │ ├── entity │ │ │ │ │ │ │ └── User.java │ │ │ │ │ │ ├── service │ │ │ │ │ │ │ ├── impl │ │ │ │ │ │ │ │ └── UserServiceImpl.java │ │ │ │ │ │ │ └── UserService.java │ │ │ │ │ │ └── DemoApplication.java │ │ └── resources │ │ ├── application.properties │ │ ├── templates │ │ │ └── user-check.html ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值