springboot 增删改查

配置springboot

server:
  port: 8080

#  jdbc 的配置
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/db_gm?userSSL=true&characterEncoding=utf-8&&serverTimezone=UTC
    username: root
    password: 123456

#  视图解析器
  mvc:
    view:
      prefix: /
      suffix: .html

# Mybatis
  mybatis:
    type-aliases-package: it.gm.springboot.pojo

dao层

package it.gm.springboot.dao;

import it.gm.springboot.pojo.UserInfor;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UserInforDao {
//    添加
@Insert("insert into tb_user(username,password) values(#{username},#{password})")
    int addUser(UserInfor userInfor);
//      删除
    @Delete("delete from tb_user where id =#{id}")
    int delUser(int id);
//    修改
    @Update("update tb_user set username=#{username},password = #{password} where id = #{id}")
    int updUser(UserInfor userInfor);
//    查询所有
    @Select("select * from tb_user")
    List<UserInfor> findAllUser();
//    查询单个
    @Select("select * from tb_user where id =#{id}")
    UserInfor findUserById(int id);
}

service层

package it.gm.springboot.service;
import it.gm.springboot.pojo.UserInfor;

import java.util.List;

public interface UserInforService {
//    添加用户
    int addUser(UserInfor userInfor);

//    删除用户
    int delUser(int id);

//    修改用户
    int updUser(UserInfor userInfor);

//    查询所有
    List<UserInfor> findAllUser();

//    查询单个用户
    UserInfor findUserById(int id);
}

实现类

package it.gm.springboot.service.Impl;

import it.gm.springboot.dao.UserInforDao;
import it.gm.springboot.pojo.UserInfor;
import it.gm.springboot.service.UserInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserInforServiceDaoImpl implements UserInforService {
    @Autowired
    private UserInforDao userInforDao;
    @Override
    public int addUser(UserInfor userInfor) {
        return userInforDao.addUser(userInfor);
    }

    @Override
    public int delUser(int id) {
        return userInforDao.delUser(id);
    }

    @Override
    public int updUser(UserInfor userInfor) {
        return userInforDao.updUser(userInfor);
    }

    @Override
    public List<UserInfor> findAllUser() {
        return userInforDao.findAllUser();
    }

    @Override
    public UserInfor findUserById(int id) {
        return userInforDao.findUserById(id);
    }
}

controller层

package it.gm.springboot.controller;

import it.gm.springboot.pojo.UserInfor;
import it.gm.springboot.service.UserInforService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/userInforController")
public class UserInforController {
    @Autowired
    private UserInforService userInforService;

    @RequestMapping("/addUser")
    public String addUser(UserInfor userInfor){
        int i = userInforService.addUser(userInfor);
        if (i>0){
            System.out.println("添加成功");
        }
        else {
            System.out.println("添加失败");
        }
        return "index";
    }
    @RequestMapping("/delUser")
    public String delUser(int id){
        int i = userInforService.delUser(id);
        if (i>0){
            System.out.println("删除成功");
        }
        else {
            System.out.println("删除失败");
        }
        return "index";
    }
    @RequestMapping("/updUser")
    public String updUser(UserInfor userInfor){
        int i = userInforService.updUser(userInfor);
        if (i>0){
            System.out.println("修改成功");
        }
        else {
            System.out.println("修改失败");
        }
        return "index";
    }
    @RequestMapping("/findAllUser")
    public String findAllUser(){
        List<UserInfor> allUser = userInforService.findAllUser();
        for (UserInfor userInfor:allUser){
            System.out.println(userInfor);
            System.out.println();
        }
        return "index";
    }
    @RequestMapping("/findUserById")
    public String findUserById(int id){
        UserInfor userInfor = userInforService.findUserById(id);
        System.out.println(userInfor);
        return "index";
    }
}

html成功页面

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>成功页面</title>
</head>
<body>
成功了
</body>
</html>

填写dao层扫描路径,配置自动打开默认页面

package it.gm.springboot;

import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.io.IOException;

@MapperScan("it.gm.springboot.dao")
@SpringBootApplication
public class SpringbootApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringbootApplication.class, args);
    }
    @EventListener({ApplicationReadyEvent.class})
    public void applicationReadyEvent() {
        System.out.println("应用已经准备就绪 ... 启动浏览器");
        String url = "http://localhost:8080";
        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " + url);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值