首页 > 其他分享 >mybatis增删改查

mybatis增删改查

时间:2024-06-18 14:32:10浏览次数:8  
标签:String UserLogin 改查 增删 userLogin mybatis import password id

package com.xin.mapper;

import com.xin.pojo.UserLogin;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Repository;

import java.math.BigInteger;
import java.util.List;

@Mapper
@Repository
public interface SelectUpdateMapper {
    //多条件模糊查询
    List<UserLogin> selectLikeSome(@Param("keywords1") String keywords1 ,   @Param("keywords2") String keywords2  );
    //查询所有
    List<UserLogin> queryAll();

    int insert(UserLogin userLogin);

    void deleteById(BigInteger id);

    int updateUserCustomer(UserLogin userLogin);

    UserLogin findAllCertByCerId(@Param("id") String id);


}

SelectUpdateMapper.xml

<?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.xin.mapper.SelectUpdateMapper">

    <resultMap id="BaseResultMap" type="com.xin.pojo.UserLogin">
        <id column="id" jdbcType="BIGINT" property="id" />
        <result column="user_name" jdbcType="VARCHAR" property="username" />
        <result column="u_password" jdbcType="VARCHAR" property="password" />
    </resultMap>

    <select id="selectLikeSome" resultMap="BaseResultMap"   >
        select *
        from userlogin
        where user_name  like  '%' #{keywords1} '%' and  u_password  like  '%' #{keywords2} '%'
    </select>

    <select id="queryAll"   resultMap="BaseResultMap" >
        select *  from userlogin
    </select>

    <update id="updateUserCustomer">
        update userlogin
        <set>
            id=#{id},
            user_name=#{username},
            u_password=#{password}
        </set>
        where id = #{id};
    </update>

    <!--根据id来删除对应的数据-->
    <delete id="deleteById">
        delete
        from usercustomer
        where id = #{id};
    </delete>

    <!--插入一条数据-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true" parameterType="com.xin.pojo.UserLogin">
        insert into userlogin(user_name, u_password)
        values (#{username}, #{password})
    </insert>

    <select id="findAllCertByCerId" resultMap="BaseResultMap">
        select *
        from userlogin
        where id = #{id}
    </select>

</mapper>
package com.xin.world;

import com.xin.mapper.SelectUpdateMapper;
import com.xin.pojo.UserLogin;
import org.junit.jupiter.api.Test;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.math.BigInteger;
import java.util.List;

/**
 * @ClassName
 * @Author
 * @Date
 * @Version 1.0
 **/
@MapperScan("com.xin.mapper")
@SpringBootTest
public class SelectUpdateTest {

    @Autowired
    SelectUpdateMapper selectUpdateMapper;


    //多条件模糊查询
    @Test
    public void selectLikeOne() {
        String name = "阿里";
        String password = "password1";
        List<UserLogin> list = selectUpdateMapper.selectLikeSome(name,password);
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i) + "============多条件模糊查询===============");
        }
    }

    //查询UserLogin
    @Test
    public void toTest() {
        List<UserLogin> userLogins = selectUpdateMapper.queryAll();
        System.out.println("========userLogins=========");
        for (int i = 0; i < userLogins.size(); i++) {
            UserLogin userLogin = userLogins.get(i);
            System.out.println(userLogin);
        }
    }
    //插入一条数据
    @Test
    public void insert() {
        UserLogin userLogin = new UserLogin();
        userLogin.setUsername("zhangsan");
        userLogin.setPassword("33444");
        selectUpdateMapper.insert(userLogin);
    }
    //根据id删除
    @Test
    public void insertUserCustomer4() {
        BigInteger id = new BigInteger("3");
        selectUpdateMapper.deleteById(id);
    }
    //修改UserLogin
    @Test
    public void updateUserCustomer() {
        String id = "4";
        UserLogin allCertByCertId = selectUpdateMapper.findAllCertByCerId(id);
        allCertByCertId.setUsername("marry");
        selectUpdateMapper.updateUserCustomer(allCertByCertId);
    }


}
package com.xin.pojo;

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

