mybatis ppt
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
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>
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 author.name != null"> AND author_name like #{author.name} </when> <otherwise> AND featured = 1 </otherwise> </choose> </select>
<mapper namespace="mapper.MemberMapper"> <select id="selectAll" resultType="domain.Member"> SELECT id, name, tel, address, type FROM members; </select> </mapper>
bind
<select id="selectBlogsLike" parameterType="Blog" resultType="Blog"> <bind name="pattern" value="'%' + _parameter.getTitle() + '%'" /> SELECT * FROM BLOG WHERE title LIKE #{pattern} </select>
<select id="selectUsers" parameterType="int" resultType="User"> select user_id as "id", user_name as "userName", hashed_password as "hashedPassword" from some_table where id = #{id} </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="org.mybatis.spring.sample.mapper.UserMapper" /> <property name="sqlSessionFactory" ref="sqlSessionFactory" /> </bean>
Mapper XML
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
Hello World
• • • • 配置文件 领域对象 Mapper接口和Mapper XML文件 主类
配置文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Configuration 3.0//EN" "http://mybatis.org/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>
<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>
配置文件
配置文件
<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>
映射文件
映射文件
• SELECT – id: 方法名 – parameterType: 参数类型 – resultType: 结果类型 • INSERT, DELETE, UPDATE – id: 方法名 – parameterType: 参数类型
百度文库
<select id="selectUsers" parameterType="int" resultType="com.someapp.model.User"> select id, username, hashedPassword from some_table where id = #{id} </select>
MyBatis基础教程
一个有发展的持久层框架
内容
• • • • • 起步 配置文件 映射文件 动态SQL 整合Spring
起步
起步
• 编程模型 • hello world
编程模型
SqlSessionFactoryBuilder
SqlSessionFactory
SqlSession
Mapper
动态SQL
if
<select id="findActiveBlogWithTitleLike" parameterType="Blog" resultType="Blog"> SELECT * FROM BLOG WHERE state = ‘ACTIVE’ <if test="title != null"> AND title like #{title} </if> </select>
<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>
领域对象
• 略
Mapper 接口
package mapper; import domain.Member; import java.util.List;
public interface MemberMapper {
public List<Member> selectAll(); }
主类
public class Main { public static void main(String [] args) throws Exception { InputStream is = Resources.getResourceAsStream("mybatis.cfg.xml"); SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(is); SqlSession ss = ssf.openSession(); MemberMapper mm = ss.getMapper(MemberMapper.class); List<Member> ls = mm.selectAll(); for (Member m: ls) System.out.println(m.getId() + ":" + m.getName()); ss.close(); } }