Service 객체 분리 하는 밥법 => 결합력을 낮춘다.

     

    기존의 Service 객체

     

    인터페이스를 사용한 Service 객체

     

     


    Service 인터페이스 추가

    NoticeService 패키지 분리 jdbc jpa

    NoticeService 인터페이스 추가

     

    JDBCNoiticeServic에  NoiticeServic로 이름 변경

    빨간줄 import

    ListController.java

     

    implements NoticeServe 추가

    JDBCNoticeService.java

     

    JDBCNoticeService.java의 내용을 복사해서 NoticeService.java에 붙여놓고 아래와 같이 수정

     

    dispatcher-servlet.xml 수정

    새로운 클래스를 사용할경우 JDBC 부분만 바꿔주면 된다.

     

    index 실행


    Connection 정보 분리

    기존의 Connection 코드 를 빼서 xml에 두고 객체화 DI 한다.

     

    Connection 정보를 담을 수 있는 인터페이스 DataSource 사용하기

    xml에서 바인딩할수 있는 객체를 set하기 위해 setter 만들기

    JDBCNoticeService.java

     

    DataSource 연결하기

    JDBCNoticeService.java

    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 com.newlecture.web.entity.Notice;
    import com.newlecture.web.service.NoticeService;
    
    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";
    	 */
    	
    	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;
    	}
    
    	
    }

     

    만들어준 setter로 xml 설정

    dataSorce로 DB정보 연결

    dispatcher-servlet.xml

     

    dispatcher-servlet.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:mvc="http://www.springframework.org/schema/mvc"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
            https://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/mvc
            https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    
        <bean name="/index" class="com.newlecture.web.controller.IndexController" />  
    	 <bean name="/notice/list" class="com.newlecture.web.controller.notice.ListController" >
    	 	<property name="noticeService" ref="noticeService"/>
    	 </bean> 
    	  <bean name="/notice/detail" class="com.newlecture.web.controller.notice.DetailController" />   
    
    	<bean
    		class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    		<property name="viewClass"
    			value="org.springframework.web.servlet.view.tiles3.TilesView" />
    		<property name="order" value="1" />
    	</bean>
    
    	<bean
    		class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
    		<property name="definitions" value="/WEB-INF/tiles.xml" />
    	</bean>
    	
    	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    		<property name="prefix" value="/WEB-INF/view/"></property>
    		<property name="suffix" value=".jsp"></property>
    		<property name="order" value="2" />
    	</bean>
    	
    	<mvc:resources location="/static/" mapping="/**"></mvc:resources>
    	
    	<bean id="noticeService" class="com.newlecture.web.service.jdbc.JDBCNoticeService" >
    		<property name="dataSource" ref="dataSource"></property>
    	</bean>
    	
    	<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
    		<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
    		<property name="url" value="jdbc:oracle:thin:@localhost:1521/xepdb1" />
    		<property name="username" value="NEWLEC" />
    		<property name="password" value="11111" />
    	</bean>
    	
    </beans>

     

    pom.xml에 maven 추가


    index 실행

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