java

์Šคํ”„๋ง - ์ƒํ’ˆ ์กฐํšŒ ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ(1)

dev_summer 2021. 2. 27. 14:08

 

 

 

์Šคํ”„๋ง ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์ด์šฉํ•˜์—ฌ ์ƒํ’ˆ์„ ์กฐํšŒํ• ์ˆ˜ ์žˆ๋Š” ๊ฒŒ์‹œํŒ์„ ๋งŒ๋“ค์–ด๋ณด์ž

 

1. product ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

1) ๊ธฐ๋ณธ ์…‹ํŒ…

๋ฉ”์ด๋ธ ๋ฐฉ์‹ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๋ฉฐ ์•„๋ž˜์˜ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ƒ์„ฑ ์‹œ์— ํ•จ๊ฒŒ ๋งŒ๋“ค์–ด ์ค„ ๊ฒƒ์ด๋‹ค.

 

 

2) Tomcat Jasper ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ ์ถ”๊ฐ€

jspํŽ˜์ด์ง€๋Š” ์Šคํ”„๋ง์ด ์ง€์›ํ•˜์ง€์•Š์•„ ๋”ฐ๋กœ ์ถ”๊ฐ€ํ•ด์•ผ๊ฒ ๋‹ค.

-pom.xml ์— ์ถ”๊ฐ€

 

<!-- https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper -->
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jasper</artifactId>
    <version>9.0.22</version>
</dependency>

 

 

 

๋‹ค์‹œ ์‹คํ–‰ํ•ด๋ณด๋ฉด jasperrk ์ถ”๊ฐ€ ๋œ ๊ฒƒ์„ ํ™•์ธ ํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ž„์˜๋กœ ํด๋”๋ฅผ ์ง€์ •ํ•ด์„œ ๋งŒ๋“ค์–ด ์ค€๋‹ค.

 

- ์„ค์ •ํŒŒ์ผ ์ˆ˜์ •(yml)

 

 

server:
  port: 8080
  servlet:
    context-path: /

spring:
  mvc:
    view:
      prefix: /WEB-INF/views/
      suffix: .jsp 
      

 

 

๋ฌธ์„œ์˜ ๊ฐ€๋…์„ฑ์„ ์œ„ํ•ด ymlํŒŒ์ผ๋กœ ์ˆ˜์ • ํ›„ jspํŽ˜์ด์ง€๋ฅผ ์ธ์‹ํ•˜๊ธฐ ์œ„ํ•œ ์„ค์ •์ฝ”๋“œ๋ฅผ ๋„ฃ๋Š”๋‹ค.

์ด๋•Œ ๋‚ด๋ถ€ํ†ฐ์บฃ์˜ ํฌํŠธ๋ฒˆํ˜ธ์™€ context-path๋ฅผ ํ•จ๊ป˜ ์„ค์ •ํ•ด ์ฃผ์—ˆ๋‹ค.

 

-productController

package com.seoha.product.controller; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class ProductController { @GetMapping({"","/"}) public String index() { return "index"; } }

 

 

 

 

indexํŽ˜์ด์ง€๋ฅผ ๋งŒ๋“ค์–ด์„œ ๋””์ŠคํŒจ์ฒ˜๊ฐ€ ์ž˜ ๋‚š์•„ ์ฑ„๋Š”๊ฐ€ jasper๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์„ค์ •์ด ๋งž๊ฒŒ ๋˜์—ˆ๋Š”์ง€ ํ™•์ธ

 

 

3) mysql, mybatis ์„ค์ •ํ•˜๊ธฐ

- mysql์˜ ์‚ฌ์šฉ์ž์™€ ํ…Œ์ด๋ธ”์„ ํ•จ๊ป˜ ๋งŒ๋“ ๋‹ค.

์‚ฌ์šฉ์ž๋Š” ์•ž์—์„œ ๋งŒ๋“ค์—ˆ๋˜ spring์„ ์‚ฌ์šฉํ•˜๊ณ  ํ…Œ์ด๋ธ”๋งŒ ๋งŒ๋“ค์–ด์•ผ๊ฒ ๋‹น

CREATE TABLE product(
id int AUTO_INCREMENT PRIMARY KEY, 
name varchar(100) not null,
price int not null,
orderCount int,
type varchar(100)

)ENGINE=InnoDB DEFAULT CHARSET = utf8;

- model ๋งŒ๋“ค๊ธฐ

์•„์ด๋””๋Š” DB์—์„œ ์˜คํ† ๋ฅผ ๊ฑธ์–ด์คฌ๊ธฐ๋•Œ๋ฌธ์— ์ƒ์„ฑ์ž๋Š” ๋กฌ๋ถ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์“ฐ์ง€์•Š๊ณ  id๋งŒ ๋นผ๊ณ  ๋”ฐ๋กœ ์ƒ์„ฑ์ž๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹น. builder๋ฅผ ์“ธ ๊ณ„ํš์€ ์•„์ง ์—†์„๊ฑฐ ๊ฐ™์ง€๋งŒ ํ˜น์‹œ ๋ชจ๋ฅผ ๊ธฐ๋Šฅ์„ ์œ„ํ•ด ๋นŒ๋”๋ฅผ ๊ฑธ์–ด๋†“๋Š”๋‹น.

