mybatis 配置说明和类结构图
2013-03-05 08:21
295 查看
配置数据源
数据源配置
<configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> </configuration>
相关配置配置文件
<mappers> <mapper resource="org/mybatis/builder/AuthorMapper.xml"/> <mapper resource="org/mybatis/builder/BlogMapper.xml"/> <mapper resource="org/mybatis/builder/PostMapper.xml"/> </mappers> // Using url fully qualified paths <mappers> <mapper url="file:///var/sqlmaps/AuthorMapper.xml"/> <mapper url="file:///var/sqlmaps/BlogMapper.xml"/> <mapper url="file:///var/sqlmaps/PostMapper.xml"/> </mappers>
SQL映射配置
SQL 映射文件结构: cache - 配置给定命名空间的缓存。 cache-ref – 从其他命名空间引用缓存配置。 resultMap – 最复杂,也是最有力量的元素,用来描述如何从数据库结果集中来加载你的对象。 parameterMap – 已经被废弃了!老式风格的参数映射。内联参数是首选,这个元素可能在将来被移除。这里不会记录。 sql – 可以重用的 SQL 块,也可以被其他语句引用。 insert – 映射插入语句 update – 映射更新语句 delete – 映射删除语句 select – 映射查询语句
Sql Select配置
Select <mapper namespace="org.mybatis.example.BlogMapper"> <select id="selectBlog" parameterType="int" resultType="Blog"> select * from Blog where id = #{id} </select> </mapper> 第一种使用完全限定名调用映射语句 Blog blog = (Blog) session.selectOne( "org.mybatis.example.BlogMapper.selectBlog", 101); 第二种采用映射接口调用映射语句 BlogMapper mapper = session.getMapper(BlogMapper.class); Blog blog = mapper.selectBlog(101);
Sql insert配置
如果 Author 表已经对 id 使用了自动生成的列类型 <insert id="insertAuthor" parameterType="domain.blog.Author" useGeneratedKeys=”true” keyProperty=”id”> insert into Author (username,password,email,bio) values (#{username},#{password},#{email},#{bio}) </insert> 如果Author表主键生成策略采用OID、Sequence <insert id="insertAuthor" parameterType="domain.blog.Author"> <selectKey resultType="Long" keyProperty="id" order="BEFORE"> select nextval FOR AUTHOR_SEQ from SYSIBM.SYSDUMMY1 </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR} ) </insert>
Sql update和Delete配置
Update <update id="updateAuthor" parameterType="domain.b update Author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} </update> Delete <delete id="deleteAuthor” parameterType="int"> delete from Author where id = #{id} </delete>
动态sql
示例一
<select id=“findActiveBlogLike” parameterType=”Blog” resultType=“Blog”> SELECT * FROM BLOG WHERE state = “ACTIVE” <if test=”title != null”> AND title like #{title} </if> <if test=”author != null and author.name != null”> AND title like #{author.name} </if> </select>
实例二
<select id=”findActiveBlogLike” parameterType=”Blog” resultType=”Blog”> SELECT * FROM BLOG <where> <if test=”state != null”> state = #{state} </if> <if test=”title != null”> AND title like #{title} </if> </where> </select>
缓存配置
MyBatis默认情况下是没有开启缓存的,除了局部的 session 缓存。要开启二级缓存,你需要在你的 SQL映射文件中添加一行:<cache/>
缓存示例
<cache eviction="FIFO" flushInterval="60000" size="512" readOnly="true"/>
这个配置创建了一个 FIFO 缓存,并每隔 60 秒刷新,存取512 个结果对象或列表的引用,而且返回的对象为只读,因此在不同线程中的调用者之间修改它们会导致冲突。
常用cache类图:
自定义cache配置
自定义Cache <cache type=”com.domain.something.MyCustomCache”> <property name=”cacheFile” value=”/tmp/my-custom-cache.tmp”/> </cache> Ehcache <mapper namespace="org.acme.FooMapper"> <cache type="org.mybatis.ehcache.EhcacheCache"/> ... </mapper> OSCache <mapper namespace="org.acme.FooMapper"> <cache type="org.mybatis.oscache.OSCache"/> ... </mapper>
整合spring
配置<bean id="sqlSessionFactory" class="org.springframework.orm.ibatis3.SqlSessionFactoryBean"> <property name="configLocation" value="classpath:SqlMapConfig.xml" /> <property name="dataSource" ref="dataSource" /> </bean>
假设类 UserMapperImpl是 SqlSessionDaoSupport的子类,它可以在 Spring 中进行如下的配置:
<bean id="userMapper" class="org.mybatis.spring.sample.mapper.UserMapperImpl"> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
mybatis类图
相关文章推荐
- JForum框架结构和主要配置文件说明
- MyBatis的配置文件的层次结构
- 关于MyBatis的配置文件SqlMapConfig.xml的标签说明
- Spring学习5_ Spring MVC与mybatis学习-----参数配置文件说明
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- spring+mybatis+Atomikos JTA事务配置说明
- BSP驱动文件结构及配置说明-ing
- Mybatis Generator配置说明
- MyBatis 配置文件基本结构
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- mybatis全局配置文件说明
- spring+mybatis+Atomikos JTA事务配置说明
- Tomcat目录结构及配置文件说明
- spring+mybatis+Atomikos JTA事务配置说明
- Spring MVC与mybatis学习-----参数配置文件说明
- Atitit.mybatis的测试 以及spring与mybatis在本项目中的集成配置说明
- tomcat目录结构及配置文件说明
- 【Puppet in action】 学习笔记之Puppet目录结构及配置文件说明
- tomcat目录结构及配置文件说明
- spring+mybatis+Atomikos JTA事务配置说明