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