当前位置:文档之家› mybatis ppt

mybatis ppt


choose, when, otherwise
<select id="findActiveBlogLike" parameterType="Blog" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <choose> <when test="title != null"> AND title like #{title} </when> <when test="author != null and != null"> AND author_name like #{} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
整合Spring
整合Spring
• jar文件 • spring.xml
jar文件
• mybatis-spring-x.xx.jar
spring.xml
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> </bean> <bean id="userMapper" class="org.mybatis.spring.mapper.MapperFactoryBean"> <property name="mapperInterface" value="erMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
Hello World
• • • • 配置文件 领域对象 Mapper接口和Mapper XML文件 主类
配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-////DTD Configuration 3.0//EN" "/dtd/mybatis-3config.dtd"> <configuration> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"></transactionManager> <dataSource type="POOLED"> <property name="driver" value="org.mariadb.jdbc.Driver" /> <property name="url" value="jdbc:mysql://localhost:3306/dbtest" /> <property name="username" value="root" /> <property name="password" value="root" /> </dataSource> </environment> </environments> <mappers> <mapper resource="mapper/MemberMapper.xml" /> </mappers> </configuration>
bind
<select id="selectBlogsLike" parameterType="Blog" resultType="Blog"> <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>
MyBatis基础教程
一个有发展的持久层框架
内容
• • • • • 起步 配置文件 映射文件 动态SQL 整合Spring
起步
起步
• 编程模型 • hello world
编程模型
SqlSessionFactoryBuilder
SqlSessionFactory
SqlSession
Mapper
<insert id="insertAuthor" parameterType="domain.blog.Author"> <selectKey keyProperty="id" resultType="int" order="BEFORE"> select CAST(RANDOM()*1000000 as INTEGER) a from SYSIBM.SYSDUMMY1 </selectKey> insert into Author (id, username, password, email,bio, favourite_section) values (#{id}, #{username}, #{password}, #{email}, #{bio}, #{favouriteSection,jdbcType=VARCHAR}) </insert>
映射文件
映射文件
• SELECT – id: 方法名 – parameterType: 参数类型 – resultType: 结果类型 • INSERT, DELETE, UPDATE – id: 方法名 – parameterType: 参数类型
<select id="selectUsers" parameterType="int" resultType="er"> select id, username, hashedPassword from some_table where id = #{id} </select>
<insert id="insertAuthor" parameterType="domain.blog.Author"> insert into Author (id,username,password,email,bio) values (#{id},#{username},#{password},#{email},#{bio}) </insert> <update id="updateAuthor" parameterType="domain.blog.Author"> update Author set username = #{username}, password = #{password}, email = #{email}, bio = #{bio} where id = #{id} </update> <delete id="deleteAuthor" parameterType="int"> delete from Author where id = #{id} </delete>
foreach
<select id="selectPostIn" resultType="domain.blog.Post"> SELECT * FROM POST P WHERE ID in <foreach item="item" index="index" collection="list" open="(" separator="," close=")"> #{item} </foreach> </select>
配置文件
配置文件
<configuration> <typeAliases> --类型别名 <typeAlias alias="Member" type="domain.Member" /> --类型别名定义 ... </typeAliases> <environments default="development"> --环境以及默认环境 <environment id="development"> --环境定义 <transactionManager type="JDBC"></transactionManager> --事务管理器 <dataSource type="POOLED"> --数据源定义 <property name="driver" value="org.mariadb.jdbc.Driver" /> --数据源属性 ... </dataSource> </environment> ... </environments> <mappers> --映射 <mapper resource="mapper/MemberMapper.xml" /> --映射文件 ... </mappers> </configuration>
相关主题