SpringBoot+Mybatis+mysql简单的增删改查+html源码分享

先看一下项目结构

SpringBoot+Mybatis+mysql简单的增删改查+html源码分享_第1张图片

这是打开后的页面效果

SpringBoot+Mybatis+mysql简单的增删改查+html源码分享_第2张图片

首先application.properties的配置

server.port=8012
spring.datasource.url=jdbc:mysql://localhost:3306/xxxx?useUnicode=true&characterEncoding=utf-8
spring.datasource.username=xxxx
spring.datasource.password=xxxx
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10
mybatis.mapper-locations= classpath:mapper/*.xml
spring.http.encoding.force=true
spring.http.encoding.charset=UTF-8
spring.http.encoding.enabled=true
server.tomcat.uri-encoding=UTF-8
spring.thymeleaf.prefix=classpath:/templates/

Pom.xml




    4.0.0
    
        org.springframework.boot
        spring-boot-starter-parent
        2.2.6.RELEASE
         
    
    com.uokos.silence
    honny
    0.0.1-SNAPSHOT
    honny
    Demo project for Spring Boot

    
        1.8
    

    
        
            org.springframework.boot
            spring-boot-starter-web
        

        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        

        
            mysql
            mysql-connector-java
            8.0.22
            runtime
        

        
            org.jdom
            jdom
            1.1.3
        

        
            org.freemarker
            freemarker
            2.3.23
        

        
            org.springframework.boot
            spring-boot-starter-test
            test
            
                
                    org.junit.vintage
                    junit-vintage-engine
                
            
        
        
            com.baomidou
            mybatis-plus-core
            3.1.2
        
        
            org.projectlombok
            lombok
            1.18.8
        
    

    
        
            
                org.springframework.boot
                spring-boot-maven-plugin
            
        
    




Controller控制层

package com.uokos.silence.honny.controller;


import com.uokos.silence.honny.dao.FunUsers;
import com.uokos.silence.honny.service.IFunUsersService;
import com.uokos.silence.honny.service.impl.FunUsersImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/funUser")
public class FunUserController {

    @Autowired
    private IFunUsersService iFunUsers;

    private final Logger logger = LoggerFactory.getLogger(FunUsersImpl.class);


    @GetMapping("/all")
    public String all(Model model) {
        List users = iFunUsers.getAll();
        model.addAttribute("users", users);
        return "user/allUser";
    }



    @GetMapping("/getOne")
    public String getOne(Integer id){
        FunUsers usr = iFunUsers.selectByPrimaryKey(id);
        return usr.toString();
    }

    /**
     * @Description: 根据id 删除
     */

    @RequestMapping(value = "delete/{userId}")
    public ModelAndView delete(@PathVariable Integer userId) {
        iFunUsers.deleteByPrimaryKey(userId);
        ModelAndView mav = new ModelAndView("redirect:/funUser/all");
        return mav;
    }


    /**
     * 添加用户
     * @param funUsers
     * @return
     */
    @RequestMapping("addUser")
    public ModelAndView AddUser(FunUsers funUsers) {
        funUsers.setAddTime(new Date());
        iFunUsers.insertSelective(funUsers);
        ModelAndView mav = new ModelAndView("redirect:/funUser/all");
        return mav;
    }


    @RequestMapping("add")
    public ModelAndView Add1() {
        return new ModelAndView("/user/add");
    }


    @RequestMapping("edit")
    public String edit(Integer id ,Model model){
        FunUsers userInfo = iFunUsers.selectByPrimaryKey(id);

        model.addAttribute("userInfo",userInfo);
        return "user/edit";
    }

    /**
     * 更新用户
     * @param
     * @return
     */
    @RequestMapping("updateUsers")
    public ModelAndView updateUser(FunUsers users) {
        users.setUpdateTime(new Date());
        iFunUsers.updateByPrimaryKey(users);
        ModelAndView mav = new ModelAndView("redirect:/funUser/all");
        return mav;
    }
}

DAO层(我这边可能字段有点多)

package com.uokos.silence.honny.dao;

import jdk.nashorn.internal.runtime.logging.Logger;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;

import java.io.Serializable;
import java.util.Date;

@Data
public class FunUsers implements Serializable {
    private Integer id;

    /**
     * 用户名称
     */
    private String username;

    /**
     * 用户密码
     */
    private String password;

    /**
     *  1男 0女
     */
    private Byte gender;

    /**
     * 最近一次登录时间
     */
    private Date lastLoginTime;

    /**
     * 最近一次登录IP地址
     */
    private String lastLoginIp;

    /**
     * 0 普通用户,1 VIP用户,2 高级VIP用户
     */
    private Byte userLevel;

    /**
     * 用户昵称或网络名称
     */
    private String nickname;

    /**
     * 用户手机号码
     */
    private String mobile;

    /**
     * 用户头像图片
     */
    private String avatar;

    /**
     * 微信登录openid
     */
    private String weixinOpenid;

    /**
     * 微信登录会话KEY
     */
    private String sessionKey;

    /**
     * 0 可用, 1 禁用, 2 注销
     */
    private Byte status;

    /**
     * 创建时间
     */
    private Date addTime;

    /**
     * 更新时间
     */
    private Date updateTime;

    /**
     * 逻辑删除
     */
    private Boolean deleted;

    private static final long serialVersionUID = 1L;
}

}

Mapper

package com.uokos.silence.honny.mapper;

import com.uokos.silence.honny.dao.FunUsers;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;


@Mapper
public interface FunUsersMapper {
    int deleteByPrimaryKey(Integer id);

    int insertSelective(FunUsers record);

    FunUsers selectByPrimaryKey(Integer id);

    int updateByPrimaryKey(FunUsers users);

    List  getAll();
}


}

Service

package com.uokos.silence.honny.service;

import com.uokos.silence.honny.dao.FunUsers;

import java.util.List;


public interface IFunUsersService {

    int deleteByPrimaryKey(Integer id);

    int insertSelective(FunUsers record);

    FunUsers selectByPrimaryKey(Integer id);

    int updateByPrimaryKey(FunUsers users);

    List  getAll();
}

Impl

package com.uokos.silence.honny.service.impl;

import com.uokos.silence.honny.dao.FunUsers;
import com.uokos.silence.honny.mapper.FunUsersMapper;
import com.uokos.silence.honny.service.IFunUsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class FunUsersImpl implements IFunUsersService {
    @Autowired
    private FunUsersMapper funUsersMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return funUsersMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insertSelective(FunUsers record) {
        return funUsersMapper.insertSelective(record);
    }

    @Override
    public FunUsers selectByPrimaryKey(Integer id) {
        return funUsersMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKey(FunUsers users) {
        return funUsersMapper.updateByPrimaryKey(users);
    }


    @Override
    public List getAll() {
        return funUsersMapper.getAll();
    }
}

Mapper.xml

(这边有点多,自己用Mybatis生成器生成就行! 源码在下面)

前端页面

add.html




    
    首页
    


添加页面!

用户名:
密码:
昵称:
手机号:

allUser.html




    
    首页
    


用户查询
姓名 密码 昵称 手机号码 添加时间 修改时间

edit.html



    
    首页
    


修改页面!!!!


用户名:
密码:
昵称:
手机号:

以下是源码:不过配置的是mysql的,需要sqlServer的只需要修改application.properties以及pom.xml的依赖就行

点击下载源码
提取码:5mmd
记得点赞哟!~

你可能感兴趣的:(源码,spring,boot,mysql,mybatis)