Skip to content

Commit 624a8be

Browse files
committed
Getting Started with Spring started
0 parents  commit 624a8be

File tree

9 files changed

+242
-0
lines changed

9 files changed

+242
-0
lines changed

.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
*/.idea/
2+
*/*.class
3+
*/*.o
4+
*/target/
5+
*.iml

GettingStartedWithSpring/pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>sec.samsung.com</groupId>
8+
<artifactId>GettingStartedWithSpring</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
11+
<dependencies>
12+
<dependency>
13+
<groupId>mysql</groupId>
14+
<artifactId>mysql-connector-java</artifactId>
15+
<version>5.1.30</version>
16+
</dependency>
17+
18+
<dependency>
19+
<groupId>org.springframework</groupId>
20+
<artifactId>spring-context</artifactId>
21+
<version>4.0.3.RELEASE</version>
22+
</dependency>
23+
24+
<dependency>
25+
<groupId>org.springframework</groupId>
26+
<artifactId>spring-jdbc</artifactId>
27+
<version>4.0.3.RELEASE</version>
28+
</dependency>
29+
30+
<dependency>
31+
<groupId>junit</groupId>
32+
<artifactId>junit-dep</artifactId>
33+
<version>4.11</version>
34+
</dependency>
35+
36+
37+
38+
</dependencies>
39+
40+
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/**
2+
* Created by style95 on 4/14/14.
3+
*/
4+
package com.samsung.sec;
5+
6+
import java.sql.Connection;
7+
import java.sql.SQLException;
8+
9+
10+
public interface ConnectionMaker {
11+
public Connection makeNewConneciton() throws ClassNotFoundException, SQLException;
12+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.samsung.sec;
2+
3+
import java.sql.Connection;
4+
import java.sql.DriverManager;
5+
import java.sql.SQLException;
6+
7+
/**
8+
* Created by style95 on 4/14/14.
9+
*/
10+
public class DConnectionMaker implements ConnectionMaker {
11+
@Override
12+
public Connection makeNewConneciton() throws ClassNotFoundException, SQLException {
13+
Class.forName("com.mysql.jdbc.Driver");
14+
Connection c = DriverManager.getConnection(
15+
"jdbc:mysql://localhost:3306/spring", "root", "qwe123");
16+
17+
return c;
18+
}
19+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/**
2+
* Created by style95 on 4/14/14.
3+
*/
4+
package com.samsung.sec;
5+
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.jdbc.datasource.SimpleDriverDataSource;
9+
10+
import javax.sql.DataSource;
11+
12+
@Configuration
13+
public class DaoFactory {
14+
@Bean
15+
public UserDao userDao() {
16+
UserDao userdao = new UserDao();
17+
userdao.setDataSource(dataSource());
18+
19+
return userdao;
20+
}
21+
22+
@Bean
23+
public DataSource dataSource() {
24+
SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
25+
26+
dataSource.setDriverClass(com.mysql.jdbc.Driver.class);
27+
dataSource.setUrl("jdbc:mysql://localhost/spring");
28+
dataSource.setUsername("root");
29+
dataSource.setPassword("qwe123");
30+
31+
return dataSource;
32+
}
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.samsung.sec;
2+
3+
public class User {
4+
String id;
5+
String name;
6+
String password;
7+
8+
public String getName() {
9+
return name;
10+
}
11+
12+
public String getPassword() {
13+
return password;
14+
}
15+
16+
public String getId() {
17+
return id;
18+
}
19+
20+
public void setId(String id) {
21+
this.id = id;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public void setPassword(String password) {
29+
this.password = password;
30+
}
31+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
2+
/**
3+
* Created by style95 on 4/14/14.
4+
*/
5+
6+
package com.samsung.sec;
7+
8+
import javax.sql.DataSource;
9+
import java.sql.*;
10+
11+
public class UserDao {
12+
private DataSource dataSource;
13+
14+
public void setDataSource(DataSource dataSource) {
15+
this.dataSource = dataSource;
16+
}
17+
public void add(User user) throws ClassNotFoundException, SQLException {
18+
Connection c = dataSource.getConnection();
19+
20+
PreparedStatement ps = c.prepareStatement("insert into users(id, name, password) values(?,?,?)");
21+
ps.setString(1, user.getId());
22+
ps.setString(2, user.getName());
23+
ps.setString(3, user.getPassword());
24+
25+
ps.executeUpdate();
26+
27+
ps.close();
28+
c.close();
29+
}
30+
31+
public User get(String id) throws ClassNotFoundException, SQLException {
32+
Connection c = dataSource.getConnection();
33+
34+
PreparedStatement ps = c.prepareStatement(
35+
"select * from users where id =?");
36+
ps.setString(1,id);
37+
38+
ResultSet rs = ps.executeQuery();
39+
rs.next();
40+
User user = new User();
41+
user.setId(rs.getString("id"));
42+
user.setName(rs.getString("name"));
43+
user.setPassword(rs.getString("password"));
44+
45+
rs.close();
46+
ps.close();
47+
c.close();
48+
49+
return user;
50+
}
51+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://www.springframework.org/schema/beans
5+
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
6+
7+
<bean id="connectionMaker" class="org.springframework.jdbc.datasource.SimpleDriverDataSource" />
8+
<bean id="userDao" class="com.samsung.sec.UserDao">
9+
<property name="dataSource" ref="connectionMaker" />
10+
</bean>
11+
</beans>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.samsung.sec;
2+
3+
import com.samsung.sec.User;
4+
import com.samsung.sec.UserDao;
5+
import org.junit.Test;
6+
import org.springframework.context.ApplicationContext;
7+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
8+
import org.springframework.context.support.GenericXmlApplicationContext;
9+
10+
import java.sql.SQLException;
11+
import java.util.Properties;
12+
13+
/**
14+
* Created by style95 on 4/14/14.
15+
*/
16+
17+
public class UserDaoTest {
18+
@Test
19+
public void doTest() throws SQLException, ClassNotFoundException {
20+
// ApplicationContext context = new GenericXmlApplicationContext("applicationContext.xml");
21+
ApplicationContext context = new AnnotationConfigApplicationContext(DaoFactory.class);
22+
23+
UserDao dao = context.getBean("userDao",UserDao.class);
24+
25+
User user = new User();
26+
user.setId("whiteship");
27+
user.setName("백기선");
28+
user.setPassword("married");
29+
30+
dao.add(user);
31+
32+
System.out.println(user.getId() + " 등록 성공");
33+
34+
User user2 = dao.get(user.getId());
35+
System.out.println(user2.getName());
36+
System.out.println(user2.getPassword());
37+
38+
System.out.println(user2.getId() + " 조회 성공");
39+
}
40+
}

0 commit comments

Comments
 (0)