MyBatis 的6种增删改查方法分别为:
- 增加(insert):
<insert id="insertUser" parameterType="User">
insert into users(id,name,age) values (#{id},#{name},#{age})
</insert>
使用:
int rows =mapper.insertUser(new User(1, "Bob", 20));
- 删除(delete):
<delete id="deleteUser" parameterType="int">
delete from users where id = #{id}
</delete>
使用:
int rows = mapper.deleteUser(1);
- 修改(update):
<update id="updateUser" parameterType="User">
update users set
name = #{name},age = #{age} where id = #{id}
</update>
使用:
mapper.updateUser(new User(1, "Lisa", 25));
- 查询单行(selectOne):
<select id="selectUser" resultType="User">
select * from users where id = #{id}
</select>
使用:
User user = mapper.selectUser(1);
- 查询多行(selectList):
<select id="selectUsers" resultType="User">
select * from users
</select>
使用:
List<User> users = mapper.selectUsers();
- 查询总数(selectCount):
<select id="selectCount" resultType="int">
select count(id) from users
</select>
使用:
int count = mapper.selectCount();
以上就是 MyBatis 的6种基本的增删改查方法,通过参数类型、ID和返回类型完成对应的增删改查操作。