1、namespace
namespace中的包名要和Dao/mapper接口的包名一致!
2、 select
选择,查询语句;
- id:就是对应的namespace中的方法名;
- resultType: Sql语句执行的返回类型!
- parameterType:参数类型!
1.编写接口
?
1
2
|
//根据id查询用户
User getUserById( int id);
|
2.编写对应的mapper.xml中的sql语句
?
1
2
3
|
< select id= "getUserById" parameterType= "int" resultType= "com.kuang.pojo.User" >
|
3.测试
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
@Test
public void getUserLike(){
SqlSession sqlSession = MybatisUtils.getSqlSession();
UserMapper mapper = sqlSession.getMapper(UserMapper. class );
List<User> userList = mapper.getUserLike( "李" );
for (User user : userList) {
System.out.println(user);
}
sqlSession.close();
}
|
3、Insert
?
1
2
3
4
|
<! --对象中的属性,可以直接取出来-->
< insert id= "addUser" parameterType= "com.kuang.pojo.User" >
insert into mybatis. user (id, name ,pwd) values (#{id},#{ name },#{pwd});
</ insert >
|
4、update
?
1
2
3
|
< update id= "updateUser" parameterType= "com.kuang.pojo.User" >
update mybatis. user set name = #{ name },pwd=#{pwd} where id = #{id};
</ update >
|
5、Delete
?
1
2
3
|
< delete id= "deleteUser" parameterType= "int" >
delete from mybatis. user where id = #{id};
</ delete >
|
注意点:
增删改需要提交事务(sqlSession.commit())
6、分析增删改查会遇到的错误
- 标签不要匹配错
- resource绑定mapper,需要使用路径
- 程序配置文件必须符合规范
- NullPointerException,没有注册到资源!
- 输出的xml文件中存在中文乱码问题!
- maven资源没有导出问题
到此这篇关于详解Mybatis中的CRUD的文章就介绍到这了,更多相关Mybatis的CRUD内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!
原文链接:https://www.cnblogs.com/laiyw/archive/2021/03/07/14495847.html
相关文章
猜你喜欢
- 个人服务器网站搭建:如何选择合适的服务器提供商? 2025-06-10
- ASP.NET自助建站系统中如何实现多语言支持? 2025-06-10
- 64M VPS建站:如何选择最适合的网站建设平台? 2025-06-10
- ASP.NET本地开发时常见的配置错误及解决方法? 2025-06-10
- ASP.NET自助建站系统的数据库备份与恢复操作指南 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
2025-05-29 53
-
2025-05-27 70
-
2025-05-29 59
-
2025-05-25 92
-
2025-05-29 37
热门评论