mybatis传参类型和方式
主要分享一下mybatis的mapper接口参数类型和传参方式,适用于初学者。
直接上代码:
pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>demo</description>
<url/>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>3.0.3</version>
</dependency>
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
application.yml文件
spring:
datasource:
username: root
password: ******
url: jdbc:mysql:///数据库名
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
实体类:User
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String username;
private String password;
private String email;
}
mapper接口
public interface UserMapper {
/**
* 只有一个参数
* 根据用户id查询用户
* @param id
* @return
*/
User querybyId(Integer id);
/**
* 有两个参数
* 根据用户名和密码查询用户
* @param username
* @param password
* @return
*/
User querybyUsernameAndPassword(@Param("username") String username, @Param("password") String password);
/**
* 有一个对象参数
* 根据用户对象查询用户
* @param user
* @return
*/
User querybyUser(User user);
/**
* 有一个map参数
* 根据map查询用户
* @param maps
* @return
*/
User querybyMap(Map<String,String> maps);
/**
* 有一个list集合参数
* 批量添加
* @param users
*/
void batchInsert(@Param("users") List<User> users);
}
mapper.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.example.demo.mapper.UserMapper">
<!-- 根据用户ID查询用户 -->
<select id="querybyId" resultType="com.example.demo.domain.User">
SELECT * FROM user WHERE id = #{id}
</select>
<!-- 根据用户名和密码查询用户 -->
<select id="querybyUsernameAndPassword" resultType="com.example.demo.domain.User">
SELECT * FROM user WHERE username = #{username} AND password = #{password}
</select>
<!-- 根据用户对象查询用户 -->
<select id="querybyUser" resultType="com.example.demo.domain.User">
SELECT * FROM user WHERE username = #{username} AND password = #{password}
</select>
<!-- 根据map查询用户 -->
<select id="querybyMap" resultType="com.example.demo.domain.User">
SELECT * FROM user
<where>
<if test="'username' !=null">
AND username = #{username}
</if>
<if test="'password' !=null">
AND password = #{password}
</if>
</where>
</select>
<!-- 批量添加用户 -->
<insert id="batchInsert">
INSERT INTO user (username, password,email) VALUES
<foreach item="user" index="index" collection="users" open="(" separator="),(" close=")">
#{user.username}, #{user.password}, #{user.email}
</foreach>
</insert>
</mapper>
测试
@SpringBootTest
class DemoApplicationTests {
@Autowired
private UserMapper userMapper;
@Test
void contextLoads() {
System.out.println(userMapper.querybyId(1));
}
@Test
void querybyUsernameAndPassword() {
System.out.println(userMapper.querybyUsernameAndPassword("tom", "123"));
}
@Test
void querybyUser() {
System.out.println(userMapper.querybyUser(new User(null, "tom", "123", null)));
}
@Test
void querybyMap() {
System.out.println(userMapper.querybyMap(new HashMap<String, String>(){{
put("username","tom"); put("password","123");
}}));
}
@Test
void batchInsert() {
userMapper.batchInsert(new ArrayList<User>(){{
add(new User(null,"tom3","123","[email protected]"));
add(new User(null,"tom4","123","[email protected]"));
}});
}
}
完结,撒花! 希望对各位初学者有帮助,感觉点赞关注。
标签:传参,username,入门,spring,boot,user,mybatis,password,User From: https://blog.csdn.net/QQ903275718/article/details/142916758