目录
1.依赖
在pom.xml文件中引入依赖
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter-test</artifactId>
<version>3.0.3</version>
<scope>test</scope>
</dependency>
2.示例代码
提示:在resource下创建文件mapper,用于存放xml配置文件
2.1不带resultMap标签示例
dao接口:
package com.example.springbootdemo.Mapper;
import com.example.springbootdemo.pojo.Student;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import java.time.LocalDate;
import java.util.List;
@Mapper
public interface StudentMapper{
@Insert("insert into td_student(username, password, name, gender, age,create_time, update_time) "+
"values(#{username},#{password},#{name},#{gender},#{age},#{createTime},#{updateTime})")
void insert(Student student);
void delete(List<Integer> ids);
void update(Student student);
List<Student> list(String name, Short gender, LocalDate begin, LocalDate end);
@Select("select * from td_student where id = #{id}")
Student getById(Integer id);
@Select("select * from td_student where username=#{username}")
Student getByUserName(String username);
}
对应的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.springbootdemo.Mapper.StudentMapper">
<update id="update">
update td_student
<set>
<if test="username != null and username != ''">
username = #{username},
</if>
<if test="password != null and password != ''">
password = #{password},
</if>
<if test="gender != null">
gender = #{gender},
</if>
<if test="age != null">
age = #{age},
</if>
<if test="name != null and name != ''">
name = #{name},
</if>
<if test="updateTime != null">
update_time = #{updateTime},
</if>
</set>
where id = #{id}
</update>
<delete id="delete">
delete from td_student where id in
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</delete>
<select id="list" resultType="com.example.springbootdemo.pojo.Student">
select * from td_student
<where>
<if test="name != null and name != ''">
name like concat('%',#{name},'%')
</if>
<if test="gender != null">
and gender = #{gender}
</if>
<if test="begin != null and end != null">
and entrydate = between #{begin} and #{end}
</if>
</where>
order by update_time desc
</select>
</mapper>
当然还有这种加resultMap标签的情况:
2.1带resultMap标签示例
<?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.dao.UserDao">
<!-- 定义User的结果映射 -->
<resultMap id="UserResultMap" type="com.example.model.User">
<id property="id" column="id" />
<result property="name" column="name" />
<result property="email" column="email" />
</resultMap>
<!-- 插入用户 -->
<insert id="insertUser" parameterType="com.example.model.User">
INSERT INTO user (name, email) VALUES (#{name}, #{email})
</insert>
<!-- 根据ID查询用户 -->
<select id="getUserById" parameterType="java.lang.Integer" resultType="com.example.model.User">
SELECT * FROM user WHERE id = #{id}
</select>
<!-- 查询所有用户 -->
<select id="getAllUsers" resultMap="UserResultMap">
SELECT * FROM user
</select>
<!-- 更新用户 -->
<update id="updateUser" parameterType="com.example.model.User">
UPDATE user SET name = #{name}, email = #{email} WHERE id = #{id}
</update>
<!-- 删除用户 -->
<delete id="deleteUser" parameterType="java.lang.Integer">
DELETE FROM user WHERE id = #{id}
</delete>
</mapper>
3.resultMap标签不加的情况说明
这取决于你的具体需求。<resultMap>
在 MyBatis 中主要用于处理复杂的映射情况,比如联合查询、嵌套结果等。对于简单的 CRUD 操作,如果不涉及复杂的映射关系,通常不需要显式定义 <resultMap>
。
对于你的 User
实体类,如果它的属性名和数据库表中的列名完全一致,并且你不做特殊的映射处理(比如将数据库中的某个列映射到 Java 对象的另一个属性),那么你可以直接使用 resultType
来指定返回结果的类型,而不需要定义 <resultMap>
。
resultType
告诉 MyBatis 返回的结果应该被映射到 com.example.model.User
类型的对象上。MyBatis 会根据 SQL 查询返回的列名自动匹配 User
类中的属性名,并将相应的值赋给属性。
然而,如果你需要处理更复杂的场景,比如列名和属性名不匹配,或者需要映射到嵌套的对象,那么你就需要定义 <resultMap>
来进行详细的映射配置。