반응형

# lodash

  • 자바스크립트 라이브러리 중 하나로 또다른 자바스크립트 라이브러리인 제이쿼리(jquery)와 많이 사용 된다.
  • lodash, jquery 사용에 따른 퍼포먼스 차이가 많이 난다.

## 사용하는 방법

  • lodash 불러오기 ( 사용 시 _. 로 시작 )
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>
  •  jquery 불러오기 ( 사용 시 $ 로 시작 )
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>

 

## lodash 적용 예제 (윈도우 넓이 구하기)

### lodash 미 적용

  • lodash 미 적용 시 코드. 
  • 화면 변동에 따라 계속 실행 됨.

See the Pen lodash 미 적용 by dlagusgh1 (@dlagusgh1) on CodePen.

console.clear();
var $window = $(window);

$window.resize(function() {
	var windowWidth = $window.width();
    
    console.log("windowWidth : " + windowWidth);
});


== 위와 같은 코드 ==
console.clear();
var $window = $(window);

function a() {
	var windowWidth = $window.width();
    
    console.log("windowWidth : " + windowWidth);
}

$window.resize(function() {
	a();
});


== 위와 같은 코드 ==
console.clear();
var $window = $(window);

var a = function () {
	var windowWidth = $window.width();
    
    console.log("windowWidth : " + windowWidth);
}

$window.resize(function() {
	a();
});

## lodash 적용 코드. ( _.Throttle 적용 )

  • _.throttle (함수, 시간);
  • 입력 주기를 방해하지 않고, 일정 시간 동안의 입력을 모아서 한 번씩 출력을 제한.

See the Pen lodash _.throttle 적용 by dlagusgh1 (@dlagusgh1) on CodePen.

console.clear();
var $window = $(window);

var b = _.throttle(function () {
	var windowWidth = $window.width();
    
    console.log("windowWidth : " + windowWidth);
}, 1000);	// 1초에 1번 실행

$window.resize(function() {
	b();
});

## lodash 적용 코드. ( _.debounce 적용 )

  • _.debounce (함수, 시간);
  • 입력 주기가 끝나면(입력이 완료되면), 입력한 시간 만큼 딜레이 후 출력한다.
  • 여러번 발생하는 이벤트에서, 가장 마지막 이벤트만 실행되도록 만드는 것.
  • 회원가입 폼 등에 많이 사용된다.

See the Pen lodash _.debounce 적용 by dlagusgh1 (@dlagusgh1) on CodePen.

console.clear();
var $window = $(window);

var c = _.debounce(function () {
	var windowWidth = $window.width();
    
    console.log("windowWidth : " + windowWidth);
}, 1000);	// 1초에 1번 실행

$window.resize(function() {
	c();
});

## _.throttle 와 _.debounce 차이점

이벤트를 언제 발생 시킬지 시점의 차이가 있다.

debounce : 입력이 끝날때까지 무한정 대기

throttle : 입력이 시작되면 일정 주기를 계속 실행한다.

 

반응형
반응형

# cannot be cast to java.lang.String 해결방법

  • 아래와 같이 값을 바로 가져올 때 해당 에러 발생.
String value = (String) map.get("value");

String name = (String) row.get("memberName");

등등..
  • 해결방법은 캐스팅 변환이 아닌 String 클래스의 valueOf(Object)
String value = String.valueOf(map.get("value"));

String name = String.valueOf(row.get("memberName"));
반응형

'프로그래밍 > 자바, JDBC' 카테고리의 다른 글

자바 날짜 비교하여 날짜간의 차이 구하기.  (0) 2020.08.29
lodash (throttle, debounce) 개념  (0) 2020.08.26
스프링 부트2  (0) 2020.08.03
자바 현재 시간, 날짜 구하기  (0) 2020.07.31
스프링 부트1  (0) 2020.07.28
반응형

# 스프링 부트2

## 스프링 부트, MySQL, MyBatis 게시물 리스트, 상세보기

### MyBatis, MySQL 연결

  • pom.xml에 직접 찾아서 넣는 방법도 있지만, 기존에 있는 것 사용 예정
  • 프로젝트 폴더 우클릭 > Spring > Edit Starters 들어가서 MyBatis Framework, MySQL Driver 클릭 후 추가
  • application.yml 문서에 Mysql, Mybatis 관련 설정 입력

### application.yml 문서

server:
  port: 8011
spring:
  mvc:
    view:
      prefix: /WEB-INF/jsp/
      suffix: .jsp
  datasource:
    driver-class-name: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://127.0.0.1:3306/at?useUnicode=true&characterEncoding=utf8&autoReconnect=true&serverTimezone=Asia/Seoul&useOldAliasMetadataBehavior=true&zeroDateTimeNehavior=convertToNull
    username: MySQL 아이디
    password: MySQL 비밀번호
mybatis:
  type-aliases-package: com.sbs.jhs.at.dto
  

### ArticleController.java

package com.sbs.jhs.at.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.sbs.jhs.at.dto.Article;
import com.sbs.jhs.at.service.ArticleService;

@Controller
public class ArticleController {
	@Autowired
	private ArticleService articleService;
	
	@RequestMapping("/article/list")
	public String showList(Model model) {
		int count = articleService.getCount();
		List<Article> articles = articleService.getForPrintArticles();
		
		model.addAttribute("count", count);
		model.addAttribute("articles", articles);
		
		return "article/list";
	}
	
	@RequestMapping("/article/detail")
	public String showDetail(Model model, long id) {
		
		Article article = articleService.getOne(id);
		System.out.println(article);
		
		model.addAttribute("article", article);
		
		return "article/detail";
	}
}

### ArticleService.java

package com.sbs.jhs.at.service;

import java.util.List;

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

import com.sbs.jhs.at.dao.ArticleDao;
import com.sbs.jhs.at.dto.Article;

@Service
public class ArticleService {
	@Autowired
	private ArticleDao articleDao;
	
	public int getCount() {
		return 2;
	}
	
	public List<Article> getForPrintArticles() {
		List<Article> articles = articleDao.getForPrintArticles();
		
		return articles;
	}

	public Article getOne(long id) {
		
		Article article = articleDao.getOne(id);
		
		
		return article;
	}

	public void hitUp(long id) {
		// TODO Auto-generated method stub
		
	}
}



### ArticleDao.java

package com.sbs.jhs.at.dao;

import java.util.List;

import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import com.sbs.jhs.at.dto.Article;

@Mapper
public interface ArticleDao {
	List<Article> getForPrintArticles();

	Article getOne(@Param("id") long id);
}

### ArticleDao.xml

<?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.sbs.jhs.at.dao.ArticleDao">
	<select id="getForPrintArticles" resultType="Article">
		SELECT *
		FROM article
		WHERE displayStatus = 1
		ORDER BY id DESC
	</select>
	<select id="getOne" resultType="Article">
		SELECT *
		FROM article
		WHERE displayStatus = 1
		AND id = #{id}
	</select>
</mapper>
반응형
반응형

# 자바 현재 시간, 날짜 구하기

  • 아래 코드를 이용하여 현재 시간, 날짜를 구할 수 있다.
// 현재 날짜/시간 가져오기
long systemTime = System.currentTimeMillis();			
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.KOREA);			 
String dateTime = formatter.format(systemTime);	

System.out.println("현재시간 및 날짜 :  " + dateTime);
반응형

+ Recent posts