Changing code to use interfaces instead of implementations.
This commit is contained in:
parent
d6fc28e120
commit
e26215578c
@ -1,16 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beansProjectDescription>
|
||||
<version>1</version>
|
||||
<pluginVersion><![CDATA[3.8.2.201610040608-RELEASE]]></pluginVersion>
|
||||
<configSuffixes>
|
||||
<configSuffix><![CDATA[xml]]></configSuffix>
|
||||
</configSuffixes>
|
||||
<enableImports><![CDATA[false]]></enableImports>
|
||||
<configs>
|
||||
<config>src/main/resources/beans.xml</config>
|
||||
</configs>
|
||||
<autoconfigs>
|
||||
</autoconfigs>
|
||||
<configSets>
|
||||
</configSets>
|
||||
</beansProjectDescription>
|
@ -1,71 +0,0 @@
|
||||
<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>com.memory.dao</groupId>
|
||||
<artifactId>memory-dao-test</artifactId>
|
||||
<version>0.0.1</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>1.7</maven.compiler.source>
|
||||
<maven.compiler.target>1.7</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>4.12</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-lang3</artifactId>
|
||||
<version>3.0</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
<version>5.1.25</version>
|
||||
</dependency>
|
||||
|
||||
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
<version>1.4.193</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-core</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-jdbc</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-test</artifactId>
|
||||
<version>4.2.5.RELEASE</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -1,22 +0,0 @@
|
||||
package com.memory.dao;
|
||||
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
|
||||
import com.memory.dao.db.UserDAO;
|
||||
import com.memory.dao.pojo.User;
|
||||
|
||||
public class App {
|
||||
public static void main(String[] args) {
|
||||
|
||||
final ApplicationContext context = new ClassPathXmlApplicationContext(
|
||||
"file:src/main/resources/beans.xml");
|
||||
|
||||
final UserDAO dao = (UserDAO)context.getBean("userDao");
|
||||
for (final User user : dao.findAll()) {
|
||||
System.out.println(user);
|
||||
}
|
||||
|
||||
((ClassPathXmlApplicationContext)context).close();
|
||||
}
|
||||
}
|
@ -1,9 +0,0 @@
|
||||
package com.memory.dao;
|
||||
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(basePackages = {"com.memory.dao"})
|
||||
public class AppConfig {
|
||||
}
|
@ -1,19 +0,0 @@
|
||||
package com.memory.dao.db;
|
||||
|
||||
public enum Queries {
|
||||
|
||||
GET_USER("SELECT * FROM users WHERE name = :name"),
|
||||
GET_ALL_USERS("SELECT * FROM users")
|
||||
;
|
||||
|
||||
private final String query;
|
||||
|
||||
Queries(final String query) {
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public String get() {
|
||||
return this.query;
|
||||
}
|
||||
|
||||
}
|
@ -1,11 +0,0 @@
|
||||
package com.memory.dao.db;
|
||||
|
||||
import com.memory.dao.pojo.User;
|
||||
import java.util.List;
|
||||
|
||||
public interface UserDAO {
|
||||
|
||||
User findByName(String name);
|
||||
List<User> findAll();
|
||||
|
||||
}
|
@ -1,61 +0,0 @@
|
||||
package com.memory.dao.db;
|
||||
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.HashMap;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.jdbc.core.RowMapper;
|
||||
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import com.memory.dao.pojo.User;
|
||||
|
||||
@Repository
|
||||
public class UserDAOImpl implements UserDAO {
|
||||
|
||||
private NamedParameterJdbcTemplate namedParameterJDBCTemplate;
|
||||
|
||||
@Autowired
|
||||
public void setNamedParameterJDBCTemplate(final NamedParameterJdbcTemplate namedParameterJDBCTemplate) {
|
||||
this.namedParameterJDBCTemplate = namedParameterJDBCTemplate;
|
||||
}
|
||||
|
||||
@Override
|
||||
public User findByName(final String name) {
|
||||
final Map<String, Object> params = new HashMap<>();
|
||||
params.put("name", name);
|
||||
|
||||
final User user = namedParameterJDBCTemplate.
|
||||
queryForObject(Queries.GET_USER.get(), params, new UserMapper());
|
||||
|
||||
System.out.println("Found: " + user);
|
||||
|
||||
return user;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<User> findAll() {
|
||||
|
||||
Map<String, Object> params = new HashMap<String, Object>();
|
||||
|
||||
final List<User> result = namedParameterJDBCTemplate.query(Queries.GET_ALL_USERS.get(), params, new UserMapper());
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
private static final class UserMapper implements RowMapper<User> {
|
||||
@Override
|
||||
public User mapRow(final ResultSet rs, final int rowNum) throws SQLException {
|
||||
final User user = new User();
|
||||
user.setId(rs.getInt("id"));
|
||||
user.setName(rs.getString("name"));
|
||||
user.setEmail(rs.getString("email"));
|
||||
return user;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -1,38 +0,0 @@
|
||||
package com.memory.dao.pojo;
|
||||
|
||||
public class User {
|
||||
|
||||
private int id;
|
||||
private String name;
|
||||
private String email;
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(final int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(final String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public String getEmail() {
|
||||
return email;
|
||||
}
|
||||
|
||||
public void setEmail(final String email) {
|
||||
this.email = email;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User [id=" + id + ", name=" + name + ", email=" + email + "]";
|
||||
}
|
||||
|
||||
}
|
@ -1,20 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<import resource="db-h2-config.xml" />
|
||||
|
||||
<bean id="userDao" class="com.memory.dao.db.UserDAOImpl">
|
||||
<property name="namedParameterJDBCTemplate" ref="jdbcTemplate" />
|
||||
</bean>
|
||||
|
||||
<bean id="jdbcTemplate"
|
||||
class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate"> <!-- <property name="dataSource" ref="dataSource" /> -->
|
||||
<!-- <constructor-arg ref="dbcpDataSource" /> -->
|
||||
<constructor-arg ref="dataSource" />
|
||||
</bean>
|
||||
|
||||
</beans>
|
@ -1,18 +0,0 @@
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:context="http://www.springframework.org/schema/context"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
|
||||
|
||||
xsi:schemaLocation="
|
||||
http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
|
||||
http://www.springframework.org/schema/jdbc
|
||||
http://www.springframework.org/schema/jdbc/spring-jdbc-4.1.xsd
|
||||
http://www.springframework.org/schema/context
|
||||
http://www.springframework.org/schema/context/spring-context-4.1.xsd">
|
||||
|
||||
<jdbc:embedded-database id="dataSource" type="H2">
|
||||
<jdbc:script location="classpath:db/sql/create-db.sql" />
|
||||
<jdbc:script location="classpath:db/sql/insert-data.sql" />
|
||||
</jdbc:embedded-database>
|
||||
|
||||
</beans>
|
@ -1,7 +0,0 @@
|
||||
--DROP TABLE users IF EXISTS;
|
||||
|
||||
CREATE TABLE users (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name VARCHAR(30),
|
||||
email VARCHAR(50)
|
||||
);
|
@ -1,3 +0,0 @@
|
||||
INSERT INTO users VALUES (1, 'mkyong', 'mkyong@gmail.com');
|
||||
INSERT INTO users VALUES (2, 'alex', 'alex@yahoo.com');
|
||||
INSERT INTO users VALUES (3, 'joel', 'joel@gmail.com');
|
@ -1,30 +0,0 @@
|
||||
package com.memory.dao;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
|
||||
import com.memory.dao.db.UserDAO;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration("/beans.xml")
|
||||
public class AppTest {
|
||||
|
||||
@Autowired
|
||||
private UserDAO dao;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
System.out.println(String.format("Dao is: %s", dao));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testApp() {
|
||||
assertTrue(true);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user