package com.seoha.product.model;

import lombok.Data;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@Data
public class Product {
	private int id;
	private String name;
	private int price;
	private int orderCount;
	private String type;
	
	@Builder
	public Product(String name, int price, int orderCount, String type) {
		super();
		this.name = name;
		this.price = price;
		this.orderCount = orderCount;
		this.type = type;
	}
}

 

 

- ymlํŒŒ์ผ์— ์ถ”๊ฐ€

  datasource:
    url: jdbc:mysql://localhost:3306/spring?serverTimezone=Asia/Seoul
    username: spring
    password: bitc5600
    driver-class-name: com.mysql.cj.jdbc.Driver

 

jsp๋กœ mysqlํ• ๋•Œ๋ž‘ ๋‹ค๋ฅธ๊ฒŒ ์—†๋‹ค! ์—ฌ๊ธฐ์„œ๋„ ์„œ๋ฒ„ํƒ€์ž„์กด์„ ์„ค์ •ํ•ด์ค€๋‹ค!

 

- config ํŒจํ‚ค์ง€ ๋งŒ๋“ค์–ด์„œ mybatis์„ค์ •ํŒŒ์ผ ๋ณต๋ถ™ํ•˜๊ธฐ

 

 

 

- DataAccessConfig

๋ณต๋ถ™๋งŒ ํ•˜๋ฉด ๋˜๋Š”๋ฐ ๋ฒ ์ด์ŠคํŒจํ‚ค์ง€๋Š” repository ํŒจํ‚ค์ง€๋ฅผ ๋ฐ”๋ผ๋ณผ์ˆ˜์žˆ๊ฒŒ ์…‹ํŒ…ํ•ด์ค€๋‹ค.

๋˜ ๋งคํผ์˜ xmlํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์ค€๋‹ค

package com.seoha.product.config;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

@Configuration
@MapperScan(basePackages = "com.seoha.product.repository")
public class DataAccessConfig {

    @Bean
    public SqlSessionFactory sqlSessionFactory(DataSource dataSource) throws Exception {

        SqlSessionFactoryBean sessionFactory = new SqlSessionFactoryBean();

        sessionFactory.setDataSource(dataSource);
        sessionFactory.setMapperLocations(
        		new PathMatchingResourcePatternResolver().getResources("classpath:mapper/*.xml"));
        return sessionFactory.getObject();
    }

    @Bean
    public SqlSessionTemplate sqlSessionTemplate(SqlSessionFactory sqlSessionFactory) {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}

 

- DataSourceConfig

package com.seoha.product.config;

import javax.sql.DataSource;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Configuration;

@Configuration
public class DataSourceConfig {

	@ConfigurationProperties(prefix="spring.datasource")
	public DataSource dataSource(){
    	return DataSourceBuilder.create().build();
	}
}

 

 

-mapper/product.xml

๋‚œ ํƒ€์ž…์— ๋งž๊ฒŒ ๋ฟŒ๋ฆฌ๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๊ธฐ์— selete๋งŒ ์‚ฌ์šฉ

<?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="com.seoha.baseball.repository.BaseballRepository">

	<select id="findAll" resultType="com.seoha.baseball.model.Baseball">
		select * from baseball
	</select>
 
</mapper>

- com.seoha.product.repository.ProductRepository ์ธํ„ฐํŽ˜์ด์Šค ๋งŒ๋“ค๊ธฐ

 

package com.seoha.product.repository;

import java.util.List;

import com.seoha.product.model.Product;

public interface ProductRepository {
	List<Product> findAll();
}	

 

 

 

 

4. jspํŽ˜์ด์ง€์— ์ถœ๋ ฅํ•˜๊ธฐ

 

 

 

-productController์ถ”๊ฐ€

๊ฐ„๋‹จํ•˜๊ฒŒ ๋งŒ๋“  ์ฟผ๋ฆฌ๋ฌธ์„ ์‚ฌ์šฉํ•˜์—ฌ ์ •๋ณด๋ฅผ ๋ชจ๋ธ์— ์ €์žฅํ•˜์˜€๋‹น

@Controller
public class ProductController {

	@Autowired
	private ProductRepository productRepository;

	@GetMapping({ "", "/" })
	public String index(Model model) {
		
		List<Product> products = productRepository.findAll();
		model.addAttribute("products", products);
		return "product";
		

 

-product์— ๋ฟŒ๋ฆฌ๊ธฐ

	<table border="1">
		<tr id="title">
			<th>id</th>
			<th>name</th>
			<th>price</th>
			<th>orderCount</th>
			<th>type</th>
		</tr>
		
		<c:forEach var="product" items="${products}">

			<tr class="item">
				<td>${product.id}</td>
				<td>${product.name}</td>
				<td>${product.price}</td>
				<td>${product.orderCount}</td>
				<td>${product.type}</td>

			</tr>
		</c:forEach>

	</table>

 

 

 

 

์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง์„ ์ด์šฉํ•ด์„œ ๋ฟŒ๋ ค๋ณด์•˜๋‹น