Upgrade to Pro — share decks privately, control downloads, hide ads and more …

実践MyBatis #kanjava

実践MyBatis #kanjava

「関西Javaエンジニアの会(関ジャバ) '17 4月度」の発表資料です。

Shinichi Kozake

April 26, 2017
Tweet

More Decks by Shinichi Kozake

Other Decks in Technology

Transcript

  1. .Z#BUJT ઃఆϑΝΠϧ 9.- .BQQFSఆٛ 9.- NBQQFST <?xml version="1.0" encoding="UTF-8" ?>

    <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="sample.domain"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> : </dataSource> </environment> </environments> <mappers> <mapper resource="sample/mapper/CityMapper.xml"/> </mappers> </configuration> <?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"> <mapper namespace="sample.mapper.CityMapper"> <select id="selectCityById" resultType="City"> select * from city where id = #{id} </select> </mapper>
  2. .Z#BUJT ઃఆϑΝΠϧ 9.- .BQQFSఆٛ 9.- .BQQFS ΠϯλϑΣʔε NBQQFST &OUJUZ 42-ͷ݁Ռηοτͱ

    Ϛοϐϯά͢Δ+BWBιʔεɻ ओʹςʔϒϧͷϨίʔυͷ ৘ใΛอ࣋͢Δɻ 42-ఆٛΛϚοϐϯά͢Δ ΠϯλϑΣʔεɻ
  3. .Z#BUJT ઃఆϑΝΠϧ 9.- .BQQFSఆٛ 9.- .BQQFS ΠϯλϑΣʔε NBQQFST &OUJUZ public

    interface CityMapper { City selectCityById(long id); } public class City { private Long id; private String name; private String state; private String country; : }
  4. 4RM4FTTJPO 'BDUPSZ #VJMFS .Z#BUJT ઃఆϑΝΠϧ 9.- 4RM4FTTJPO 'BDUPSZ 4RM4FTTJPO .BQQFSఆٛ

    9.- .BQQFS ΠϯλϑΣʔε CVJME PQFO4FTTJPO HFU.BQQFS NBQQFST SFBE &OUJUZ
  5. 4RM4FTTJPO 'BDUPSZ #VJMFS .Z#BUJT ઃఆϑΝΠϧ 9.- 4RM4FTTJPO 'BDUPSZ 4RM4FTTJPO .BQQFSఆٛ

    9.- .BQQFS ΠϯλϑΣʔε CVJME PQFO4FTTJPO HFU.BQQFS NBQQFST SFBE &OUJUZ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSession session = sqlSessionFactory.openSession(); try { CityMapper mapper = session.getMapper(CityMapper.class); City city = mapper.selectCityById(1); : } finally { session.close(); }
  6. .BQQFS ΠϯλϑΣʔε &OUJUZ public interface CityMapper { @Select("select * from

    city where id = #{id}") City selectCityById(long id); }
  7. .Z#BUJT ઃఆϑΝΠϧ 9.- <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC

    "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="sample.domain"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> : </dataSource> </environment> </environments> <!-- <mappers> <mapper resource="sample/mapper/CityMapper.xml"/> </mappers> --> <mappers> <package name="sample.mapper" /> </mappers> </configuration>
  8. 4RM4FTTJPO 'BDUPSZ #VJMFS .Z#BUJT ઃఆϑΝΠϧ 9.- 4RM4FTTJPO 'BDUPSZ 4RM4FTTJPO .BQQFSఆٛ

    9.- .BQQFS ΠϯλϑΣʔε CVJME PQFO4FTTJPO HFU.BQQFS NBQQFST SFBE &OUJUZ String resource = "mybatis-config.xml"; InputStream inputStream = Resources.getResourceAsStream(resource); SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream); SqlSessionManager session = SqlSessionManager.newInstance(sqlSessionFactory); session.startManagedSession(); try { CityMapper mapper = session.getMapper(CityMapper.class); City city = mapper.selectCityById(1); : } finally { session.close(); }
  9. +%,1SPYZ +%,1SPYZ .BQQFS 'BDUPSZ#FBO 4RM4FTTJPO 'BDUPSZ 4RM4FTTJPO 5FNQMBUF .Z#BUJT ઃఆϑΝΠϧ

    9.- .BQQFS ΠϯλϑΣʔε PQFO4FTTJPO 4RM4FTTJPO 4RM4FTTJPO 'BDUPSZ#FBO 4RM4FTTJPO 'BDUPSZ #VJMEFS SFBE CVJME
  10. +%,1SPYZ +%,1SPYZ .BQQFS 'BDUPSZ#FBO 4RM4FTTJPO 5FNQMBUF .BQQFS ΠϯλϑΣʔε PQFO4FTTJPO 4RM4FTTJPO

    4RM4FTTJPO 'BDUPSZ#FBO 4RM4FTTJPO'BDUPSZ#VJMEFS Λ༻͍ͯ4RM4FTTJPO'BDUPSZ ͷ#FBOΛੜ੒ɻ %*ίϯςφ্ʹ#FBOԽ͞Εͨ σʔλιʔεΛΠϯδΣΫγϣϯɻ 4RM4FTTJPO 'BDUPSZ 4RM4FTTJPO 'BDUPSZ #VJMEFS CVJME .Z#BUJT ઃఆϑΝΠϧ 9.- SFBE
  11. +%,1SPYZ 4RM4FTTJPO 'BDUPSZ .Z#BUJT ઃఆϑΝΠϧ 9.- PQFO4FTTJPO 4RM4FTTJPO 4RM4FTTJPO 'BDUPSZ#FBO

    4RM4FTTJPO 'BDUPSZ #VJMEFS SFBE CVJME +%,1SPYZ .BQQFS 'BDUPSZ#FBO 4RM4FTTJPO 5FNQMBUF .BQQFS ΠϯλϑΣʔε .BQQFSΦϒδΣΫτΛ ੜ੒͠ɺ%*ίϯςφʹ #FBOొ࿥ 4QSJOHͷτϥϯβΫγϣϯ؅ཧԼ Ͱ42-Λ࣮ߦ͢Δ࢓૊ΈΛఏڙɻ
  12. @Mapper public interface CityMapper { @Select("select * from city where

    id = #{id}") City selectCityById(long id); } public class City { : } @SpringBootApplication public class DemoApplication implements CommandLineRunner { final private CityMapper mapper; public DemoApplication(CityMapper mapper) { this.mapper = mapper; } public static void main(String... args) { SpringApplication.run(DemoApplication.class, args); } public void run(String... args) throws Exception { City city = mapper.selectCityById(1); : } }
  13. @Mapper public interface CityMapper { @Select("select * from city where

    id = #{id}") City selectCityById(long id); } public class City { : } @SpringBootApplication public class DemoApplication implements CommandLineRunner { final private CityMapper mapper; public DemoApplication(CityMapper mapper) { this.mapper = mapper; } public static void main(String... args) { SpringApplication.run(DemoApplication.class, args); } public void run(String... args) throws Exception { City city = mapper.selectCityById(1); : } } ͨͬͨ͜Ε͚ͩʂ
  14. public interface TypeHandler<T> { void setParameter(PreparedStatement ps, int i, T

    parameter, JdbcType jdbcType) throws SQLException; T getResult(ResultSet rs, String columnName) throws SQLException; T getResult(ResultSet rs, int columnIndex) throws SQLException; T getResult(CallableStatement cs, int columnIndex) throws SQLException; } ɾ1SFQBSFE4UBUFNFOUͷύϥϝʔλʔΛηοτ͢Δͱ͖ɺ ɹ͋Δ͍͸3FTVMU4FU͔Β஋Λऔಘ͢Δͱ͖ʹɺ ɹඞͣͦͷ+BWBλΠϓʹରԠ͢Δ5ZQF)BOEMFS͕࢖༻͞ΕΔɻ ɾඪ४ͷλΠϓϋϯυϥʔΛΦʔόʔϥΠυͨ͠Γɺ ɹࣗΒ࡞੒ͨ͠λΠϓϋϯυϥʔΛ࢖ͬͯະରԠ͋Δ͍͸ ɹඇඪ४ͷܕΛѻ͏͜ͱ΋Մೳɻ
  15. : public List<City> selectByNameAndState() { return query() .where() .nameEqualTo("sakai") .and()

    .stateEqualTo("osaka") .orderBy("id desc") .fetch(); } ಠࣗʢΦϨΦϨʣ1MVHJOͰྲྀΕΔΑ͏ͳ"1* ͰΫΤϦΛ͔͚Δ޻෉Λͨ͠Γ͍ͯ͠·͢ɻ