2017년 11월 6일 월요일

[Spring] DB setting

Dependencies:

Add these on POM.xml


<project>
    ...
    </build>
    <dependencies>
        // You write Dependencies here.
    </dependencies>
</project>

Spring context


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

Spring core


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

Spring jdbc


<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>

Commons-dbcp


<dependency>
    <groupId>commons-dbcp</groupId>
    <artifactId>commons-dbcp</artifactId>
    <version>1.4</version>
</dependency>

MySQL connector

Download MySQL Connector
Project Folder Right Click > Build Path > Configure Build Path > Add External JARs > (Add this jar file)

Insert

  1. BookDaoSpring.java
  2. Test.java
  3. applicationContext.xml

BookDaoSpring.java

public class BookDaoSpring {
    // dao relies on this Object
    private JdbcTemplate jdbcTemplate;
    // Constructor, Setter for DI
    public BookDaoSpring() {}
    public BookDaoSpring(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }
////////////////////////////////////////////////////////
    public int insert(BookVO book) {
        String sql = 
            "INSERT INTO BOOK(TITLE,PUBLISHER,PRICE,WRITER)"
            +"VALUES(?,?,?,?)"; 
        return jdbcTemplate.update(sql,
                book.getTitle(),
                book.getPublisher(),
                book.getPrice(),
                book.getWriter());
    }
}

Test.java

public class Test {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("test02_spring/applicationContext.xml");
        BookDaoSpring dao = context.getBean("dao", BookDaoSpring.class);
  
        BookVO book = new BookVO("Spring textbook", "meme", 30000, "Samsung");
 System.out.println("insert Result"+dao.insert(book));
    }
}

applicationContext.xml

Right Click its package > New > Spring Bean Configuration File > name it 'applicationContext'

<bean id="ddd" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
 <property name="url" value="jdbc:mysql://127.0.0.1./spring"/>
 <property name="username" value="root"/>
 <property name="password" value="sds1501"/>
</bean>

<bean id="jjjj" class="org.springframework.jdbc.core.JdbcTemplate">
 <property name="dataSource" ref="ddd"/>
</bean>

<bean id="dao" class="test02_spring.BookDaoSpring">
 <property name="jdbcTemplate" ref="jjjj"/>
</bean>
  
  
  
 

 

            
 

        
            
        

2017년 11월 5일 일요일

[Spring] Two ways to implements.

Sample.java

package test01_scope;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("sss")
@Scope("prototype")
public class Sample {
 //
 }

Test.java

Sample sample1 = context.getBean("sss", Sample.class);

application.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd">
<bean class="test01_scope.Sample" id="sample1" scope="prototype"/>
<bean class="test01_scope.Sample" id="sample2" scope="singleton"/>

<context:annotation-config/>
<context:component-scan base-package="test01_scope"/>
</beans>

2017년 11월 2일 목요일

[Spring]Beginning of Spring

There are many ways to implements Spring Project.

How to Make a Maven Project(for Beginner):

  1. Create 'Java Project'
  2. Right-Click the Project folder > Configure > Convert to Maven Project > Finish
  3. src > Spring Bean Configuration File(.xml) > name it 'applicationContext'

Then, it creates a POM.xml file. This is not the way you do in the company but to make it look clear without many files and complex file tree, you can start it in this way.
In the company, you create a Maven Project directly.

ref)
maven: accumulator of knowledge
POM: Project Object Model

Then, you have two choices to import Spring Library.

  1. Download: mvnrepository(spring-corespring-context)
  2. Edit POM.xml: 

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>Day02_DI</groupId>
  <artifactId>Day02_DI</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.6.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
    </plugins>
  </build>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-core</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>
  <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.2.5.RELEASE</version>
</dependency>
</dependencies>
</project>

Then, create 'spring Bean Configuration File' into 'src' folder with a name -  'applicationContext.xml'.


@autoWired