SpringBoot+JPA+EasyUI+MySQL+thymeleaf 基本 CURD

一、前言

一直在探索自己最喜欢(最简单)的 CRUD+分页,前面写过很多了,代码基本都是由前面进化而来的,而且也是越来越简单,JS 代码也是越来越规范简洁

1、Servlet+EasyUI+MySQL

2、Spring+SpringMVC+MyBatis+pageHelper

下面来个

3、SpringBoot+EasyUI+MySQL

二、CRUD功能演示

1、分页

这里写图片描述

2、增加

这里写图片描述

3、查找

这里写图片描述

4、修改

这里写图片描述

5、删除

这里写图片描述

三、代码实现

1、代码关系结构:

由于比较简单

①entity、②dao、③service、④controller 四层中省略了 ③service 层

这里写图片描述

2、entity 层
package com.cun.bean;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name = "t_user")
public class User {
   
   

	@Id
	@GeneratedValue
	private Integer id;

	@Column(length = 50)
	private String name;
	
	@Column(length = 50)
	private String word;
	
	public Integer getId() {
   
   
		return id;
	}

	public void setId(Integer id) {
   
   
		this.id = id;
	}

	public String getName() {
   
   
		return name;
	}

	public void setName(String name) {
   
   
		this.name = name;
	}

	public String getWord() {
   
   
		return word;
	}

	public void setWord(String word) {
   
   
		this.word = word;
	}
}

3、dao 层
package com.cun.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;

import com.cun.bean.User;

/**
 * 继承后面的接口,只是为了能进行搜索
 * @author linhongcun
 *
 */
public interface UserDao extends JpaRepository<User, Integer>,JpaSpecificationExecutor<User>{
   
   
	

}

4、controller 层
package com.cun.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cun.bean.User;
import com.cun.dao.UserDao;
import com.cun.util.StringUtil;

@Controller
@RequestMapping("/user")
public class UserController {
   
   

	@Autowired
	private UserDao userDao;// 由于比较简单,省略了 service 层

	/**
	 * 4、删除
	 * @param ids
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Map<String, Object> delete(@RequestParam(value = "ids") String ids) {
   
   
		Map<String, Object> resultMap = new HashMap<String, Object>();
		String[] idsStr = ids.split(",");
		//for (int i = 0; i < ids.length; i++) { 
		for (int i = 0; i < idsStr.length(); i++) {
   
    // 2018.7.25 根据网友意见做出正确修改
			userDao.delete(Integer.parseInt(idsStr[i]));
		}
		resultMap.put("success", true);

		return resultMap;
	}

	/**
	 * 3、添加、修改
	 * @param user
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/save")
	public Map<String, Object> save(User user) {
   
   
		Map<String, Object> resultMap = new HashMap<String, Object>();
		userDao.save(user);
		resultMap.put("success", true);
		return resultMap;
	}

	/**
	 * 2、显示
	 * @param page
	 * @param rows
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/list") 
	public Map<String, Object> list(User user, @RequestParam(value = "page", required = false) Integer page,
			@RequestParam(value = "rows", required = false) Integer rows) {
   
   
		Pageable pageable = new PageRequest(page-1
评论 19
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

IT小村

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

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

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

打赏作者

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

抵扣说明:

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

余额充值