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

Spring Bootを触ってみた

Avatar for onozaty onozaty
December 22, 2016

Spring Bootを触ってみた

社内勉強会にてSpring Bootについて発表した資料です。

Avatar for onozaty

onozaty

December 22, 2016
Tweet

More Decks by onozaty

Other Decks in Programming

Transcript

  1. ͳͥSpring Bootʁ • ͪΐͬͱ৮ͬͯΈͨΒɺͱͯ΋؆୯Ͱɺίʔ υॻָ͍͔ͯͯͬͨ͠ • Play Framework ΑΓෑډ͕௿͘ײͨ͡ •

    Springͷํ͕৘ใ͕ଟ͍ • Play΍Γ͍ͨ͠ɺScalaͰॻ͖͍͚ͨͲɺ νʔϜʹಋೖ͠Α͏ͱͳΔͱScalaΛ֮͑Δ ίετ͸ͦΕͳΓʹͰ͔͍
  2. Hello World! EclipseͰΠϯϙʔτͯ͠ɺ“Hello World!!” Λฦ ͢ίϯτϩʔϥ௥Ճ package com.example; import org.springframework.stereotype.Controller;

    import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller public class HelloController { @RequestMapping("/") @ResponseBody public String hello(){ return "Hello World!!"; } }
  3. Spring JDBC @Repository public class CustomerRepository { @Autowired private NamedParameterJdbcTemplate

    jdbcTemplate; public List<Customer> findAll() { return jdbcTemplate.query( "SELECT * FROM customers ORDER BY id", new BeanPropertyRowMapper<Customer>(Customer.class)); } public void update(Customer customer) { jdbcTemplate.update( "UPDATE customers SET name = :name, address = :address WHERE id = :id", new BeanPropertySqlParameterSource(customer)); } }
  4. Spring DATA JPA @Entity @Table(name = "customers") @Data @AllArgsConstructor @NoArgsConstructor

    public class Customer { @Id @GeneratedValue private Integer id; private String name; private String address; }
  5. Spring DATA JPA @Service public class CustomerService { @Autowired private

    CustomerRepository customerRepository; public List<Customer> findAll() { return customerRepository.findAll(); } public List<Customer> findByName(String name) { return customerRepository.findByName(name); } public Customer create(Customer customer) { return customerRepository.save(customer); } @Repository public interface CustomerRepository extends JpaRepository<Customer, Integer> { public List<Customer> findByName(String name); }