基于框架实现的增删改查业务

1.创建数据库

create table tb_user(
  id int primary key auto_increment,
  name varchar(32) comment '姓名',
  username varchar(32)  comment '登录名',
  password varchar(32)  comment '登录密码',
  gender varchar(2)  comment '性别: 1-男;2-女',
  birth date  comment '生日'
);

2.导入的依赖

parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.0</version>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>boot_vue</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>boot_vue</name>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.18.24</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3.application.yml配置文件

spring:
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/springboot?serverTimezone=Asia/Shanghai
    username: root
    password: 123456
mybatis:
  type-aliases-package: com.lay.pojo

4.创建User实体类

/**
 * 实体
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class User implements Serializable {
    private Integer id;
    private String name;
    private String username;
    private String password;
    private String gender;
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @JsonFormat(pattern = "yyyy-MM-dd") // 使用jackson转换日期数据时,自定义格式
    private Date birth;
}

5.mapper操作数据库接口

import java.util.List;
@Mapper
@Component
public interface UserMapper {
    /**
     * 用户登录
     * @param username
     * @return
     */
    User loginUser(String username);

    /**
     * 用户注册
     * @param user
     * @return
     */
    int registerUser(User user);

    /**
     * 分页查询
     * @return
     */
    List<User> selectPageUser(@Param("start") Integer start,@Param("limit") Integer limit);

    /**
     * 统计总数
     * @return
     */
    int selectTotal();


    /**
     * 根据用户id查询用户
     * @param id
     * @return
     */
    User selectByIdUser(Integer id);
    /**
     * 添加用户
     * @param user
     * @return
     */
    int insertUser(User user);

    /**
     * 修改用户
     * @param user
     * @return
     */
    int updateUser(User user);

    /**
     * 批量删除
     * @return
     */
    int deleteUser(@Param("ids") Integer[] ids);
}

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.lay.mapper.UserMapper">

    <!--用户登录-->
    <select id="loginUser" resultType="User">
        select id,name,username,password,gender,birth from tb_user where username=#{username}
    </select>
    <!--用户注册-->
    <insert id="registerUser" keyProperty="id" useGeneratedKeys="true">
        insert into tb_user(id,name,username,password,gender,birth)
        values (default,#{name},#{username},#{password},#{gender},#{birth})
    </insert>
    <!--分页查询-->
    <select id="selectPageUser" resultType="User">
        select id,name,username,password,gender,birth from tb_user
        limit #{start},#{limit}
    </select>
    <!--统计总数-->
    <select id="selectTotal" resultType="_int">
        select count(1) from tb_user
    </select>
    <!--根据id查询用户-->
    <select id="selectByIdUser" resultType="User">
        select id,name,username,password,gender,birth from tb_user where id=#{id}
    </select>
    <!--添加用户-->
    <insert id="insertUser" keyProperty="id" useGeneratedKeys="true">
        insert into tb_user(id,name,username,password,gender,birth)
        values (default,#{name},#{username},#{password},#{gender},#{birth})
    </insert>
    <!--修改用户-->
    <update id="updateUser">
        update tb_user set name=#{name},username=#{username},password=#{password},gender=#{gender},birth=#{birth}
        where id=#{id}
    </update>
    <!--批量删除-->
    <delete id="deleteUser">
        delete from tb_user where id in 
        <foreach collection="ids" separator="," open="(" close=")" item="id">
            #{id}
        </foreach>
    </delete>
</mapper>

7.service业务接口

public interface UserService {

    /**
     * 用户登录
     * @param username
     * @return
     */
    User getLoginUser(String username,String password);

    /**
     * 用户注册
     * @param user
     * @return
     */
    int getRegisterUser(User user);

    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    Map<String,Object> getSelectPageUser(Integer page,Integer rows);


    /**
     * 根据用户id查询用户
     * @param id
     * @return
     */
    User getSelectByIdUser(Integer id);
    /**
     * 添加用户
     * @param user
     * @return
     */
    int getInsertUser(User user);

    /**
     * 修改用户
     * @param user
     * @return
     */
    int getUpdateUser(User user);

    /**
     * 批量删除
     * @return
     */
    int getDeleteUser(@Param("ids") Integer[] ids);
}

8.service业务实现类

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
    @Override
    public User getLoginUser(String username, String password) {
        User user = userMapper.loginUser(username);
        //判断用户输入的用户名为空
        if (user==null){
            return null;
        }
        //走到这个判断说明查询的用户已经存在数据库
        //判断用户输入的密码和数据库的密码是否相同
        if (password.equals(user.getPassword())){
            return user;
        }
        //不存在返回空
        return null;
    }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @Override
    public int getRegisterUser(User user) {
        try {
            //注册成功
            return userMapper.registerUser(user);
        }catch (Exception e){
            //注册失败
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    @Override
    public Map<String, Object> getSelectPageUser(Integer page, Integer rows) {

        //分页查询
        List<User> list = userMapper.selectPageUser(((page - 1) * rows), rows);
        //统计总数
        int total = userMapper.selectTotal();
        //定义map集合储存数据
        Map<String,Object> result = new HashMap<>();
        //当前页数
        result.put("page",page);
        //当前行数
        result.put("rows",rows);
        //统计总数
        result.put("total",total);
        //统计list集合
        result.put("list",list);
        return result;
    }

    /**
     * 根据id查询用户
     * @param id
     * @return
     */
    @Override
    public User getSelectByIdUser(Integer id) {
        //根据id查询数据库对应的对象
        User user = userMapper.selectByIdUser(id);
        return user;
    }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @Override
    public int getInsertUser(User user) {
        try {
            //添加成功
           return userMapper.insertUser(user);
        }catch (Exception e){
            //添加失败
            e.printStackTrace();
            return 0;
        }
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @Override
    public int getUpdateUser(User user) {
       try {
           //修改成功
           return userMapper.updateUser(user);
       }catch (Exception e){
           //修改失败
           e.printStackTrace();
           return 0;
       }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @Override
    public int getDeleteUser(Integer[] ids) {
        try {
            //删除成功
            return userMapper.deleteUser(ids);
        }catch (Exception e){
            //删除失败
            e.printStackTrace();
            return 0;
        }
    }
}

9.controller控制器

@Controller
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param username
     * @param password
     * @return
     */
   @RequestMapping("/loginUser")
   public User loginUser(String username, String password, HttpSession session){
       try {
           User user = userService.getLoginUser(username, password);
           session.setAttribute("user",user);
           return user;
       }catch (Exception e){
           return null;
       }
   }

    /**
     * 注册用户
     * @param user
     * @return
     */
    @RequestMapping("/registerUser")
    @ResponseBody
   public Map<String, Object> registerUser(User user){
        Map<String, Object> result = new HashMap<>();
       try {
           userService.getRegisterUser(user);
           result.put("status", 200);
           result.put("msg", "注册成功,请登录");
       } catch (Exception e) {
           e.printStackTrace();
           result.put("status", 500);
           result.put("msg", "服务器忙,请稍后重试");
       }
       return result;
   }

    /**
     * 分页查询
     * @param page
     * @param rows
     * @return
     */
    @RequestMapping("/selectPageUser")
    @ResponseBody
   public Map<String,Object> selectPageUser(Integer page,Integer rows){
       return userService.getSelectPageUser(page,rows);
   }

    /**
     * 添加用户
     * @param user
     * @return
     */
    @RequestMapping("/insertUser")
    @ResponseBody
   public int insertUser(User user){
       try {
           userService.getInsertUser(user);
           return 1;
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }

    /**
     * 修改前根据id查询用户
     * @return
     */
    @RequestMapping("/preSelectByIdUser")
    @ResponseBody
   public User preSelectByIdUser(Integer id){
           return userService.getSelectByIdUser(id);
   }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @RequestMapping("/updateUser")
    @ResponseBody
   public int updateUser(User user){
       try {
           userService.getUpdateUser(user);
           return 1;
       } catch (Exception e) {
           e.printStackTrace();
           return 0;
       }
   }

    /**
     * 批量删除
     * @param ids
     * @return
     */
   @RequestMapping("/deleteUser")
   @ResponseBody
   public Map<String, Object> deleteUser(Integer[] ids){
       Map<String, Object> result = new HashMap<>();
       try {
           userService.getDeleteUser(ids);
           result.put("status", 200);
       } catch (Exception e) {
           e.printStackTrace();
           result.put("status", 500);
       }
       return result;
   }
}

10.springboot启动器

@SpringBootApplication
public class BootVueApplication {

    public static void main(String[] args) {
        SpringApplication.run(BootVueApplication.class, args);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

鲸叫我照顾大海

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

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

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

打赏作者

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

抵扣说明:

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

余额充值