100일 챌린지/빅데이터기반 인공지능 융합 서비스 개발자

Day 72 - Spring Boot 로 프로그램 만들기

ksyke 2024. 11. 11. 12:51

목차

    CRED 프로그램 만들기

    lombok 연결하기

    mysql 사용하기

    application.properties

    spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
    spring.datasource.url=jdbc:mysql://localhost:3306/xe
    spring.datasource.username=scott
    spring.datasource.password=tiger

    ProjectApplication

    @RestController
    @SpringBootApplication
    public class Sts04Application {
    
    	public static void main(String[] args) {
    		SpringApplication.run(Sts04Application.class, args);
    	}
    }

    DeptVo

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    @Builder
    public class DeptVo {
    	int deptno;
    	String dname,loc;
    }

    DeptDaoImpl

    @Repository
    public class DeptDaoImpl implements DeptDao {
    	@Autowired
    	JdbcTemplate jdbcTemplate;
    	
    	@Override
    	public List<DeptVo> getList(){
    		return jdbcTemplate.query("select * from dept", new RowMapper<DeptVo>() {
    
    			@Override
    			public DeptVo mapRow(ResultSet rs, int rowNum) throws SQLException {
    				// TODO Auto-generated method stub
    				return DeptVo.builder()
    						.deptno(rs.getInt("deptno"))
    						.dname(rs.getString("dname"))
    						.loc(rs.getString("loc"))
    						.build()
    						;
    			}
    		});
    	}
    }

    DeptDao

    public interface DeptDao {
    
    	List<DeptVo> getList();
    
    }

    MainController

    @Controller
    public class MainController {
    	@Autowired
    	DeptDao deptDao;
    
    	@ResponseBody
    	@GetMapping("/")
    	public List<DeptVo> index() {
    		return deptDao.getList();
    	}
    }

    H2 사용하기

    application.properties

    spring.h2.console.enabled=true
    spring.h2.console.path=/h2
    spring.h2.console.settings.web-allow-others=true
    #spring.h2.console.settings.web-admin-password=1234

    ProjectApplication.class

    @SpringBootApplication
    public class Sts05Application implements CommandLineRunner{
    
    	public static void main(String[] args) {
    		SpringApplication.run(Sts05Application.class, args);
    	}
    	
    	@Autowired
    	DataSource dataSource;
    
    	@Override
    	public void run(String... args) throws Exception {
    		try(
    				Connection conn=dataSource.getConnection()
    				){
    			conn.createStatement().execute("create table dept100 (deptno int primary key,dname varchar(10),loc varchar(10))");
    			
    		}
    	}
    }

    webcrawling 하기

    ProjectApplication.class

            Thread.sleep(10000);
    
            String msg="https://www.weather.go.kr/w/index.do";
            URL url=new URL(msg);
            URLConnection conn=url.openConnection();
            try(
                    InputStream is=conn.getInputStream();
                    ){
                int cnt=-1;
                while((cnt=is.read())!=-1)
                System.out.print((char)cnt);
            }

    spring batch

    https://spring.io/projects/spring-batch

     

    Spring Batch

    A lightweight, comprehensive batch framework designed to enable the development of robust batch applications vital for the daily operations of enterprise systems. Spring Batch provides reusable functions that are essential in processing large volumes of re

    spring.io

     

    log level 설정하기

    application.properties

    logging.level.web=error
    logging.level.com.gimhae.sts04=debug

    file 로 로그 저장하기

    application.properties

    logging.file.path=.
    logging.file.name=test