springboot+mybatis 实现根据条件查询数据库 将结果集合循环遍历显示在页面上

1、model层  bean类

package com.example.demo.model;
public class Lamp {
    private int id;
    //支付账单的openid
    private String openid;
    //发起人
   private String sname;
   //祝福人
   private String gname;
   //灯的位置
    private String address;
    //点灯的时间如7天
    private int cycle;
    //灯的类型
    private String kinds;
    //祝福语
    private String content;
    //付费的金额
    private String money;
    //付费的时间
    private String time;

    public void setId(int id) {
        this.id = id;
    }

    public int getId() {
        return id;
    }

    public String getOpenid() {
        return openid;
    }

    public String getSname() {
        return sname;
    }

    public String getGname() {
        return gname;
    }

    public String getAddress() {
        return address;
    }

    public int getCycle() {
        return cycle;
    }

    public String getKinds() {
        return kinds;
    }

    public String getContent() {
        return content;
    }

    public String getMoney() {
        return money;
    }

    public String getTime() {
        return time;
    }

    public void setOpenid(String openid) {
        this.openid = openid;
    }

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

    public void setGname(String gname) {
        this.gname = gname;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setCycle(int cycle) {
        this.cycle = cycle;
    }

    public void setKinds(String kinds) {
        this.kinds = kinds;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setMoney(String money) {
        this.money = money;
    }

    public void setTime(String time) {
        this.time = time;
    }
}

2、在数据库中创建表

springboot+mybatis 实现根据条件查询数据库 将结果集合循环遍历显示在页面上_第1张图片

 3、编写dao层代码

package com.example.demo.dao;
import com.example.demo.model.Lamp;
import java.util.List;
public interface LampDao {
    //插入数据
    int insert(Lamp l);
    //查询数据
    List query(String openid);
}

4、实现类

package com.example.demo.service;
import com.example.demo.dao.LampDao;
import com.example.demo.model.Lamp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class LampService {
    @Autowired
    private LampDao ld;
    public int insert(Lamp l) {
        return ld.insert(l);
    }
    public List query(String openid) {

        return ld.query(openid);
    }
}

5、编写服务层代码

package com.example.demo.control;
import com.example.demo.model.Lamp;
import com.example.demo.service.LampService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
@Controller()
@Scope("prototype")
@RequestMapping("/lamp")
public class LampControl {
    @Autowired
    private LampService us;
    @RequestMapping("/query")
    public String  query(@Param("openid")String openid, Model model) {
        System.out.println("openid:"+openid);
        List list_lamp=us.query(openid);
        model.addAttribute("list",list_lamp);
        //把这个集合发送给my前端页面
     return "my";
    }
    @RequestMapping("/insert")
    public int insert(@RequestBody Lamp lamp) throws  Exception{
        System.out.println("插入数据库操作");
        return us.insert(lamp);
    }
}

6、显示页面




    
    我的中心


序号 学生id 学生姓名 学生年龄 学生课程-课程列表 学生课程-课程分数

最关键的部分是,java代码如何与数据库进行连接

连接部分  

application.yml

server:
  port: 80
spring:
  thymeleaf:
    prefix: classpath:/templates/
  datasource:
    url: jdbc:mysql://localhost:3306/idea?serverTimezone=UTC
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver

#server:
#port:8088
mybatis:
  mapper-locations: classpath:mapper/*.xml
  type-aliases-package: com.example.demo.model

# 测试账号
##微信支付的部分


操作部分

在mapper的下面的LampMapper.xml文件里面




    
        
        
        
        
        
        
        
        
        
        
    

    
        id, openid,sname,gname,address,cycle,kinds,content,money,time
    

    

    
        insert into lamp ( id, openid,sname,gname,address,cycle,kinds,content,money,time
        )
        values (#{id,jdbcType=INTEGER}, #{openid,jdbcType=VARCHAR}, #{sname,jdbcType=VARCHAR}, #{gname,jdbcType=VARCHAR}, #{address,jdbcType=VARCHAR}, #{cycle,jdbcType=VARCHAR}, #{kinds,jdbcType=VARCHAR}, #{content,jdbcType=VARCHAR}, #{money,jdbcType=VARCHAR}, #{time,jdbcType=VARCHAR}
        )
    


springboot+mybatis 实现根据条件查询数据库 将结果集合循环遍历显示在页面上_第2张图片


运行结果

springboot+mybatis 实现根据条件查询数据库 将结果集合循环遍历显示在页面上_第3张图片

 

你可能感兴趣的:(springboot菜鸟教程)