스프링 xml 설정을 ->  Annotation설정으로 

    Annotaion 활용 서비스 객체 DI

    dependency injection 부분을 Annotaion으로 변경하기

    ListController 객체의 property를 xml에서 Annotaion 으로 변경하기

    servlet-context.xml

    @Autowired 

    ListController.java

     

    xmlns:context 네임스페이스 태그 만들기

    <context:annotation-config />

    servlet-context.xml

    @Autowired 표기 위치

    ListController.java

    setter를 지워준다.


    Service객체의 property를 xml에서 Annotaion 으로 변경하기

    xml의 property를 쉽게 바인딩 하기 

    servlet-context.xml

    setter를 삭제 @Autowired 표시

    JDBCNoticeService.java

     

    Annotation을 사용하겠다는 설정 해주기

    service-context.xml


    ListController.java

    package com.newlecture.web.controller.notice;
    
    import java.util.List;
    
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.servlet.ModelAndView;
    import org.springframework.web.servlet.mvc.Controller;
    
    import com.newlecture.web.entity.Notice;
    import com.newlecture.web.service.NoticeService;
    import com.newlecture.web.service.jdbc.JDBCNoticeService;
    
    public class ListController implements Controller {
    	
    	@Autowired
    	private NoticeService noticeService;
    	
    	/*
    	 * //@Autowired 바인딩 될때 수행할것이 있으면 여기에 @Autowired 표기하는것이 바람직하다. public void
    	 * setNoticeService(NoticeService noticeService) { this.noticeService =
    	 * noticeService; }
    	 */
    
    	@Override
    	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    		// TODO Auto-generated method stub
    		ModelAndView mv = new ModelAndView("notice.list"); //객체생성
    		//mv.setViewName("/WEB-INF/view/notice/list.jsp"); //데이터 담은거 setting
    		List<Notice> list = noticeService.getList(1, "title", "");
    		mv.addObject("list", list);
    		return mv;
    	}
    
    }

     

    index 실행 - 공지사항이 잘 동작되는지 확인


    Annotation 으로 서비스 객체 생성하기

    xml에 있는 서비스객체 주석 처리하기

    service-context.xml

     

    @Component 주석 표기

    JDBCNoticeService.java

    context component scan 태그 사용해서 설정

    <context:annotation-config /> 은 지워줘도 된다.

    service-context.xml

     

    @Component 역할 분명하게 해 주기 

    @Controller, @Service, @Repository 중에서 표기

     

    JDBCNoticeService.java

    package com.newlecture.web.service.jdbc;
    
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;
    import java.sql.Statement;
    import java.util.ArrayList;
    import java.util.Date;
    import java.util.List;
    
    import javax.sql.DataSource;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Component;
    import org.springframework.stereotype.Service;
    
    import com.newlecture.web.entity.Notice;
    import com.newlecture.web.service.NoticeService;
    
    @Service //@Controller, @Service, @Repository 
    public class JDBCNoticeService implements NoticeService {
    	/*
    	 * private String url = "jdbc:oracle:thin:@localhost:1521/xepdb1";
    	 * private String uid = "NEWLEC"; 
    	 * private String pwd = "11111"; 
    	 * private String driver = "oracle.jdbc.driver.OracleDriver";
    	 */
    	
    	@Autowired
    	private DataSource dataSource;
    	
    	/*
    	 * public void setDataSource(DataSource dataSource) { this.dataSource =
    	 * dataSource; }
    	 */
    
    	public List<Notice> getList(int page, String field, String query) throws ClassNotFoundException, SQLException{
    		
    		int start = 1 + (page-1)*10;     // 1, 11, 21, 31, ..
    		int end = 10*page; // 10, 20, 30, 40...
    		
    		String sql = "SELECT * FROM NOTICE_VIEW WHERE "+field+" LIKE ? AND NUM BETWEEN ? AND ?";	
    		
    		//Class.forName(driver);
    		//Connection con = DriverManager.getConnection(url,uid, pwd);
    		Connection con = dataSource.getConnection();
    		PreparedStatement st = con.prepareStatement(sql);
    		st.setString(1, "%"+query+"%");
    		st.setInt(2, start);
    		st.setInt(3, end);
    		ResultSet rs = st.executeQuery();
    		
    		List<Notice> list = new ArrayList<Notice>();
    		
    		while(rs.next()){
    		    int id = rs.getInt("ID");
    		    String title = rs.getString("TITLE");
    		    String writerId = rs.getString("WRITER_ID");
    		    Date regDate = rs.getDate("REGDATE");
    		    String content = rs.getString("CONTENT");
    		    int hit = rs.getInt("hit");
    		    String files = rs.getString("FILES");
    		    
    		    Notice notice = new Notice(
    		    					id,
    		    					title,
    		    					writerId,
    		    					regDate,
    		    					content,
    		    					hit,
    		    					files
    		    				);
    
    		    list.add(notice);
    		    
    		}
    
    		
    		rs.close();
    		st.close();
    		con.close();
    		
    		return list;
    	}
    	
    	// Scalar 
    	public int getCount() throws ClassNotFoundException, SQLException {
    		int count = 0;
    		
    		String sql = "SELECT COUNT(ID) COUNT FROM NOTICE";	
    		
    		//Class.forName(driver);
    		//Connection con = DriverManager.getConnection(url,uid, pwd);
    		Connection con = dataSource.getConnection();
    		Statement st = con.createStatement();
    		
    		ResultSet rs = st.executeQuery(sql);
    		
    		if(rs.next())
    			count = rs.getInt("COUNT");		
    		
    		rs.close();
    		st.close();
    		con.close();
    		
    		return count;
    	}
    
    	public int insert(Notice notice) throws SQLException, ClassNotFoundException {
    		String title = notice.getTitle();
    		String writerId = notice.getWriterId();
    		String content = notice.getContent();
    		String files = notice.getFiles();
    		
    		String url = "jdbc:oracle:thin:@localhost:1521/xepdb1";
    		String sql = "INSERT INTO notice (    " + 
    				"    title," + 
    				"    writer_id," + 
    				"    content," + 
    				"    files" + 
    				") VALUES (?,?,?,?)";	
    		
    		//Class.forName(driver);
    				//Connection con = DriverManager.getConnection(url,uid, pwd);
    		Connection con = dataSource.getConnection();                 
    		//Statement st = con.createStatement();
    		//st.ex....(sql)
    		PreparedStatement st = con.prepareStatement(sql);
    		st.setString(1, title);
    		st.setString(2, writerId);
    		st.setString(3, content);
    		st.setString(4, files);
    		
    		int result = st.executeUpdate();
    		
    		
    		st.close();
    		con.close();
    		
    		return result;
    	}
    	
    	public int update(Notice notice) throws SQLException, ClassNotFoundException {
    		String title = notice.getTitle();
    		String content = notice.getContent();
    		String files = notice.getFiles();
    		int id = notice.getId();
    		
    		String url = "jdbc:oracle:thin:@localhost:1521/xepdb1";
    		String sql = "UPDATE NOTICE " + 
    				"SET" + 
    				"    TITLE=?," + 
    				"    CONTENT=?," + 
    				"    FILES=?" + 
    				"WHERE ID=?";
    		
    		//Class.forName(driver);
    		//Connection con = DriverManager.getConnection(url,uid, pwd);
    		Connection con = dataSource.getConnection();
    		//st.ex....(sql)
    		PreparedStatement st = con.prepareStatement(sql);
    		st.setString(1, title);
    		st.setString(2, content);
    		st.setString(3, files);
    		st.setInt(4, id);
    		
    		int result = st.executeUpdate();
    				
    		st.close();
    		con.close();
    		
    		return result;
    	}
    	
    	public int delete(int id) throws ClassNotFoundException, SQLException {
    	
    		String url = "jdbc:oracle:thin:@localhost:1521/xepdb1";
    		String sql = "DELETE NOTICE WHERE ID=?";
    		
    		//Class.forName(driver);
    		//Connection con = DriverManager.getConnection(url,uid, pwd);
    		Connection con = dataSource.getConnection();
    		//st.ex....(sql)
    		PreparedStatement st = con.prepareStatement(sql);		
    		st.setInt(1, id);
    		
    		int result = st.executeUpdate();
    				
    		st.close();
    		con.close();
    		
    		return result;
    	}
    
    	
    }

     


    Controller 객체 생성과 url mappirng을 Annotation으로 변경하기

    IndexController 주석처리

    servlet-context.xml

     

    Controller 주석을 사용하기 위해서 scan 붙여줌

    servlet-context.xml

     

    참조하고 있는 인터페이스를 지워준다 implements Controller

    기존 url mapping 정보를 지워준다.(주석 처리함

    url mapping 시킬 메서드를 아무 이름으로 만들어 준 뒤 @RequestMapping 주석 표기 

    키보드 ctrl + shift + o(영문) 커맨드를 입력하면 import를 알아서 생성하고 지워준다.

    mvc.Controller import를 지워야 @Controller주석을 쓸 수 있다.

    IndexController.java

     

    <mvc:annotation-driven /> 태그 추가 - 사용자 요청을 수반 -> @RequestMapping("/index")

    servlet-context.xml


    index실행 확인

    • 네이버 블러그 공유하기
    • 네이버 밴드에 공유하기
    • 페이스북 공유하기
    • 카카오스토리 공유하기
    loading