Commit 0ca1c686 authored by Syed Ilyas Uddin's avatar Syed Ilyas Uddin

Adding customer crud app

parents
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store
\ No newline at end of file
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CheckStyle-IDEA" serialisationVersion="2">
<checkstyleVersion>10.12.5</checkstyleVersion>
<scanScope>JavaOnly</scanScope>
<copyLibs>true</copyLibs>
<option name="thirdPartyClasspath" />
<option name="activeLocationIds" />
<option name="locations">
<list>
<ConfigurationLocation id="bundled-sun-checks" type="BUNDLED" scope="All" description="Sun Checks">(bundled)</ConfigurationLocation>
<ConfigurationLocation id="bundled-google-checks" type="BUNDLED" scope="All" description="Google Checks">(bundled)</ConfigurationLocation>
</list>
</option>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
<option name="workspaceImportForciblyTurnedOn" value="true" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
<mapping directory="$PROJECT_DIR$/spring-mvc" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
<artifactId>customer-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>5.2.7.RELEASE</version>
</dependency>
<dependency>
<groupId>jstl</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.20</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>5.4.24.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>1.0.0.GA</version>
</dependency>
<dependency>
<groupId>com.mchange</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.5.5</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>3.0.0</version> <!-- Choose the appropriate version -->
</dependency>
<dependency>
<groupId>jakarta.transaction</groupId>
<artifactId>jakarta.transaction-api</artifactId>
<version>2.0.0</version> <!-- Choose the appropriate version -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>customer-app</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
\ No newline at end of file
package com.customerdemo.controller;
import com.customerdemo.entity.Customer;
import com.customerdemo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
@RequestMapping("/customer")
public class CustomerController {
private final CustomerService customerService;
@Autowired
public CustomerController(CustomerService customerService) {
this.customerService = customerService;
}
@GetMapping("/list")
public String listCustomer(Model model){
// if record not available in database then getCustomers() given an error
try{
List<Customer> customers = customerService.getCustomers();
model.addAttribute("customers", customers);
}catch (Exception e){
model.addAttribute("customers", null);
}
return "customers-list";
}
@GetMapping("/show-customer-form")
public String showCustomerForm(Model model){
model.addAttribute("customer", new Customer());
return "customer-form";
}
@GetMapping("/update-form")
public String updateCustomerForm(@RequestParam("id")int id, Model model){
model.addAttribute("customer", customerService.getCustomer(id));
return "customer-form";
}
@PostMapping("/save")
public String saveCustomer(@ModelAttribute("customer") Customer customer){
customerService.saveOrUpdate(customer);
return "redirect:/customer/list";
}
@GetMapping("/delete")
public String deleteCustomer(@RequestParam("id") int id){
customerService.delete(id);
return "redirect:/customer/list";
}
}
package com.customerdemo.dao;
import com.customerdemo.entity.Customer;
import java.util.List;
public interface CustomerDAO {
List<Customer> getCustomers();
void saveOrUpdate(Customer customer);
Customer getCustomer(int id);
void delete(int id);
}
package com.customerdemo.dao;
import com.customerdemo.entity.Customer;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
class CustomerDAOImpl implements CustomerDAO{
private final SessionFactory sessionFactory;
@Autowired
public CustomerDAOImpl(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public List<Customer> getCustomers() {
Session session = sessionFactory.getCurrentSession();
Query<Customer> query;
query = session.createQuery("from Customer", Customer.class);
return query.getResultList();
}
@Override
public void saveOrUpdate(Customer customer) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(customer);
}
@Override
public Customer getCustomer(int id) {
Session session = sessionFactory.getCurrentSession();
return session.get(Customer.class,id);
}
@Override
public void delete(int id) {
Session session = sessionFactory.getCurrentSession();
Query query = session.createQuery("delete from Customer c where c.Id =:id");
query.setParameter("id",id);
query.executeUpdate();
}
}
package com.customerdemo.entity;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private String name;
private String email;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
package com.customerdemo.service;
import com.customerdemo.entity.Customer;
import java.util.List;
public interface CustomerService {
List<Customer> getCustomers();
void saveOrUpdate(Customer customer);
Customer getCustomer(int id);
void delete(int id);
}
package com.customerdemo.service.impl;
import com.customerdemo.dao.CustomerDAO;
import com.customerdemo.entity.Customer;
import com.customerdemo.service.CustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
@Service
public class CustomerServiceImpl implements CustomerService {
private final CustomerDAO customerDAO;
@Autowired
public CustomerServiceImpl(CustomerDAO customerDAO) {
this.customerDAO = customerDAO;
}
@Override
@Transactional
public List<Customer> getCustomers() {
return customerDAO.getCustomers();
}
@Override
@Transactional
public void saveOrUpdate(Customer customer) {
customerDAO.saveOrUpdate(customer);
}
@Override
@Transactional
public Customer getCustomer(int id) {
return customerDAO.getCustomer(id);
}
@Override
@Transactional
public void delete(int id) {
customerDAO.delete(id);
}
}
<?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"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- Add support for component scanning -->
<context:component-scan base-package="com.customerdemo" />
<!-- Add support for conversion, formatting and validation support -->
<mvc:annotation-driven/>
<!-- Define Spring MVC view resolver -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/view/" />
<property name="suffix" value=".jsp" />
</bean>
<!-- Step 1: Define Database DataSource / connection pool -->
<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close">
<property name="driverClass" value="com.mysql.cj.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/customercrud" />
<property name="user" value="root" />
<property name="password" value="root" />
<!-- these are connection pool properties for C3P0 -->
<property name="initialPoolSize" value="5"/>
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="20" />
<property name="maxIdleTime" value="30000" />
</bean>
<!-- Step 2: Setup Hibernate session factory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="myDataSource" />
<property name="packagesToScan" value="com.customerdemo" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</bean>
<!-- Step 3: Setup Hibernate transaction manager -->
<bean id="myTransactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<!-- Step 4: Enable configuration of transactional behavior based on annotations -->
<tx:annotation-driven transaction-manager="myTransactionManager" />
<!-- Add support for reading web resources: css, images, js, etc ... -->
<mvc:resources location="/resources/" mapping="/resources/**"/>
</beans>
\ No newline at end of file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<head>
<title>Add Customer</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css">
</head>
<body>
<div class="container">
<div class="wrapper">
<header class="header">
<h2>Customer Detail</h2>
</header>
<div class="form-wrapper">
<form:form cssClass="form" action="save" modelAttribute="customer" method="post">
<form:hidden path="id"/>
<div class="form-group">
Name: <form:input path="name" class="form-control"/>
</div>
<div class="form-group">
Email: <form:input path="email" class="form-control"/>
</div>
<div class="form-group">
<input type="submit" class="btn">
</div>
</form:form>
</div>
</div>
</div>
</body>
</html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Customers List</title>
<link rel="stylesheet" href="${pageContext.request.contextPath}/resources/css/style.css">
</head>
<body>
<div class="container">
<div class="wrapper">
<header class="header">
<h2>Customer Details</h2>
</header>
<div class="content">
<a href="show-customer-form" class="btn">Add Customer</a>
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Action</th>
</tr>
<c:forEach var="customer" items="${customers}">
<c:url var="updateLink" value="/customer/update-form">
<c:param name="id" value="${customer.id}"/>
</c:url>
<c:url var="deleteLink" value="/customer/delete">
<c:param name="id" value="${customer.id}"/>
</c:url>
<tr>
<td>${customer.name}</td>
<td>${customer.email}</td>
<td>
<a href="${updateLink}" class="btn-link">Update</a> |
<a href="${deleteLink}" class="btn-link" onclick="if(!(confirm('Are you sure you want to delete this customer?'))) return false">Delete</a>
</td>
</tr>
</c:forEach>
</table>
</div>
</div>
</div>
</body>
\ No newline at end of file
<html>
<head>
<title>Customer List</title>
<link rel="stylesheet" href="../../resources/css/style.css">
</head>
<body>
<div class="container">
<div class="wrapper">
<header class="header">
<h2>CRM Customer Relation Manager</h2>
</header>
<div class="form-wrapper">
<form action="save-customer" class="form">
<div class="form-group">
Name: <input type="text" class="form-control">
</div>
<div class="form-group">
Email: <input type="text" class="form-control">
</div>
<div class="form-group">
<input type="submit" class="btn">
</div>
</form>
</div>
</div>
</div>
</body>
</html>
\ 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://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<display-name>spring-mvc-customer-demo</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc-crud-demo-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
\ No newline at end of file
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Customer List</title>
</head>
<body>
<% response.sendRedirect("customer/list"); %>
</body>
</html>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
.container {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
}
.wrapper {
padding: 0 6rem;
width: 100%;
}
.header {
color: #ffffff;
text-align: center;
background-color: rgb(36, 114, 5);
padding: 1.6rem 0;
}
.btn {
display: inline-block;
color: #ffffff;
background-color: #2280ec;
padding: 0.6rem 1rem;
margin: 1rem 0;
border-radius: 3px;
text-decoration: none;
font-weight: 600;
border: none;
outline: none;
cursor: pointer;
}
.btn:hover {
background-color: #136dd3;
}
.content table {
width: 100%;
border-collapse: collapse;
text-align: left;
}
.content table tr,
th,
td {
padding: 1rem;
color: #000000;
}
.content table tr th {
background-color: rgb(221, 26, 26);
font-weight: bold;
font-size: 18px;
color: #000000;
}
.content table tr:nth-child(even) {
background-color: #c4c4c4;
}
.content table tr:nth-child(odd) {
background-color: #FF8A65;
}
.form-wrapper {
background-color: #f0ffff;
display: flex;
justify-content: center;
align-items: center;
}
.form {
display: flex;
justify-content: center;
flex-direction: column;
}
.form-group {
margin-top: 1rem;
}
.form-control {
padding: 0.6rem 0.2rem;
}
.error{
color: #ff0000;
}
.btn-link{
text-decoration: none;
font-weight: 600;
color: #3b3b3b;
}
.btn-link:hover{
color: #000000;
}
\ No newline at end of file
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