SpringBoot初级开发--做一个MVC的Web整体工程(2)

SpringBoot很简单的就可以创建一个MVC的web项目,根据IDEA的Spring Iinitializr选择如下
在这里插入图片描述
选择需要的依赖选项
在这里插入图片描述
创建工程后。我们把整个工程进行JAVA包结构分层。建立controller,service,dao,exception,po,vo,util
controler 控制器包
service 业务逻辑包
dao 数据库访包
model 模型类包,下面还包含了po,vo子包
  po 持久化对象,主要用户数据库
  vo 值对象,用于视图表达
exception 自定义异常类包
util 工具包

在这里插入图片描述
同时我们把整个工程的编码设置成UTF8,整个工程src的目录结构分别是
src/main/java 主要的java程序保存目录
src/main/resources 静态资源和配置文件保存目录
src/test/java 主要java程序测试用例保存目录
在这里插入图片描述
在controller包增加一个Welcome控制器类
在这里插入图片描述
内容如下

package com.example.firstweb.controller;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class Welcome {

     @GetMapping("/welcomeindex")
    public String welcomeIndex(){
        return "welcomeindex";
    }
}

在pom.xm文件中,加入mybatis依赖

       <!-- mybatis -->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>3.1.1</version>
        </dependency>
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper-spring-boot-starter</artifactId>
            <version>2.1.5</version>
        </dependency>

开始生成一个PO对象WelcomePO,与数据库进行衔接

package com.example.firstweb.service;

import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Getter
@Setter
@Data
@Table(name = "welcome_info")
public class WelcomePo {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY,generator = "JDBC")
    private Long id;
    private String info;
}

在Mysql数据库,建立一个test数据库,并且建立一张welcome_info表。并且插入一条数据

create database test default character set utf8;

create table welcome_info(id int(11) NOT NULL AUTO_INCREMENT,info varchar(255) default null,PRIMARY KEY (`id`));

insert into welcome_info(info) values('welcome every body');

在这里插入图片描述
增加动态数据源POM依赖,以及配置

        <!-- 数据库动态连接池-->
        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
            <version>2.5.4</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.33</version>
        </dependency>

在application.properties增加如下配置

spring.datasource.dynamic.hikari.pool-name=example-cp
spring.datasource.dynamic.hikari.max-pool-size=20
spring.datasource.dynamic.hikari.max-lifetime=17170000
spring.datasource.dynamic.hikari.connectionInitSql=set names utf8mb4

spring.datasource.dynamic.primary=test
#test数据库
spring.datasource.dynamic.datasource.test.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.dynamic.datasource.test.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.dynamic.datasource.test.username=root
spring.datasource.dynamic.datasource.test.password=root

spring.datasource.dynamic.datasource.test.hikari.max-pool-size=12
spring.datasource.dynamic.datasource.test.hikari.max-lifetime=15000000

增加DAO接口WelcomDao,代码r如下

@Mapper
public interface WelcomDao {
    @Select("select * from  welcome_info where id=1")
    @DS("test")
    public WelcomePo getWelcomInfo();
}

增加Service类WelcomeService,代码如下

package com.example.firstweb.service;

import com.example.firstweb.dao.WelcomDao;
import com.example.firstweb.model.po.WelcomePo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service(value = "welcomeService")
public class WelcomeService {
    @Autowired
    private WelcomDao welcomDao;

    public WelcomePo getWelcomInfo(){
        return welcomDao.getWelcomInfo();
    }
}

在Welcome的Controller中增加对service的调用,返回数据库中的数据,并且把po数据拷贝给vo对象。

package com.example.firstweb.controller;


import com.example.firstweb.model.po.WelcomePo;
import com.example.firstweb.model.vo.WelcomeVo;
import com.example.firstweb.service.WelcomeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class Welcome {

    @Autowired
    private WelcomeService welcomeService;

    @GetMapping("/welcomeindex")
    public String welcomeIndex(){

        WelcomePo wpo= welcomeService.getWelcomInfo();

        WelcomeVo wvo= new WelcomeVo();

        BeanUtils.copyProperties(wpo, wvo);

        return "welcomeindex";
    }
}

这样从数据库一直到controller的流程就通了,接下来我们增加thymeleaf的视图页面的表达,先增加依赖配置,然后再次修改controller
pom.xml增加

        <!-- thymeleaf -->
        <dependency>
		        <groupId>org.springframework.boot</groupId>
		        <artifactId>spring-boot-starter-thymeleaf</artifactId>
         </dependency>

welcome的controller修改如下:

package com.example.firstweb.controller;


import com.example.firstweb.model.po.WelcomePo;
import com.example.firstweb.model.vo.WelcomeVo;
import com.example.firstweb.service.WelcomeService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class Welcome {

    @Autowired
    private WelcomeService welcomeService;

    @GetMapping("/welcomeindex")
    public ModelAndView welcomeIndex(){

        ModelAndView view = new ModelAndView("welcomeindex");


        WelcomePo wpo= welcomeService.getWelcomInfo();

        WelcomeVo wvo= new WelcomeVo();

        BeanUtils.copyProperties(wpo, wvo);

        view.addObject("welcomedata", wvo);

        return view;
    }
}


改完controller之后,增加thymeleaf的视图页面,在src/resources/templates目录下增加一个welcomeindex.html的页面。页面内容如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>欢迎页面</title>

</head>
<body>
<h1><b th:text="${welcomedata.info}"></b></h1>
</body>
</html>

这样整个开发流程,就开发完了。一个完整的MVC工程建立完毕,包括了模型视图控制器。
运行程序,因为我在application.properties设置的服务端口是8088.,用浏览器访问http://localhost:8088/welcomeindex 然后看到如下显示
在这里插入图片描述
整体工程可以在这里下载,大家可以自己去看看。链接: https://pan.baidu.com/s/1KkEnFDyP1kC1YwEgLbzyJg 提取码: hemr

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

机核动力

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值