增删改查(CRUD)操作:
Select标签是mybatis中最常用的标签之一,通过函数的返回值属性属性可以详细配置每一条SQL语句。
-
id:命名空间中唯一的标识符,接口中的方法名与映射文件中的SQL语句ID 一一对应。
-
parameterType:传入SQL语句的参数类型,参数多时可以多尝试使用Map。
-
resultType:SQL语句返回值类型。
1. SELECT操作:
-
在Mapper中添加对应方法:
public interface Mapper {
//根据id查询用户
User selectUserById(int id);
//根据name查询用户
User selectUserByName(String name);
}
-
在Mapper.xml中添加select语句:
<mapper namespace="com.mybatis.dao.Mapper">
<select id="selectUserById" resultType="com.mybatis.pojo.User">
select * from user where id = #{id}
</select>
<select id="selectUserByName" resultType="com.mybatis.pojo.User">
select * from user where name = #{name}
</select>
</mapper>
-
测试类中测试:
public class MapperTest {
@Test
public void selectUser() {
SqlSession session = MybatisUtils.getSession(); //获取SqlSession连接
Mapper mapper = session.getMapper(Mapper.class);
User user = mapper.selectUserById(3);
User user1 = mapper.selectUserByName("李四");
System.out.println("根据Id查询:"+user);
System.out.println("根据name查询:"+user1);
session.close();
}
}
-
运行结果:
2. INSERT操作:
-
Mapper接口中添加对应的方法:
//插入用户数据
int addUser(User user);
-
Mapper.xml中添加insert语句:
<insert id="addUser" parameterType="com.mybatis.pojo.User">
insert into user (id,name,pwd) values (#{id},#{name},#{pwd})
</insert>
-
测试类中测试:
User user3 = new User(4,"赵六","qwer");
int flag = mapper.addUser(user3);
System.out.println("插入成功: "+flag);
session.commit(); //注意记得提交事务
session.close();
增删改操作是事务型操作,要主动提交事务才会持久化到文件中
-
运行结果:
3. UPDATE操作:
-
Mapper接口中添加对应的方法:
//更新一个用户
int updateUser(User user);
-
Mapper.xml中添加update语句:
<update id="updateUser" parameterType="com.mybatis.pojo.User">
update user set name=#{name},pwd=#{pwd} where id = #{id}
</update>
-
测试类中测试:
User user4 = mapper.selectUserByName("李四");
System.out.println("修改前密码:"+user4.getPwd());
user4.setPwd("654321");
mapper.updateUser(user4);
System.out.println("修改后密码:"+user4.getPwd());
session.commit(); //注意记得提交事务
增删改操作是事务型操作,要主动提交事务才会持久化到文件中
-
运行结果:
4. DELETE操作:
-
Mapper接口中添加对应的方法:
//根据名称删除用户
void deleteUser(String name);
-
Mapper.xml中添加delete语句:
<delete id="deleteUser" parameterType="com.mybatis.pojo.User">
delete from user where name = #{name}
</delete>
-
测试类中测试:
//删除用户”张三“
mapper.deleteUser("张三");
session.commit(); //注意记得提交事务
增删改操作是事务型操作,要主动提交事务才会持久化到文件中
-
运行结果:
5. 模糊查询
-
Mapper接口中添加对应的方法:
//模糊查询
User selectLike(String name);
-
Mapper.xml中添加模糊查询语句:
<select id="selectLike" resultType="com.mybatis.pojo.User">
select * from user where name like "%"#{name}"%"
</select>
注意防止sql注入问题
-
测试类中测试:
//查询姓名中有”王“的用户信息
User user5 = mapper.selectLike("王");
System.out.println("结果为:"+user5);
session.commit(); //注意记得提交事务
增删改操作是事务型操作,要主动提交事务才会持久化到文件中
-
运行结果:
6. Map查询
-
Mapper接口中添加对应的方法:
//Map查询
User selectByMap(Map<String,Object> map);
-
Mapper.xml中添加Map语句:
<select id="selectByMap" parameterType="map" resultType="com.mybatis.pojo.User">
select * from user where name = #{username} and id = #{id}
</select>
-
测试类中测试:
Map<String, Object> map = new HashMap<String, Object>();
map.put("username","王五");
map.put("id","1");
User user6 = mapper.selectByMap(map);
System.out.println(user6);
session.commit(); //注意记得提交事务
增删改操作是事务型操作,要主动提交事务才会持久化到文件中
-
运行结果:
7. 总结
-
所有的增删改操作都需要提交事务!
-
接口所有的普通参数,尽量都写上@Param参数,多个参数时,必须写上!
-
参数较多时,可以考虑使用map传递参数!
-
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)