基于SpringBoot+Mybatis+Thymeleaf的XX管理系统实现二(controller、entity、service层代码编写)

论坛 期权论坛 编程之家     
选择匿名的用户   2021-5-26 10:23   639   0

环境搭建可以查看我的上一篇文章

基于SpringBoot+Mybatis+Thymeleaf的XX管理系统实现一(准备+环境搭建)

这一部分实现controller层、entity层、service层的相关代码,先把spring的hello world跑通

也是对第一步配环境的检查和对spring DI和IOC思想的理解

controller层——控制层,常用于接受request请求,返回response请求

entity层——实体层,主要保存数据库实体的实体类,

service层——用于对服务进行实现,逻辑进行实现

mapper层——用于存放DB操作的接口类

注:在resource下也要与mapper层有对应的文件夹和对应的文件名存放sql的xml文件,并进行配置

mysql就可以寻找到,resource的创建目录时,如果写成com.anthony.infosystem不能创建三级,只创建一级目录

因此,使用如上的创建方式即可创建三级。

再到这个infosystem下创建mapper目录,存放xml文件

将不同层的Java类文件创建好之后,如图所示,

然后开始逐个编写代码。 先实现最基本的功能 写个简单的springboot+mybatis的hello world

即 使用select * from table语句实现打印全部,连接数据库成功

所有代码如下:

controller

package com.anthony.infosystem.controller;

import com.anthony.infosystem.entity.Bond;
import com.anthony.infosystem.service.IBondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class controller {

    @Autowired
    IBondService bondService;

    @RequestMapping("/showAll")
    public String PrintAll(Model model)
   {
        List<Bond> bondList=bondService.findAllRecord();

        model.addAttribute("list",bondList);
       return "HelloWorld";
   }
}

实体类Bond (getter和setter、tostring需要右键由IDEA生成,字段名字和数据库要对应)

package com.anthony.infosystem.entity;


import java.util.Date;

public class Bond {

    Integer id;
    String bonds_name;
    String sales_name;
    Integer amount;
    Date created_at;
    Date updated_at;

    public Bond() {
    }

    @Override
    public String toString() {
        return "Bond{" +
                "id=" + id +
                ", bonds_name='" + bonds_name + '\'' +
                ", sales_name='" + sales_name + '\'' +
                ", amount=" + amount +
                ", created_at=" + created_at +
                ", updated_at=" + updated_at +
                '}';
    }

    public Bond(Integer id, String bonds_name, String sales_name, Integer amount, Date created_at, Date updated_at) {
        this.id = id;
        this.bonds_name = bonds_name;
        this.sales_name = sales_name;
        this.amount = amount;
        this.created_at = created_at;
        this.updated_at = updated_at;
    }
    public Integer getId() {
        return id;
    }

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

    public String getBonds_name() {
        return bonds_name;
    }

    public void setBonds_name(String bonds_name) {
        this.bonds_name = bonds_name;
    }

    public String getSales_name() {
        return sales_name;
    }

    public void setSales_name(String sales_name) {
        this.sales_name = sales_name;
    }

    public Integer getAmount() {
        return amount;
    }

    public void setAmount(Integer amount) {
        this.amount = amount;
    }

    public Date getCreated_at() {
        return created_at;
    }

    public void setCreated_at(Date created_at) {
        this.created_at = created_at;
    }

    public Date getUpdated_at() {
        return updated_at;
    }

    public void setUpdated_at(Date updated_at) {
        this.updated_at = updated_at;
    }
}

Mapper

package com.anthony.infosystem.mapper;

import com.anthony.infosystem.entity.Bond;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface BondMapper {

    List<Bond> findAll();
}

service接口

package com.anthony.infosystem.service;

import com.anthony.infosystem.entity.Bond;

import java.util.List;

public interface IBondService {

    List<Bond> findAllRecord();
}

service实现类

package com.anthony.infosystem.service.impl;

import com.anthony.infosystem.entity.Bond;
import com.anthony.infosystem.mapper.BondMapper;
import com.anthony.infosystem.service.IBondService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class BondServiceImpl implements IBondService {

    @Autowired
    BondMapper bondMapper;

    @Override
    public List<Bond> findAllRecord() {

        return bondMapper.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.anthony.infosystem.mapper.BondMapper">
    <select id="findAll" resultType="Bond">
        select * from demo_bonds_sales_record;

    </select>
</mapper>

application.properties文件 (注意路径和mysql的url 这里改了端口号为8077)

#mybatis连接数据库的参数
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/group3?characterEncoding=utf-8&useSSL=false&serverTimezone=Hongkong
spring.datasource.username=root
spring.datasource.password=1234
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/

#mybatis的配置
mybatis.type-aliases-package=com.anthony.infosystem.entity
mybatis.mapper-locations=classpath:com/anthony/infosystem/mapper/*.xml
server.port=8077
spring.thymeleaf.cache=false

HelloWorld.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>债券查询结果</title>
</head>
<body>

<h1>债券查询结果</h1>

<table class="table table-bordered table-striped mb-none" id="datatable-default">
         <thead>
          <tr>
           <th>债券类型</th>
           <th>债券销售经理</th>
           <th>交易价格</th>
           <th class="hidden-phone">创建时间</th>
           <th class="hidden-phone">修改时间</th>
          </tr>
         </thead>
         <tbody>

          <tr class="gradeX" th:each="list:${list}">
           <td th:text="${list.bonds_name}">A</td>
           <td th:text="${list.sales_name}">张三</td>

           <td th:text="${list.amount}">1</td>
           <td th:text="${list.created_at}">4</td>
           <td th:text="${list.updated_at}">X</td>
          </tr>

         </tbody>
        </table>

</body>
</html>

然后运行并访问localhost:8077/showAll即可查看数据

必须保证mysql数据库服务是开的、url正确、里面有数据!!!

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP