springboot篇】七. springboot中的crud

基于【springboot篇】六. springboot中的事务控制

springboot中的crud

项目准备
  1. 创建项目day64-springboot-05-crud
  2. 项目配置参考上一章
  3. 新建数据库day64-springboot-crud
    DROP TABLE IF EXISTS `user`;
    CREATE TABLE `user` (
      `id` int(8) NOT NULL AUTO_INCREMENT,
      `name` varchar(20) DEFAULT NULL,
      `pwd` varchar(20) DEFAULT NULL,
      `age` int(3) DEFAULT NULL,
      `money` double(8,2) DEFAULT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
    
    INSERT INTO `user` VALUES ('1', 'wpj', '123456', '23', '10000.00');
    INSERT INTO `user` VALUES ('2', 'czh', '123456', '22', '10000.00');
    
  4. 创建实体类User
    package com.wpj.bean;
    
    import com.baomidou.mybatisplus.annotations.TableId;
    import com.baomidou.mybatisplus.annotations.TableName;
    import com.baomidou.mybatisplus.enums.IdType;
    import lombok.Data;
    
    @Data
    @TableName("user")
    public class User {
    
        @TableId(type = IdType.AUTO)
        private Integer id;
    
        private String name;
    
        private String pwd;
    
        private Integer age;
        
        private Double money;
    
    }
    
  5. 创建mapper接口和service及其impl
    package com.wpj.mapper;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.wpj.bean.User;
    
    public interface IUserMapper extends BaseMapper<User> {
    
    }
    
    package com.wpj.service;
    
    import com.wpj.baseservice.IBaseService;
    import com.wpj.bean.User;
    
    public interface IUserService extends IBaseService<User> {
    
    }
    
    package com.wpj.service.impl;
    
    import com.baomidou.mybatisplus.mapper.BaseMapper;
    import com.baomidou.mybatisplus.plugins.Page;
    import com.wpj.baseservice.impl.BaseServiceImpl;
    import com.wpj.bean.User;
    import com.wpj.mapper.IUserMapper;
    import com.wpj.service.IUserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    
    @Service
    public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    
        @Autowired
        private IUserMapper iUserMapper;
    
        @Override
        protected BaseMapper<User> getBaseMapper() {
            return iUserMapper;
        }
    }
    
  6. 创建mapper文件(也接口同名)
    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE mapper
            PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
            "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
    
    <mapper namespace="com.wpj.mapper.IUserMapper">
        
        
    </mapper>
    
  7. application.properties修改为application.yml
    spring:
      datasource:
        driver-class-name: com.mysql.jdbc.Driver
        url: jdbc:mysql://localhost:3306/day64-springboot-crud
        username: root
        password: 123456
    
    mybatis-plus:
      type-aliases-package: com.wpj.bean
      mapper-locations: classpath:mapper/*.xml
    
  8. 开启包扫描
    package com.wpj.day64springboot05crud;
    
    import org.mybatis.spring.annotation.MapperScan;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @EnableTransactionManagement // 开启事务
    @SpringBootApplication(scanBasePackages = "com.wpj")
    @MapperScan(basePackages = "com.wpj.mapper")
    public class Day64Springboot05CrudApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(Day64Springboot05CrudApplication.class, args);
        }
    
    }
    
  9. 测试
    在这里插入图片描述
    10.测试环境完成。。。。。。。开始做增删改查。

1 显示数据库所有信息

1.1 写controller

package com.wpj.web.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.wpj.bean.User;
import com.wpj.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class UserController {

    @Autowired
    private IUserService iUserService;

    @RequestMapping("/getPage")
    public String getPage(Page<User> page, ModelMap map){
        iUserService.getPage(page);
        return "userList";
    }
}

1.2 写userList页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <base th:href="${#request.getContextPath()+'/'}">
</head>
<body>
    <table>
        <tr>
            <td>ID</td>
            <td>name</td>
            <td>pwd</td>
            <td>age</td>
            <td>money</td>
            <td>operator</td>
        </tr>
        <tr th:each="user:${page.records}">
            <td th:text="${user.id}"/>
            <td th:text="${user.name}"/>
            <td th:text="${user.pwd}"/>
            <td th:text="${user.age}"/>
            <td th:text="${user.money}"/>
            <td>
                <a href="#">编辑</a>
                <a href="#">删除</a>
            </td>
        </tr>
    </table>
</body>
</html>

1.3 运行Day64Springboot05CrudApplication启动tomcat,访问

在这里插入图片描述

2. 编辑数据

2.1 写controller

2.2 修改html中的a标签

3. 删除数据

3.1 写controller

@RequestMapping(value = "/deleteUser/{id}")
    public String deleteUser(@PathVariable Integer id){
        iUserService.delete(id);
        return "redirect:../getPage";
    }

3.2 修改html中的a标签

	<a th:href="|deleteUser/${user.id}|">删除</a>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值