mybatis映射XML文件的示例分析
这篇文章主要介绍mybatis映射XML文件的示例分析,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
mybatis映射XML文件
一个简单的映射文件:
当然这个文件中没有任何的元素
The Mapper XML files have only a few first class elements :
cache - Configuration of the cache for a given namespace.
cache-ref - Reference to a cache configuration from another namespace.
resultMap - The most complicated and powerful element that describes how to load your objects from the database result sets.
sql - A reusable chunk of SQL that can be referenced by other statements.
insert - A mapped INSERT statement.
update - A mapped UPDATE statement.
delete - A mapped DELETE statement.
select - A mapped SELECT statement.
select
简单的例子:
select也有很多属性可以让你配置:
insert, update and delete
语句:
insert into Author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) update Author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} delete from Author where id = #{id}
f your database supports auto-generated key fields (e.g. MySQL and SQL Server),上面的插入语句可以写成:
insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio})
如果你的数据库还支持多条记录插入,可以使用下面这个语句:
insert into Author (username, password, email, bio) values (#{item.username}, #{item.password}, #{item.email}, #{item.bio})
sql
这个element可以定义一些sql代码的碎片,然后在多个语句中使用,降低耦合。比如:
${alias}.id,${alias}.username,${alias}.password
然后在下面的语句中使用:
Result Maps
官网给了个最最复杂的例子
大体意思呢就是一个博客系统有一个作者,很多博文,博文中有一个作者,很多评论,很多标签(包括了一对多,一对一)
以上是"mybatis映射XML文件的示例分析"这篇文章的所有内容,感谢各位的阅读!希望分享的内容对大家有帮助,更多相关知识,欢迎关注行业资讯频道!