Commit e615b1e5 by jorgecacs

Add dao implementation and config

parent 2d82b510
Pipeline #2 failed with stages
in 0 seconds
......@@ -31,11 +31,21 @@
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
<version>2.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-web-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
......
package com.rshka.mcs.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@EnableWebMvc
@Configuration
public class AppConfiguration extends WebMvcConfigurationSupport {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder().generateUniqueName(false).setName("testdb")
.setType(EmbeddedDatabaseType.H2).addDefaultScripts().setScriptEncoding("UTF-8").ignoreFailedDrops(true)
.build();
}
@Bean
public NamedParameterJdbcTemplate namedParamJdbcTemplate() {
NamedParameterJdbcTemplate namedParamJdbcTemplate = new NamedParameterJdbcTemplate(dataSource());
return namedParamJdbcTemplate;
}
public class AppConfiguration extends AnnotationConfigWebApplicationContext {
}
package com.rshka.mcs.configuration;
import javax.sql.DataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
@Configuration
public class DBConfiguration {
@Bean
public DataSource dataSource() {
return new EmbeddedDatabaseBuilder()
.generateUniqueName(false).
setName("testdb")
.setType(EmbeddedDatabaseType.H2)
.addDefaultScripts()
.setScriptEncoding("UTF-8")
.ignoreFailedDrops(true)
.build();
}
@Bean
public NamedParameterJdbcTemplate namedParamJdbcTemplate() {
NamedParameterJdbcTemplate namedParamJdbcTemplate = new NamedParameterJdbcTemplate(dataSource());
return namedParamJdbcTemplate;
}
}
package com.rshka.mcs.configuration;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.h2.server.web.WebServlet;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class WebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return null;
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[] { AppConfiguration.class };
}
@Override
protected String[] getServletMappings() {
return new String[] { "/" };
}
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
super.onStartup(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("h2-console", new WebServlet());
servlet.setLoadOnStartup(2);
servlet.addMapping("/console/*");
}
}
......@@ -3,30 +3,40 @@ package com.rshka.mcs.dao;
import java.util.List;
import java.util.Map;
import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;
import com.rshka.mcs.beans.Customer;
import com.rshka.mcs.mapper.CustomerMapper;
@Repository
public class CustomerDaoImpl implements CustomerDao {
private final String CUSTOMER_TABLE = "CUSTOMER";
private final String SCHEMA_NAME = "";
private final String SELECT_CUSTOMER_QUERY = "select * from customer where document_number = ?";
private final String SELECT_CUSTOMER_QUERY = "select * from customer where document_number = :document";
private final String SELECT_CUSTOMERS_QUERY = "select * from customer";
private final String INSERT_CUSTOMER_QUERY = "insert into customer(document_number, name, address, phone) values (?, ?, ?, ?)";
@Autowired
private JdbcTemplate jdbcTemplate;
private DataSource dataSource;
@Autowired
NamedParameterJdbcTemplate jdbcTemplate;
/**
* Return a customer by the specified document
......@@ -34,8 +44,11 @@ public class CustomerDaoImpl implements CustomerDao {
public Customer getCustomer(String document) {
try {
Customer c = jdbcTemplate.queryForObject(SELECT_CUSTOMER_QUERY, new Object[] { document },
new CustomerMapper());
MapSqlParameterSource params = new MapSqlParameterSource();
params.addValue("document", document);
Customer c = jdbcTemplate.queryForObject(SELECT_CUSTOMER_QUERY, params, new CustomerMapper());
return c;
......@@ -71,7 +84,7 @@ public class CustomerDaoImpl implements CustomerDao {
try {
SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate);
SimpleJdbcInsert insert = new SimpleJdbcInsert(jdbcTemplate.getJdbcTemplate());
insert.withTableName(CUSTOMER_TABLE).usingGeneratedKeyColumns("id");
......
INSERT INTO person(name, document, address, phone)
VALUES ('First', '1234566', 'First avenue', '123 21213');
INSERT INTO person(name, document, address, phone)
VALUES ('Second', '11232', 'Third avenue', '444 030');
INSERT INTO person(name, document, address, phone)
VALUES ('Second', '1298986', 'Seventh avenue', '123 111');
\ No newline at end of file
DROP TABLE IF EXISTS person;
CREATE TABLE person(
id NUMERIC IDENTITY PRIMARY KEY,
name VARCHAR(512) NOT NULL,
document VARCHAR(512) NOT NULL,
address VARCHAR(512) NOT NULL,
phone VARCHAR(512)
);
\ No newline at end of file
<?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:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">
<mvc:annotation-driven>
</mvc:annotation-driven>
<context:component-scan
base-package="com.rshka.mcs" />
<!-- Use in-memory embedded H2 database -->
<jdbc:embedded-database id="dataSource"
type="H2">
</jdbc:embedded-database>
<!-- Run H2 web server within application that will access the same in-memory
database -->
<bean id="h2Server" class="org.h2.tools.Server"
factory-method="createTcpServer" init-method="start"
destroy-method="stop" depends-on="h2WebServer">
<constructor-arg
value="-tcp,-tcpAllowOthers,-tcpPort,9092" />
</bean>
<bean id="h2WebServer" class="org.h2.tools.Server"
factory-method="createWebServer" init-method="start"
destroy-method="stop">
<constructor-arg
value="-web,-webAllowOthers,-webPort,8082" />
</bean>
<bean factory-bean="dataSource" factory-method="getObject" />
</beans>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">
<display-name>demo_mcs</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<display-name>demo_mcs</display-name>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
\ No newline at end of file
#Generated by Maven Integration for Eclipse
#Wed Jan 15 21:27:27 PYST 2020
#Thu Jan 16 07:31:55 PYST 2020
version=0.0.1-SNAPSHOT
groupId=com.rshk.demo.mcs
m2e.projectName=demo_mcs
......
......@@ -31,11 +31,21 @@
<version>1.18.10</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
<version>2.9.4</version>
</dependency>
<!-- https://mvnrepository.com/artifact/javax/javaee-web-api -->
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
......@@ -60,4 +70,4 @@
</plugin>
</plugins>
</build>
</project>
\ No newline at end of file
</project>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment