springboot简单增删改查

springboot简单增删改查

1.前台页面

登录页

springboot简单增删改查_第1张图片

展示页
springboot简单增删改查_第2张图片
新增页

springboot简单增删改查_第3张图片
编辑页

springboot简单增删改查_第4张图片

2.后台代码

application.properties 配置 自动生成仅修改数据库名



# 数据库连接地址
spring.datasource.url=jdbc:mysql://localhost:3306/crud?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8


entity

package com.rugao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Employee {
	private int id;
	private String name;
	private int sex;
	private String position;
	private String department;
}

User

package com.rugao.entity;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
	private int id;
	private String username;
	private String password;
}

Dao

empdao

package com.rugao.dao;

import com.rugao.entity.Employee;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

@Mapper
public interface EmpDao {
	List<Employee> queryAll();

	int deleteById(int id);

	int addEmp(Employee employee);

	int updateEmp(Employee employee);

	Employee queryById(int id);

	List<Employee> queryByName(String name);
}

UserDao

package com.rugao.dao;

import com.rugao.entity.User;
import org.apache.ibatis.annotations.Mapper;

@Mapper
public interface UserDao {
	 User queryUser(String username);
}

Mapper

empMapper


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rugao.dao.EmpDao">
    <select id="queryAll" resultType="com.rugao.entity.Employee">
        select * from employee
    select>

    <delete id="deleteById" parameterType="int">
        delete from employee where id = #{id}
    delete>

    <insert id="addEmp" parameterType="com.rugao.entity.Employee">
        insert into employee (name,sex,position,department) values (#{name},#{sex},#{position},#{department})
    insert>

    <update id="updateEmp" parameterType="com.rugao.entity.Employee">
        update employee set name=#{name},sex=#{sex},position=#{position},department=#{department} where id=#{id}
    update>

    <select id="queryById" resultType="com.rugao.entity.Employee" parameterType="int">
        select * from employee where id = #{id}
    select>

    <select id="queryByName" resultType="com.rugao.entity.Employee" parameterType="String">
        select * from employee where name = #{name}
    select>
mapper>

UserMapper


DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.rugao.dao.UserDao">
    <select id="queryUser" resultType="com.rugao.entity.User">
        select * from user where username=#{username}
    select>
mapper>

service

empService

package com.rugao.service;

import com.rugao.entity.Employee;

import java.util.List;

public interface EmpService {
	List<Employee> queryAll();

	int deleteById(int id);

	int addEmp(Employee employee);

	int updateEmp(Employee employee);

	Employee queryById(int id);

	List<Employee> queryByName(String name);
}

empServiceImpl

package com.rugao.service.impl;

import com.rugao.dao.EmpDao;
import com.rugao.entity.Employee;
import com.rugao.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

	@Autowired
	private EmpDao empDao;

	@Override
	public List<Employee> queryAll() {
		return empDao.queryAll();
	}

	@Override
	public int deleteById(int id) {
		return empDao.deleteById(id);
	}

	@Override
	public int addEmp(Employee employee) {
		return empDao.addEmp(employee);
	}

	@Override
	public int updateEmp(Employee employee) {
		return empDao.updateEmp(employee);
	}

	@Override
	public Employee queryById(int id) {
		return empDao.queryById(id);
	}

	@Override
	public 	List<Employee> queryByName(String name) {
		return empDao.queryByName(name);
	}
}

userService

package com.rugao.service;

import com.rugao.entity.User;

public interface UserService {
	User queryUser(String username);
}

userServiceImpl

package com.rugao.service.impl;

import com.rugao.dao.UserDao;
import com.rugao.entity.User;
import com.rugao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {

	@Autowired
	private UserDao userDao;

	@Override
	public User queryUser(String username) {
		return userDao.queryUser(username);
	}
}

Controller

UserController

package com.rugao.controller;

import com.rugao.entity.User;
import com.rugao.service.UserService;
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 javax.servlet.http.HttpSession;

@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@RequestMapping("/")
	public String tologin(){
		return "login";
	}

	@RequestMapping("/login")
	public String login(String username, String password, Model model, HttpSession session){
		User user = userService.queryUser(username);
		if(user!=null){
			if (password.equals(user.getPassword())){
				session.setAttribute("userInfo",username);
				return "redirect:/showAll";
			}else {
				model.addAttribute("msg","用户名或密码不正确");
				return "login";
			}
		}else {
			model.addAttribute("mes","用户不存在");
			return "login";
		}

	}

	@RequestMapping("/logout")
	public String logout(HttpSession session){
		session.invalidate();
		return "login";
	}
}

empContoller

package com.rugao.controller;

import com.rugao.entity.Employee;
import com.rugao.service.EmpService;
import javafx.scene.control.Alert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.io.Console;
import java.util.List;

@Controller
public class EmpController {

	@Autowired
	private EmpService empService;

	@RequestMapping("/showAll")
	public String show(Model model){
		List<Employee> list = empService.queryAll();
		model.addAttribute("emp",list);
		return "index";
	}

	@RequestMapping("/delete/{id}")
	public String delte(@PathVariable("id") int id){
		empService.deleteById(id);
		return "redirect:/showAll";
	}

	@RequestMapping("/toAdd")
	public String toAdd(){
		return "add";
	}

	@RequestMapping("/add")
	public String AddEmp(Employee employee){
		empService.addEmp(employee);
		return "redirect:/showAll";
	}

	@RequestMapping("/toUpdate/{id}")
	public String toUpdate(@PathVariable("id")int id, Model model){
		Employee employee = empService.queryById(id);
		model.addAttribute("emp",employee);
		return "updateEmp";
	}

	@RequestMapping("/updateEmp")
	public String update(Employee employee){
		empService.updateEmp(employee);
		return "redirect:/showAll";
	}

	@RequestMapping("/queryEmp")
	public String findByName(String name,Model model){
		List<Employee> employee = empService.queryByName(name);
		model.addAttribute("emp",employee);
		return "index";
	}
}

gitee 地址

https://gitee.com/xy1262892971/springboot-crud

你可能感兴趣的:(springboot,spring,boot)