大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)

刚入门,此项目包括SpringBoot与Mybatis的整合,前端使用freemarker。项目包含增删改查模糊查询与分页

 

先来创建数据

表结构(三表):

          学生表:

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第1张图片

          老师表:

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第2张图片

          中间表:

   向数据表中填入数据:

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第3张图片

打开   idea 

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第4张图片

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第5张图片

idea自动导入依赖,这里前端使用  freemarker

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第6张图片

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第7张图片

可以看到,这是自动导入之后
 

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第8张图片

 在application.properties中配置

server.port=8080
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/rabbitmq_test?serverTimezone=GMT%2B8

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

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第9张图片

          在pom文件中加入mybatis的分页插件。


            com.github.pagehelper
            pagehelper-spring-boot-starter
            1.2.10
        

 

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第10张图片

          加入之后记刷新jar包

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第11张图片

还有需要加入相应的 js。如果觉得页面不好看的可以加入css样式。这里选择bootstrap

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第12张图片

 

目录:

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第13张图片

 

实体类 Student:

package com.xinyi.mydemo.com.xinyi.entity;

public class Student {

    private int sid;
    private String sname;
    private String sage;
    private String tname;

    public Student() {
    }

    public Student(int sid, String sname, String sage, String tname) {
        this.sid = sid;
        this.sname = sname;
        this.sage = sage;
        this.tname = tname;
    }

    public int getSid() {
        return sid;
    }

    public void setSid(int sid) {
        this.sid = sid;
    }

    public String getSname() {
        return sname;
    }

    public void setSname(String sname) {
        this.sname = sname;
    }

    public String getSage() {
        return sage;
    }

    public void setSage(String sage) {
        this.sage = sage;
    }

    public String getTname() {
        return tname;
    }

    public void setTname(String tname) {
        this.tname = tname;
    }

    @Override
    public String toString() {
        return "Student{" +
                "sid=" + sid +
                ", sname='" + sname + '\'' +
                ", sage='" + sage + '\'' +
                ", tname='" + tname + '\'' +
                '}';
    }
}

controller层  StudentController:

package com.xinyi.mydemo.com.xinyi.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import com.xinyi.mydemo.com.xinyi.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

@RestController
@RequestMapping("student")
public class StudentController {


    @Autowired
    private StudentService studentService;

    
    @GetMapping("getStudentAll")
    public ModelAndView getStudentAll(@RequestParam(required = false,defaultValue = "1") Integer pageNum){
        ModelAndView mv = new ModelAndView();
        PageHelper.startPage(pageNum,3);
        Page students = studentService.selectStudentWithPage();
        PageInfo studentPageInfo = students.toPageInfo();
        mv.addObject("list",studentPageInfo.getList());
        mv.setViewName("selectStudent");
        return mv;
    }

}

service层  接口StudentService:

package com.xinyi.mydemo.com.xinyi.service;

import com.github.pagehelper.Page;
import com.xinyi.mydemo.com.xinyi.entity.Student;

public interface StudentService {
    Page selectStudentWithPage();
}

service层   StudentImpl:

package com.xinyi.mydemo.com.xinyi.service.imp;

import com.github.pagehelper.Page;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import com.xinyi.mydemo.com.xinyi.mapper.StudentMapper;
import com.xinyi.mydemo.com.xinyi.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class StudentImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;


    @Override
    public Page selectStudentWithPage() {
        return studentMapper.selectStudentWithPage();
    }
}

mapper层  接口StudentMapper:

package com.xinyi.mydemo.com.xinyi.mapper;

import com.github.pagehelper.Page;
import com.xinyi.mydemo.com.xinyi.entity.Student;
import org.apache.ibatis.annotations.Mapper;


@Mapper   //加入Mapper注解
public interface StudentMapper {
    Page selectStudentWithPage();
}

mapper.*xml   .StudentMapper.xml:






    

页面     templates/selectStudent.ftl:






    <#list list as list>
        
学生序号 学生名称 学生年龄 教导老师
${list.sid} ${list.sname} ${list.sage} ${list.tname}

测试:

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第14张图片

 

访问路径:http://localhost:8080/student/getStudentAll

结果:

大白话、最简单——SpringBoot+Mybatis+freemarker整合(一)_第15张图片

 

 

这样一个简单的结果就出现了。

 

你可能感兴趣的:(springboot,mybatis,freemarker)