springboot查询全过程-版本1

本文介绍了一个使用MyBatis、Spring Boot和Thymeleaf实现的学生信息管理系统。系统包括学生实体类、控制器、服务层、持久层及对应的XML映射文件,实现了从数据库查询所有学生信息并展示的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

bean

@Data
public class Student {
    private Integer id;
    private String name;
    private Integer age;
    private String className;
}

controller

@Controller
public class StudentController {
    @Autowired
    StudentService studentService;
    @GetMapping("getall")
    public String getall(Model model){
        List<Student> list = studentService.getAll();
        model.addAttribute("info", list);
        return "mycrud";
    }
}

service

@Service
public class StudentService {
    @Autowired
    StudentMapper studentMapper;
    public List<Student> getAll() {
        return studentMapper.findAll();
    }
}

mapper

@Mapper
public interface StudentMapper {
    List<Student> findAll();
}

mapper.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.myself.wykpage.mapper.StudentMapper">
    <resultMap type="com.myself.wykpage.bean.Student" id="studentMap">
        <id property="id" column="id" />
        <result property="name" column="name" />
        <result property="age" column="age" />
        <result property="className" column="class_name" />
    </resultMap>
    <select id="findAll" resultMap="studentMap">
        select * from student
    </select>
    </mapper>

properties配置内容

spring.datasource.username=root
spring.datasource.password=1234
spring.datasource.url=jdbc:mysql://localhost:3306/students
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

mybatis.mapper-locations=classpath:xml/*.xml

pom.xml配置内容(配置在plugin标签中)

<resources>
            <resource>
                <directory>src/main/resources</directory>
            </resource>
  </resources>
<dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.0</version>
        </dependency>
         <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.47</version>
        </dependency>

前端页面

<table cellpadding="5" cellspacing="0" border="1">
    <tr>
        <th>id</th>
        <th>name</th>
        <th>age</th>
        <th>className</th>
    </tr>
        <tr th:each="students:${info}">
            <td th:text="${students.id}"></td>
            <td th:text="${students.name}"></td>
            <td th:text="${students.age }"></td>
            <td th:text="${students.className}"></td>
        </tr>
    <tr>

完成!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值