使用Springboot写一个网站

使用Springboot写一个网站

之前的实训使用Springboot写过一个网站,把他记录一下
springboot比较好的一点就是内置Tomcat,之前写jave ee的时候真是痛不欲生,tomcat总是能莫名其妙的出问题,这个还是比较方便

主要配置文件

那一大堆问号是因为从朋友那里直接拷贝过来的,估计是编码问题,你们可以把他改成gbk试试
主要就是配置一下数据库的连接


#???????
server.port=8088
#????   IP??:???/??????
#??????  http://localhost:??/????/
server.servlet.context-path=/yaohao

#mysql8??
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#mysql5??   ???????????ip??
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/tf?characterEncoding=utf-8&useSSL=false&serverTimezone=GMT
spring.datasource.username=root
spring.datasource.password=123456
#mybatis??
mybatis.type-aliases-package=com.code.entity
#mybatis.mapper-locations=classpath*:mapper/*.xml
mybatis-plus.mapper-locations=classpath*:mapper/*.xml

#??sql??
logging.level.com.example.project1=debug

#jsp?????
spring.mvc.view.prefix=/view/
spring.mvc.view.suffix=.jsp

# ??jsp??????
server.servlet.jsp.init-parameters.development=true

# ?????????
spring.servlet.multipart.max-request-size=10MB
# ????????
spring.servlet.multipart.max-file-size=10MB


controller类

在controller类里面,每一个实体都需要对应一个controller类,更准确的来说,是你想要实现的操作
foodcontroller类

package com.example.project1.conctroller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.project1.entity.Food;
import com.example.project1.entity.FoodType;
import com.example.project1.mapper.FoodMapper;
import com.example.project1.mapper.FoodTypeMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class FoodConctroller {
    @Resource
    FoodMapper foodMapper;
    @Resource
    FoodTypeMapper foodTypeMapper;

    @RequestMapping("/FoodIndex")
    public String FoodIndex(Model m) {
        List<Food> foods = foodMapper.selectList(null);
        m.addAttribute("data",foods);

        return "FoodIndex";
    }




    //美食列表界面

//    @RequestMapping("/FoodList")
//    public String FoodList(Model model) {
//        List<Food> foods = foodMapper.selectList(null);
//        model.addAttribute("data",foods);
//        return "FoodList";
//    }
//跳转美食添加界面
    @RequestMapping("/FoodAdd")
    public String FoodAdd(Model model) {
        List<FoodType> foodTypes = foodTypeMapper.selectList(null);
        model.addAttribute("type", foodTypes);

        return "FoodAdd";
    }
    //插入数据库
    @RequestMapping("/FoodInsert")
    public String FoodInsert(Food f) {
        foodMapper.insert(f);
        return "redirect:FoodList";
    }
    //删除数据
    @RequestMapping("/FoodDelete")
    public String FoodDelete(int id) {
        foodMapper.deleteById(id);
        return "redirect:FoodList";
    }
    //更新数据

    @RequestMapping("/FoodEdit")
    public String bookEdit(int id, Model m) {
        List<FoodType> foodTypes = foodTypeMapper.selectList(null);
        m.addAttribute("foodtype", foodTypes);

        Food food = foodMapper.selectById(id);
        m.addAttribute("foodobj", food);
        return "FoodEdit5";

    }//通过edit调出界面,再由FoodUpdate实现
    @RequestMapping("/FoodUpdate")
    public String FoodUpdate(Food food) {
        foodMapper.updateById(food);
        return "redirect:FoodList";
    }
    //查找
    @RequestMapping("/FoodList")
    public String FoodList(Model m, String keyword, String caixi) {
        //   System.out.println("keyword = " + keyword);
        EntityWrapper<Food> wrapper = new EntityWrapper<Food>();
       // EntityWrapper<Food> wrapper = new EntityWrapper<Food>();
        if (null != keyword && !"".equals(keyword)) {
            wrapper.like("foodname", keyword);
            m.addAttribute("keyword", keyword);
        }
        if (null != caixi && !"".equals(caixi)) {
            wrapper.and().eq("type", caixi);
            m.addAttribute("caixi", caixi);
        }

        List<Food> foods = foodMapper.selectList(wrapper);

        m.addAttribute("data", foods);
        return "FoodList";
    }
}


logincontroller类

package com.example.project1.conctroller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.project1.entity.Admininfo;
import com.example.project1.mapper.AdmininfoMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class LoginController {
    @Resource
    AdmininfoMapper admininfoMapper;

    //跳转到登录页面
    @RequestMapping("/login")
    public String login() {
        return "login";
    }

    //退出需要清空session里面的东西
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session) {
        //   session.invalidate();//清空session中的值
        session.removeAttribute("loginuser");//移出loginuser的值,最好用第二个
        return "login";
    }

    //登录验证
    @RequestMapping("/loginSubmit")
    public String login(String name, String pwd, Model m, HttpSession session) {//model只能传值一个页面,session可以一直用
        //构建查询条件
        EntityWrapper<Admininfo> wrapper = new EntityWrapper<Admininfo>();
        wrapper.eq("username", name).and().eq("password", pwd);
        //调用查询方法
        List<Admininfo> admininfos = admininfoMapper.selectList(wrapper);
        //判断是否登录成功
        if (admininfos.size()>0) {
            //成功
            Admininfo admininfo = admininfos.get(0);
          //  m.addAttribute("loginuser",admininfo);

          //  Admininfo admininfo = admininfos.get(0);//获取第一条数据
            session.setAttribute("loginuser", admininfo);
            return "redirect:FoodIndex";
        } else {
            //失败
            System.out.println("登录失败");
            m.addAttribute("msg", "账号或者密码错误,请重试");
            return "login";
        }

    }
    @RequestMapping("/register")
    public String register(){
        return "register";
    }
    @RequestMapping("registerinfo")
    public String reisterinfo(Admininfo admininfo){
        admininfoMapper.insert(admininfo);

        return "redirect:login";
    }


}

placecontroller类

package com.example.project1.conctroller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.example.project1.entity.Place;
import com.example.project1.mapper.PlaceMapper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class PlaceConctroller {
    @Resource
    PlaceMapper placeMapper;
//        @RequestMapping("/PlaceList")
//    public String FoodList(Model model) {
//            List<Place> places = placeMapper.selectList(null);
//           // List<Food> foods = foodMapper.selectList(null);
//        model.addAttribute("data",places);
//        return "PlaceList";
//    }



    @RequestMapping("/PlaceIndex")
    public String PlaceIndex(Model m) {
        List<Place> places = placeMapper.selectList(null);
        m.addAttribute("data",places);

        return "PlaceIndex";
    }
    @RequestMapping("/PlaceList")


    public String PlaceList(Model m, String keyword, String didian) {
        EntityWrapper<Place> wrapper = new EntityWrapper<Place>();
        if (null != keyword && !"".equals(keyword)) {
            wrapper.like("placename", keyword);
            m.addAttribute("keyword", keyword);
        }
        if (null != didian && !"".equals(didian)) {
            wrapper.and().eq("placespot", didian);
            m.addAttribute("didian", didian);
        }

        List<Place> places = placeMapper.selectList(wrapper);
        m.addAttribute("data", places);
        return "PlaceList";
    }
    //pass

    @RequestMapping("/PlaceEdit")
    public String PlaceEdit(int id,Model m){
        Place place = placeMapper.selectById(id);

        m.addAttribute("obj",place);

        return "PlaceEdit";
    }
    @RequestMapping("/PlaceUpdate")
    public String unserinfoUpdate(Place p){
        placeMapper.updateById(p);


        return "redirect:PlaceList";
    }


    @RequestMapping("/PlaceAdd")
    //跳转到插入界面
    public String PlaceAdd(Model model){
        //List<Place> places =placeMapper.selectList(null);

      //  model.addAttribute("users",places);
        return "PlaceAdd";
    }
    //插入数据库
    @RequestMapping("/PlaceInsert")
    public String PlaceInsert(Place p) {

        placeMapper.insert(p);
        return "redirect:PlaceList";
    }
    //删除数据
    @RequestMapping("/PlaceDelete")
    public String PlaceDelete(int id){
        placeMapper.deleteById(id);
        //重定向
        return "redirect:PlaceList";
    }
}



uploadcontroller类

package com.example.project1.conctroller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Controller
public class UploadController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

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

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "D:\\upload";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败!";
    }


}


userinfocontroller类

package com.example.project1.conctroller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Controller
public class UploadController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UploadController.class);

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

    @PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "D:\\upload";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败!";
    }


}


实体类

然后需要再设立一个实体的类
对每一个实体所需要的变量进行定义
具体代码都差不多,我就随便找一个类放上来了

package com.example.project1.entity;

import com.baomidou.mybatisplus.annotations.TableId;
import com.baomidou.mybatisplus.annotations.TableName;
import com.baomidou.mybatisplus.enums.IdType;
import lombok.Data;

@Data
@TableName("admininfo")
public class Admininfo {
    @TableId(type = IdType.AUTO)
    private Integer id;	//编号
    private String username;	//管理员账号名
    private String password;	//管理员密码
    private String headimg;
}

mapper

时间有点久,忘了这个是干嘛的了,有知道的兄弟可以在评论区解答一下

package com.example.project1.mapper;


import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.example.project1.entity.Admininfo;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface AdmininfoMapper extends BaseMapper<Admininfo> {
}

貌似是控制用的,但是不确定,几个mapper都差不多,就放一个吧

project1Application

package com.example.project1;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Project1Application {

    public static void main(String[] args) {
        SpringApplication.run(Project1Application.class, args);
    }

}

最后,这个是我的文件结构

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值