Professional Documents
Culture Documents
MySQL
Andreas Bsk Follow
The goal of this project was a simple CRM (Customer Relationship Management) tool,
where users with proper authentication and authorization would be able to perform
CRUD operations on a customer data set.
The technology stack used was Java Spring 5, Hibernate, MySQL and basic HTML with
CSS.
The Implementation
Starting with creating two new database schema with one table each, one to hold the
application users:
USE `users_db`;
)
and one to hold the actual data, e.g the customers:
Using a properties file to hold the JDBC and pool connection properties, a few Spring
annotations ( @PropertySource, @Autowired ) and a DataSource, we can establish a
@Autowired
@Bean
try {
securityDataSource.setDriverClass(env.getProperty("security.jdbc.driv
er"));
}
securityDataSource.setJdbcUrl(env.getProperty("security.jdbc.url"));
securityDataSource.setUser(env.getProperty("security.jdbc.user"));
securityDataSource.setPassword(env.getProperty("security.jdbc.passwor
d"));
securityDataSource.setInitialPoolSize(
getIntProperty("security.connection.pool.initialPoolSize"));
securityDataSource.setMinPoolSize(
getIntProperty("security.connection.pool.minPoolSize"));
securityDataSource.setMaxPoolSize(
getIntProperty("security.connection.pool.maxPoolSize"));
securityDataSource.setMaxIdleTime(
getIntProperty("security.connection.pool.maxIdleTime"));
return securityDataSource;
In order for users to be able to access pages and perform CRUD operations on the
customer data set (based on their role), the following HTTP configuration was added:
@Override
http.authorizeRequests()
.antMatchers("/customer/showForm*").hasAnyRole("MANAGER", "ADMIN")
.antMatchers("/customer/save*").hasAnyRole("MANAGER", "ADMIN")
.antMatchers("/customer/delete").hasRole("ADMIN")
.antMatchers("/customer/**").hasRole("EMPLOYEE")
.antMatchers("/resources/**").permitAll()
.and()
.formLogin()
.loginPage("/showMyLoginPage")
.loginProcessingUrl("/authenticateTheUser")
.permitAll()
.and()
.logout().permitAll()
.and()
.exceptionHandling().accessDeniedPage("/access-denied");
In the above block, additional configuration was added to override Spring’s default login
form (to add a custom one with some CSS ) and also Tomcat’s default access denied
error page.
@Controller
@GetMapping("/showMyLoginPage")
return "fancy-login";
@GetMapping("/access-denied")
public String showAccessDenied() {
return "access-denied";
Even though log in form was overridden, authentication code was not required as
redirecting the data to /authenticateTheUser lets Spring handle that for us.
Moving forward with the customer data, a new simple class with getters and setters was
created to hold the basic customer info:
@Entity
@Table(name="customer")
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="id")
@Column(name="first_name")
@Column(name="last_name")
@Column(name="email")
}
Using @Entity, @Table and @Column annotations, Spring understands how these
properties match the MySQL tables.
@Service
@Autowired
@Override
@Transactional
return customerDAO.getCustomers();
@Override
@Transactional
customerDAO.saveCustomer(theCustomer);
@Override
@Transactional
return customerDAO.getCustomer(theId);
@Override
@Transactional
customerDAO.deleteCustomer(theId);
@Repository
@Autowired
@Override
// create a query
Query<Customer> theQuery =
Customer.class);
return customers;
}
@Override
currentSession.saveOrUpdate(theCustomer);
@Override
return theCustomer;
@Override
Query theQuery =
theQuery.setParameter("customerId", theId);
theQuery.executeUpdate();
} }
Finally, a customer controller was added to handle the URL mapping, render the views
and make the appropriate service calls:
@Controller
@RequestMapping("/customer")
@Autowired
@GetMapping("/list")
theModel.addAttribute("customers", theCustomers);
return "list-customers";
@GetMapping("/showFormForAdd")
theModel.addAttribute("customer", theCustomer);
return "customer-form";
@PostMapping("/saveCustomer")
customerService.saveCustomer(theCustomer);
return "redirect:/customer/list";
@GetMapping("/showFormForUpdate")
Model theModel) {
theModel.addAttribute("customer", theCustomer);
return "customer-form";
@GetMapping("/delete")
customerService.deleteCustomer(theId);
return "redirect:/customer/list";
}
}
It’s worth mentioning that those controller functions use Spring’s Model to achieve data
binding on the rendered views.
The Application
Here are some screenshots of the project:
Spring Hibernate Java MySQL Software Development