b'\n_START_ARTICLE_\nIBATIS\n_START_SECTION_\nPou\xc5\xbeit\xc3\xad v Jav\xc4\x9b\n_START_PARAGRAPH_\nKa\xc5\xbed\xc3\xa1 aplikace pou\xc5\xbe\xc3\xadvaj\xc3\xadc\xc3\xad iBATIS se to\xc4\x8d\xc3\xad okolo instance t\xc5\x99\xc3\xaddy SqlSessionFactory. Tato instance m\xc5\xaf\xc5\xbee b\xc3\xbdt z\xc3\xadsk\xc3\xa1na pomoc\xc3\xad t\xc5\x99\xc3\xaddy SqlSessionFactoryBuilder, kter\xc3\xa1 je schopn\xc3\xa1 po\xc5\xbeadovanou t\xc5\x99\xc3\xaddu vytvo\xc5\x99it na z\xc3\xa1klad\xc4\x9b XML konfigura\xc4\x8dn\xc3\xadho souboru nebo p\xc5\x99edp\xc5\x99ipraven\xc3\xa9 instance t\xc5\x99\xc3\xaddy Configuration.\n_START_SECTION_\nVytvo\xc5\x99en\xc3\xad SqlSessionFactory z XML souboru\n_START_PARAGRAPH_\nVytvo\xc5\x99en\xc3\xad instance SqlSessionFactory z XML souboru je velmi jednoduch\xc3\xa9. iBATIS obsahuje pomocnou t\xc5\x99\xc3\xaddu Resource, kter\xc3\xa1 zjednodu\xc5\xa1uje na\xc4\x8d\xc3\xadt\xc3\xa1n\xc3\xad konfigura\xc4\x8dn\xc3\xadch soubor\xc5\xaf z classpath a jin\xc3\xbdch m\xc3\xadst._NEWLINE_String resource = "org/apache/ibatis/example/Configuration.xml";_NEWLINE_Reader reader = Resources.getResourceAsReader(resource);_NEWLINE_sqlMapper = new SqlSessionFactoryBuilder().build(reader);_NEWLINE_Konfigura\xc4\x8dn\xc3\xad XML soubor obsahuje nastaven\xc3\xad pro iBATIS framework, v\xc4\x8detn\xc4\x9b nastaven\xc3\xad pot\xc5\x99ebn\xc3\xa9ho pro p\xc5\x99ipojen\xc3\xad k datab\xc3\xa1zi a nastaven\xc3\xad transakc\xc3\xad. Pln\xc3\xbd v\xc3\xbd\xc4\x8det mo\xc5\xbenost\xc3\xad je k naleznut\xc3\xad v u\xc5\xbeivatelsk\xc3\xa9 dokumentaci, jednoduch\xc3\xbd p\xc5\x99\xc3\xadklad vypad\xc3\xa1 takto:_NEWLINE_<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE configuration  PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"  "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> _NEWLINE_<configuration>_NEWLINE_  <environments default="production">_NEWLINE_      <environment id="development">_NEWLINE_      <transactionManager type="JDBC"/>_NEWLINE_      <dataSource type="POOLED">_NEWLINE_        <property name="driver" value="${driver}"/>_NEWLINE_        <property name="url" value="${url}"/>_NEWLINE_        <property name="username" value="${username}"/>_NEWLINE_        <property name="password" value="${password}"/>_NEWLINE_      </dataSource>_NEWLINE_    </environment>_NEWLINE_          <environment id="production">  _NEWLINE_         <transactionManager type="MANAGED">  _NEWLINE_             <property name="" value=""/>  _NEWLINE_         </transactionManager>  _NEWLINE_         <dataSource type="POOLED">  _NEWLINE_             <property name="driver" value="${driver}"/>  _NEWLINE_             <property name="url" value="${url}"/>  _NEWLINE_             <property name="username" value="${username}"/>  _NEWLINE_             <property name="password" value="${password}"/>  _NEWLINE_             <property name="poolMaximumActiveConnections" value="25"/>  _NEWLINE_             <property name="poolMaximumIdleConnections" value="5"/>  _NEWLINE_         </dataSource>  _NEWLINE_     </environment>_NEWLINE_  </environments>_NEWLINE_  <mappers>_NEWLINE_    <mapper resource="org/apache/ibatis/example/BlogMapper.xml"/>_NEWLINE_  </mappers></configuration>_NEWLINE_Za zm\xc3\xadnku zde ur\xc4\x8dit\xc4\x9b stoj\xc3\xad podpora rozli\xc5\xa1en\xc3\xad r\xc5\xafzn\xc3\xbdch b\xc4\x9bhov\xc3\xbdch prost\xc5\x99ed\xc3\xad, co\xc5\xbe je novinka od verze 3.0. Pomoc\xc3\xad tagu <environment> m\xc3\xa1me mo\xc5\xbenost definovat r\xc5\xafzn\xc3\xa9 nastaven\xc3\xad pro p\xc5\x99ipojen\xc3\xad k datab\xc3\xa1zi a pot\xc3\xa9 je m\xc4\x9bnit pomoc\xc3\xad jednoduch\xc3\xa9 konfigurace.\n_START_SECTION_\nVytvo\xc5\x99en\xc3\xad SqlSessionFactory bez pou\xc5\xbeit\xc3\xad XML\n_START_PARAGRAPH_\niBATIS nab\xc3\xadz\xc3\xad t\xc5\x99\xc3\xaddu Configuration, kter\xc3\xa1 poskytuje stejn\xc3\xa9 konfigura\xc4\x8dn\xc3\xad mo\xc5\xbenosti, jako XML soubor._NEWLINE_DataSource dataSource = BlogDataSourceFactory.getBlogDataSource();TransactionFactory transactionFactory = new JdbcTransactionFactory();Environment environment = _NEWLINE_new Environment("development ", transactionFactory, dataSource);Configuration configuration = new Configuration(environment);configuration.addMapper(BlogMapper.class);SqlSessionFactory sqlSessionFactory = _NEWLINE_new SqlSessionFactoryBuilder().build(configuration);_NEWLINE_V tomto p\xc5\x99\xc3\xadpad\xc4\x9b pot\xc5\x99ebujeme nav\xc3\xadc je\xc5\xa1t\xc4\x9b takzvanou mapper t\xc5\x99\xc3\xaddu, kter\xc3\xa1 obsahuje SQL mapovac\xc3\xad anotace, abychom se vyhnuli pot\xc5\x99eb\xc4\x9b XML. Z d\xc5\xafvodu omezen\xc3\xad Java anotac\xc3\xad a komplexnosti n\xc4\x9bkter\xc3\xbdch mapov\xc3\xa1n\xc3\xad jsou XML st\xc3\xa1le pot\xc5\x99eba pro nejslo\xc5\xbeit\xc4\x9bj\xc5\xa1\xc3\xad mapov\xc3\xa1n\xc3\xad (nap\xc5\x99. Nested Join Mapping).\n_START_SECTION_\nZ\xc3\xadsk\xc3\xa1v\xc3\xa1n\xc3\xad SqlSession z SqlSessionFactory\n_START_PARAGRAPH_\nNyn\xc3\xad, kdy\xc5\xbe u\xc5\xbe m\xc3\xa1me SqlSessionFactory, m\xc5\xaf\xc5\xbeeme z\xc3\xadskat instanci t\xc5\x99\xc3\xaddy SqlSession. Tato t\xc5\x99\xc3\xadda obsahuje v\xc5\xa1echny metody pot\xc5\x99ebn\xc3\xa9 pro vykon\xc3\xa1v\xc3\xa1n\xc3\xad SQL dotaz\xc5\xaf do datab\xc3\xa1ze. Mapovan\xc3\xa9 SQL p\xc5\x99\xc3\xadkazy je mo\xc5\xben\xc3\xa9 prov\xc3\xa1d\xc4\x9bt p\xc5\x99\xc3\xadmo proti tomuto objektu. Nap\xc5\x99\xc3\xadklad:_NEWLINE_SqlSession session = sqlMapper.openSession();try {_NEWLINE_  Blog blog = (Blog) session.select(_NEWLINE_    "org.apache.ibatis.example.BlogMapper.selectBlog", 101);} finally {_NEWLINE_  session.close();}_NEWLINE_I kdy\xc5\xbe tento p\xc5\x99\xc3\xadstup funguje, je zde daleko p\xc5\x99ehledn\xc4\x9bj\xc5\xa1\xc3\xad p\xc5\x99\xc3\xadstup. Pou\xc5\xbeit\xc3\xadm rozhran\xc3\xad, kter\xc3\xa9 p\xc5\x99esn\xc4\x9b pop\xc3\xad\xc5\xa1e parametry a n\xc3\xa1vratov\xc3\xa9 hodnoty pro dan\xc3\xbd p\xc5\x99\xc3\xadkaz, je mo\xc5\xben\xc3\xa9 doc\xc3\xadlit daleko \xc4\x8dist\xc4\x9bj\xc5\xa1\xc3\xadho a typov\xc4\x9b bezpe\xc4\x8dn\xc3\xa9ho k\xc3\xb3du. Nap\xc5\x99\xc3\xadklad:_NEWLINE_SqlSession session = sqlSessionFactory.openSession();try {_NEWLINE_  BlogMapper mapper = session.getMapper(BlogMapper.class);_NEWLINE_  Blog blog = mapper.selectBlog(101);} finally {_NEWLINE_  session.close();}\n_START_SECTION_\nMapovan\xc3\xa9 SQL p\xc5\x99\xc3\xadkazy\n_START_PARAGRAPH_\nP\xc5\x99\xc3\xadkazy mohou b\xc3\xbdt definovan\xc3\xa9 bu\xc4\x8f pomoc\xc3\xad XML, nebo anotac\xc3\xad. Poj\xc4\x8fme se nejd\xc5\x99\xc3\xadve pod\xc3\xadvat na XML.  \xc3\x9aplnou sadu funkc\xc3\xad poskytovan\xc3\xbdch iBATISem lze realizovat pomoc\xc3\xad mapovac\xc3\xadho jazyku zalo\xc5\xbeen\xc3\xa9ho na XML, kter\xc3\xbd u\xc4\x8dinil iBATIS tak popul\xc3\xa1rn\xc3\xad v pr\xc5\xafb\xc4\x9bhu posledn\xc3\xadch let.  Zde je p\xc5\x99\xc3\xadklad s mapov\xc3\xa1n\xc3\xadm pomoc\xc3\xad XML, kter\xc3\xa9 by umo\xc5\xbenilo \xc3\xbasp\xc4\x9b\xc5\xa1n\xc3\xa9 vol\xc3\xa1n\xc3\xad v\xc3\xbd\xc5\xa1e uveden\xc3\xa9 SqlSession._NEWLINE_<?xml version="1.0" encoding="UTF-8" ?> _NEWLINE_<!DOCTYPE mapper     PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"     "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> _NEWLINE_<mapper namespace="org.apache.ibatis.example.BlogMapper"> _NEWLINE_  <select id="selectBlog" parameterType="int" resultType="Blog"> _NEWLINE_    select * _NEWLINE_from Blog _NEWLINE_where id = #{id} _NEWLINE_  </select> _NEWLINE_</mapper>_NEWLINE_Tato uk\xc3\xa1zka definuje mapovan\xc3\xbd p\xc5\x99\xc3\xadkaz\xe2\x80\x9cselectBlog\xe2\x80\x9d, ve jmenn\xc3\xa9m prostoru \xe2\x80\x9corg.apache.ibatis.example.BlogMapper\xe2\x80\x9d. N\xc3\xa1sleduj\xc3\xadc\xc3\xad uk\xc3\xa1zka demonstruje pou\xc5\xbeit\xc3\xad anotac\xc3\xad pro stejn\xc3\xbd \xc3\xba\xc4\x8del: _NEWLINE_package org.apache.ibatis.example;public interface BlogMapper {_NEWLINE_  @Select("SELECT * FROM blog WHERE id = #{id}")_NEWLINE_  Blog selectBlog(int id);}_NEWLINE_Anotace maj\xc3\xad sv\xc3\xa9 omezen\xc3\xad a nehod\xc3\xad se na slo\xc5\xbeit\xc4\x9bj\xc5\xa1\xc3\xad konstrukce._NEWLINE_Update, delete_NEWLINE_Update ani Delete se od Selectu moc neli\xc5\xa1\xc3\xad._NEWLINE_<select id="selectBlog" parameterType="int" resultType="Blog"> _NEWLINE_  select * _NEWLINE_          from Blog _NEWLINE_          where id = #{id} _NEWLINE_</select> _NEWLINE__NEWLINE_<update id="updateBlog" parameterType="Blog">_NEWLINE_  update Blog set _NEWLINE_title = #{title}_NEWLINE_  where id = #{id}_NEWLINE_</update><delete id="deleteBlog\xe2\x80\x9d parameterType="int">_NEWLINE_    delete from Blog where id = #{id}_NEWLINE_</delete>\n_START_SECTION_\nDynamick\xc3\xa9 SQL\n_START_PARAGRAPH_\nVelmi zaj\xc3\xadmav\xc3\xa1 je tak\xc3\xa9 podpora dynamick\xc3\xbdch SQL dotaz\xc5\xaf pomoc\xc3\xad OGNL. Pou\xc5\xbe\xc3\xadvaj\xc3\xad se \xc4\x8dty\xc5\x99i z\xc3\xa1kladn\xc3\xad podm\xc3\xadnkov\xc3\xa9 elementy:_NEWLINE_\xe2\x80\xa2if_NEWLINE_\xe2\x80\xa2choose (when, otherwise)_NEWLINE_\xe2\x80\xa2trim (where, set)_NEWLINE_\xe2\x80\xa2foreach_NEWLINE_<select id="findActiveBlogLike"_NEWLINE_        parameterType="Blog" resultType="Blog">_NEWLINE_    SELECT * FROM BLOG WHERE state = \xe2\x80\x98ACTIVE\xe2\x80\x99_NEWLINE_    <if test="title != null">_NEWLINE_        AND title like ${title}_NEWLINE_    </if>_NEWLINE_    <if test="author != null && author.name != null">_NEWLINE_        AND title like ${author.name}_NEWLINE_    </if></select><select id="findActiveBlogLike"_NEWLINE_        parameterType="Blog" resultType="Blog">_NEWLINE_    SELECT * FROM BLOG WHERE state = \xe2\x80\x98ACTIVE\xe2\x80\x99_NEWLINE_    <choose>_NEWLINE_        <when test="title != null">_NEWLINE_            AND title like ${title}_NEWLINE_        </when>_NEWLINE_        <when test="author != null && author.name != null">_NEWLINE_            AND title like ${author.name}_NEWLINE_        </when>_NEWLINE_        <otherwise>_NEWLINE_            AND featured = 1_NEWLINE_        </otherwise>_NEWLINE_    </choose></select><select id="selectPostIn" resultType="domain.blog.Post">_NEWLINE_    SELECT *_NEWLINE_    FROM POST P_NEWLINE_    WHERE ID in_NEWLINE_    <foreach item="item" index="index" collection="list"_NEWLINE_             open="(" separator="," close=")">_NEWLINE_        #{item}_NEWLINE_    </foreach></select>_NEWLINE_Element trim (pop\xc5\x99\xc3\xadpad\xc4\x9b where a set) \xc5\x99e\xc5\xa1\xc3\xad situace, kde by podm\xc3\xadnka v ur\xc4\x8dit\xc3\xa9 \xc4\x8d\xc3\xa1sti dotazu mohla p\xc5\xafsobit vytvo\xc5\x99en\xc3\xad nevalidn\xc3\xadho SQL dotazu. Nap\xc5\x99\xc3\xadklad, pokud bychom v prvn\xc3\xadm p\xc5\x99\xc3\xadpad\xc4\x9b odstranili podm\xc3\xadnku \xe2\x80\x9estate = \xe2\x80\x98ACTIVE\xe2\x80\x99\xe2\x80\x9d, mohli bychom doj\xc3\xadt k dotazu _NEWLINE_SELECT * FROM BLOG _NEWLINE_WHERE_NEWLINE_Pokud by se ob\xc4\x9b podm\xc3\xadnky vyhodnotili jako nepravda. iBATIS m\xc3\xa1 ov\xc5\xa1em pro tyto situace velmi jednoduch\xc3\xa9 \xc5\x99e\xc5\xa1en\xc3\xad v podob\xc4\x9b elementu where:_NEWLINE_<select id=\xe2\x80\x9dfindActiveBlogLike\xe2\x80\x9d _NEWLINE_parameterType=\xe2\x80\x9dBlog\xe2\x80\x9d resultType=\xe2\x80\x9dBlog\xe2\x80\x9d>_NEWLINE_  SELECT * FROM BLOG _NEWLINE_  <where> _NEWLINE_  <if test=\xe2\x80\x9dstate != null\xe2\x80\x9d>_NEWLINE_     state = #{state}_NEWLINE_  </if> _NEWLINE_  <if test=\xe2\x80\x9dtitle != null\xe2\x80\x9d>_NEWLINE_    AND title like #{title}_NEWLINE_  </if>_NEWLINE_  <if test=\xe2\x80\x9dauthor != null and author.name != null\xe2\x80\x9d>_NEWLINE_    AND title like #{author.name}_NEWLINE_  </if>_NEWLINE_  </where></select>_NEWLINE_Pokud bychom s chov\xc3\xa1n\xc3\xadm where elementu nebyli spokojeni, m\xc5\xaf\xc5\xbeeme si nadefinovat vlastn\xc3\xad chov\xc3\xa1n\xc3\xad pomoc\xc3\xad elementu trim. Takto by vypadal ekvivalent k elementu where:_NEWLINE_<trim prefix="WHERE" prefixOverrides="AND |OR ">_NEWLINE_  \xe2\x80\xa6_NEWLINE_</trim>\n_START_SECTION_\nN\xc3\xa1stroje\n_START_PARAGRAPH_\nIbator je gener\xc3\xa1tor k\xc3\xb3du pro iBATIS. Z existuj\xc3\xadc\xc3\xad rela\xc4\x8dn\xc3\xad struktury je schopen vygenerovat k\xc3\xb3d POJO class, SqlMap.xml obsahuj\xc3\xadc\xc3\xad CRUD dotazy a DAO interface a implementaci. M\xc5\xaf\xc5\xbee b\xc3\xbdt pou\xc5\xbeit na za\xc4\x8d\xc3\xa1tku projektu nebo opakovan\xc4\x9b i v r\xc3\xa1mci build cyklu (pomoc\xc3\xad Antu).'