import java.util.List;

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

}

标签:String,UserLogin,改查,增删,userLogin,mybatis,import,password,id
From: https://blog.csdn.net/m0_70355065/article/details/139771605

相关文章

  • maven 加载不到 mybatis xml 配置文件
     <build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin>......
  • 浅析Mybatis拦截器
    一、背景最近针对项目中出现的慢sql,我们使用自定义Mybatis拦截器,结合DUCC动态配置慢sql阈值,来监控慢sql并报警,提前发现风险点。借着这个契机,浅析下Mybatis拦截器原理,个人理解,不足之处请指正。二、Mybatis拦截器Mybatis使用plugin来拦截方法调用,所以MyBatisplugin也称为:Mybatis......
  • 【Mybatis】Mybatis快速入门
    MyBatis是一款优秀的持久层框架,用于简化JDBC的开发。MyBatis本是Apache的一个开源项目iBatis,2010年这个项目由apache迁移到了googlecode,并且改名为MyBatis。2013年11月迁移到Github。官网:https://mybatis.org/mybatis-3/zh/index.htmlMybatis入门Mybatis会把数据库执......
  • MyBatisX插件生成代码
    MyBatisX插件MyBatisPlus提供了一个IDEA插件——MybatisX,使用它可根据数据库快速生成Entity、Mapper、Mapper.xml、Service、ServiceImpl等代码,使用户更专注于业务。下面演示具体用法安装插件在IDEA插件市场搜索MyBatisX,进行在线安装配置数据库连接在IDEA中配置数据......
  • MybatisPlus逻辑删除
    逻辑删除就是基于代码逻辑模拟删除效果,但并不会真正删除数据。思路如下:1.在表中添加一个字段标记数据是否被删除2.当删除数据时把标记置为13.查询时只查询标记为0的数据 例如逻辑删除字段为deleted,那么删除操作的sql语句为:UPDATEuserSETdeleted=1WHEREid=1AND......
  • Mybatis框架
    Java中的持久层框架 1.mybatis:最早叫ibatis       开发效率低,执行性能好2.hibernate                开发效率高,执行性能低反射对象.属性=值属性.赋值方法(对象,值)*反射是java进阶的分水岭*不要乱用反射,反射性能......
  • Mybatis-Plus-Join(MPJ连表查询)
    mybatis-plus作为mybatis的增强工具,它的出现极大的简化了开发中的数据库操作,但是长久以来,它的联表查询能力一直被大家所诟病。一旦遇到leftjoin或rightjoin的左右连接,你还是得老老实实的打开xml文件,手写上一大段的sql语句一款叫做mybatis-plus-join的工具(后面就简称mpj了),可以不......
  • 『手写Mybatis』创建简单的映射器代理工厂
    前言在阅读本文之前,我相信你已经是一个MybatisORM框架工具使用的熟练工了,那你是否清楚这个ORM框架是怎么屏蔽我们对数据库操作的细节的?比如我们使用JDBC的时候,需要手动建立数据库链接、编码SQL语句、执行数据库操作、自己封装返回结果等。但在使用ORM框架后,只需要......
  • 『手写Mybatis』实现映射器的注册和使用
    前言如何面对复杂系统的设计?我们可以把Spring、MyBatis、Dubbo这样的大型框架或者一些公司内部的较核心的项目,都可以称为复杂的系统。这样的工程也不在是初学编程手里的玩具项目,没有所谓的CRUD,更多时候要面对的都是对系统分层的结构设计和聚合逻辑功能的实现,再通过层层转换......
  • 带你学习Mybatis之执行器Executor
    执行器ExecutorExecutor定义了数据库操作的基本方法,SqlSession接口中的功能都是基于Executor接口实现的,真正执行java和数据库交互的类,负责维护一级缓存和二级缓存,并提供事务管理的相关操作,会将数据库相关操作委托给StatementHandler完成public enum ExecutorType { ......