Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

week10_백인서 #109

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.example.jdbcdemoleal.Config;
import com.example.jdbcdemoleal.repository.PostRepository;
import com.example.jdbcdemoleal.repository.PostRepositoryImpl;
import com.example.jdbcdemoleal.service.PostService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import javax.sql.DataSource;

@Configuration
public class config {
@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost:3306/mydatabase");
dataSource.setUsername("root");
dataSource.setPassword("0000");
return dataSource;
}
@Bean
public JdbcTemplate jdbcTemplate(DataSource dataSource) {
return new JdbcTemplate(dataSource);
}
@Bean
public PostRepository postRepository(JdbcTemplate jdbcTemplate) {
return new PostRepositoryImpl(jdbcTemplate);
}

@Bean
public PostService postService(PostRepository postRepository) {
return new PostService(postRepository);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@

@SpringBootApplication
public class JdbcdemolealApplication {

public static void main(String[] args) {
SpringApplication.run(JdbcdemolealApplication.class, args);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
public interface PostRepository {
List<Post> findAll();
void save(Post post);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.example.jdbcdemoleal.repository;
import com.example.jdbcdemoleal.entity.Post;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

@Repository
public class PostRepositoryImpl implements PostRepository {
private final JdbcTemplate jdbcTemplate;

public PostRepositoryImpl(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}

@Override
public List<Post> findAll() {
String sql = "SELECT * FROM post";
return jdbcTemplate.query(sql, new PostRowMapper());
}

@Override
public void save(Post post) {
String sql = "INSERT INTO post (title, content) VALUES (?, ?)";
jdbcTemplate.update(sql, post.getTitle(), post.getContent());
}

private static class PostRowMapper implements RowMapper<Post> {
@Override
public Post mapRow(ResultSet rs, int rowNum) throws SQLException {
return new Post(rs.getLong("id"), rs.getString("title"), rs.getString("content"));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package com.example.jdbcdemoleal.service;
import com.example.jdbcdemoleal.entity.Post;
import com.example.jdbcdemoleal.repository.PostRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class PostService {
private final PostRepository postRepository;

@Autowired
public PostService(PostRepository postRepository) {
this.postRepository = postRepository;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
spring.application.name=jdbcdemoleal
spring.datasource.url=jdbc:h2:tcp://localhost/~/test
spring.datasource.url= jdbc:mysql://localhost:3306/mydatabase
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.datasource.username=root
spring.datasource.password=0000
spring.h2.console.enabled=true