LoginSignup
0
0

More than 5 years have passed since last update.

GivePoint作成

Posted at

STSの設定

Package Explorerから、
src > testフォルダを右クリック、[Delete]で削除します。
次に、src/main/java > com.example.demoの中にあるJavaクラスファイルを削除します。
01.png
02.png

pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>GivePoint</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <name>GivePoint</name>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.8.RELEASE</version>
    </parent>

    <dependencies>
        <!-- (1) -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <!-- (2) -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <!-- (3) -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.14.0</version>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.lazyluke</groupId>
            <artifactId>log4jdbc-remix</artifactId>
            <version>0.2.7</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <!-- (3) -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <properties>
        <java.version>1.8</java.version>
        <!-- (4) -->
    </properties>
</project>
App.java
package com.customer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;

/** アプリケーションクラス */
@EnableAutoConfiguration
@ComponentScan
public class App {

    /** Spring BOOTはおなじみのmainメソッドから始まる
     * @param args Java実行時の引数
     *  */
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
AppConfig.java
package com.customer;

import javax.sql.DataSource;

import net.sf.log4jdbc.Log4jdbcProxyDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/** 設定などを管理するConfigクラス */
@Configuration
public class AppConfig {

    // データソース設定のプロパティを格納
    @Autowired
    DataSourceProperties dataSourceProperties;

    // データソース
    DataSource dataSource;

    @Bean
    DataSource realDataSource() {
        // データソースインスタンスを作る
        DataSourceBuilder factory = DataSourceBuilder.create(this.dataSourceProperties.getClassLoader())
                .url(this.dataSourceProperties.getUrl()).username(this.dataSourceProperties.getUsername())
                .password(this.dataSourceProperties.getPassword());
        this.dataSource = factory.build();
        return this.dataSource;
    }

    // log4JDBCをつかうために
    @Bean
    DataSource dataSource() {
        return new Log4jdbcProxyDataSource(this.dataSource);
    }
}
CustomerRestController.java
package com.customer.api;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.customer.service.CustomerService;

@RestController
public class CustomerRestController {
    @Autowired
    CustomerService customerService;

    @RequestMapping(value = "addpoint/{point}", method = RequestMethod.GET)
    int addPoint(@PathVariable int point) {
        int appdateRows = customerService.addPoint(point);
        return appdateRows;
    }
}
ustomerRepository.java
package com.customer.repository;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.stereotype.Repository;

@Repository
public class CustomerRepository {
    @Autowired
    NamedParameterJdbcTemplate jdbcTemplate;

    public int addPoint(int point) {
        SqlParameterSource param = new MapSqlParameterSource()
                .addValue("point", point).addValue("id", 1);

        int updatedRows = jdbcTemplate.update(
                "UPDATE AmountCustomer SET Point = Point + :point WHERE Id = :id", param);

        return updatedRows;
    }
}
CustomerService.java
package com.customer.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.customer.repository.CustomerRepository;

@Service
public class CustomerService {
    @Autowired
    CustomerRepository customerRepository;

    public int addPoint(int point) {
        int updateRows = customerRepository.addPoint(point);
        return updateRows;
    }
}
application.properties
spring.datasource.url=jdbc:mysql://localhost/kigyoudb
spring.datasource.username=user
spring.datasource.password=12345
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=validate
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0