• java使用mapper操作mysql


    上半部分搭建springboot 简单使用数据库查询
    添加链接描述

    在impl接口实现操作

    package com.service.impl;
    import com.dao.UserMapper;
    import com.pojo.User;
    import com.service.UserService;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import java.util.List;
    @Service
    public class UserServiceImpl implements UserService {
        @Autowired
        private UserMapper userMapper;
        @Override
        public List dade() {
            return userMapper.selectAll();
        }
        @Override
        public User oneUser(Long id) {
            return userMapper.selectByPrimaryKey(id);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    1、selectAll查所有

    	@Override
        public List dade() {
            return userMapper.selectAll();
        }
    
    • 1
    • 2
    • 3
    • 4

    2、selectByPrimaryKey根据id查询

        @Override
        public User oneUser(Long id) {
            return userMapper.selectByPrimaryKey(id);
        }
    
    • 1
    • 2
    • 3
    • 4

    3、insert保存

        @Override
        public User oneUser(Long id) {
            User user = new User();
            user.setAge((long) 18);
            user.setName("dade");
            user.setPhone("14796178000");
            user.setDate("2023-12-23");
            userMapper.insert(user);
            return userMapper.selectByPrimaryKey(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4、updateByPrimaryKey修改

        @Override
        public User oneUser(Long id) {
            User user = userMapper.selectByPrimaryKey(id);
            user.setName("大得");
            userMapper.updateByPrimaryKey(user);
            return userMapper.selectByPrimaryKey(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    5、deleteByPrimaryKey删除

        @Override
        public User oneUser(Long id) {
            userMapper.deleteByPrimaryKey(2);
            return userMapper.selectByPrimaryKey(id);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    6、selectByExample多条件查询

    @Override
        public List oneUser(Long id) {
            Example example=new Example(User.class);
            Example.Criteria criteria = example.createCriteria();
            //根据id查询
            criteria.andEqualTo("id", 3);
            //模糊查询
            criteria.andLike("name","%d%");
            return userMapper.selectByExample(example);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
  • 相关阅读:
    【深度学习实验】卷积神经网络(八):使用深度残差神经网络ResNet完成图片多分类任务
    git安装
    C++中的##、#符号含义
    从0到一配置单节点zookeeper
    git学习笔记
    使用 Python 的高效相机流
    Java编程简便技巧篇(二)——字母和数字之间的转换
    JSP 空教室查询管理系统yeclipse开发mysql数据库bs框架java编程jdbc详细设计
    CentOS 常见命令详解
    java 整合 swagger-ui 步骤
  • 原文地址:https://blog.csdn.net/qq_34631220/article/details/136491297