Advertisement
Guest User

Untitled

a guest
Aug 19th, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 18.38 KB | None | 0 0
  1. //AppConfig.java
  2. package com.mindtree.configuration;
  3.  
  4. import org.springframework.context.annotation.ComponentScan;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.web.servlet.config.annotation.EnableWebMvc;
  7.  
  8. @Configuration
  9. @EnableWebMvc
  10. @ComponentScan(basePackages = {"com.mindtree"})
  11. public class AppConfig {
  12.  
  13.  
  14. }
  15. //-------------------------------------------------------------------------------------------
  16.  
  17. //AppInitializer.java
  18.  
  19. package com.mindtree.configuration;
  20.  
  21. import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
  22.  
  23. public class AppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer
  24. {
  25.  
  26. @Override
  27. protected Class<?>[] getRootConfigClasses() {
  28. return new Class[] {AppConfig.class};
  29. }
  30.  
  31. @Override
  32. protected Class<?>[] getServletConfigClasses() {
  33.  
  34. return new Class[] {};
  35. }
  36.  
  37. @Override
  38. protected String[] getServletMappings() {
  39.  
  40. return new String[] {"/"};
  41. }
  42.  
  43. }
  44. //-------------------------------------------------------------------------------
  45. //HibernateConfiguration.java
  46.  
  47. package com.mindtree.configuration;
  48. import java.util.Properties;
  49.  
  50. import javax.sql.DataSource;
  51.  
  52. import org.hibernate.SessionFactory;
  53. import org.springframework.beans.factory.annotation.Autowired;
  54. import org.springframework.context.annotation.Bean;
  55. import org.springframework.context.annotation.ComponentScan;
  56. import org.springframework.context.annotation.Configuration;
  57. import org.springframework.context.annotation.PropertySource;
  58. import org.springframework.core.env.Environment;
  59. import org.springframework.jdbc.datasource.DriverManagerDataSource;
  60. import org.springframework.orm.hibernate5.HibernateTransactionManager;
  61. import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
  62. import org.springframework.transaction.annotation.EnableTransactionManagement;
  63.  
  64. @Configuration
  65. @ComponentScan(basePackages = { "com.mindtree" })
  66. @EnableTransactionManagement
  67. @PropertySource(value = { "classpath:application.properties" })
  68. public class HibernateConfiguration {
  69.  
  70. @Autowired
  71. private Environment environment;
  72.  
  73. @Bean
  74. public LocalSessionFactoryBean sessionFactory() {
  75. String[] entityPackage = new String[] { "com.mindtree" };
  76. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  77. sessionFactory.setDataSource(dataSource());
  78. sessionFactory.setPackagesToScan(entityPackage);
  79. sessionFactory.setHibernateProperties(hibernateProperties());
  80. return sessionFactory;
  81. }
  82.  
  83. @Bean
  84. public DataSource dataSource() {
  85. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  86. dataSource.setDriverClassName(environment.getRequiredProperty("jdbc.driverClass"));
  87. dataSource.setUrl(environment.getRequiredProperty("jdbc.url"));
  88. dataSource.setUsername(environment.getRequiredProperty("jdbc.username"));
  89. dataSource.setPassword(environment.getRequiredProperty("jdbc.password"));
  90. return dataSource;
  91. }
  92.  
  93. private Properties hibernateProperties() {
  94. Properties properties = new Properties();
  95. properties.put("hibernate.dialect", environment.getRequiredProperty("hibernate.dialect"));
  96. properties.put("hibernate.show_sql", environment.getRequiredProperty("hibernate.show_sql"));
  97. properties.put("hibernate.format_sql", environment.getRequiredProperty("hibernate.format_sql"));
  98. properties.put("hibernate.hbm2ddl.auto", environment.getRequiredProperty("hibernate.hbm2ddl.auto"));
  99. properties.put("hibernate.connection.release_mode", environment.getRequiredProperty("hibernate.connection.release_mode"));
  100. properties.put("hibernate.connection.shutdown", environment.getRequiredProperty("hibernate.connection.shutdown"));
  101. return properties;
  102. }
  103.  
  104. @Bean
  105. @Autowired
  106. public HibernateTransactionManager transactionManager(SessionFactory s) {
  107. HibernateTransactionManager txManager = new HibernateTransactionManager();
  108. txManager.setSessionFactory(s);
  109. return txManager;
  110. }
  111. }
  112.  
  113. //-----------------------------------------------------------------------------------------------------------------------------------
  114.  
  115. //EmployeeController.java
  116.  
  117. package com.mindtree.controller;
  118.  
  119. import java.util.List;
  120.  
  121. import org.springframework.beans.factory.annotation.Autowired;
  122. import org.springframework.http.HttpStatus;
  123. import org.springframework.http.ResponseEntity;
  124. import org.springframework.http.ResponseEntity.BodyBuilder;
  125. import org.springframework.web.bind.annotation.DeleteMapping;
  126. import org.springframework.web.bind.annotation.GetMapping;
  127. import org.springframework.web.bind.annotation.PathVariable;
  128. import org.springframework.web.bind.annotation.PostMapping;
  129. import org.springframework.web.bind.annotation.PutMapping;
  130. import org.springframework.web.bind.annotation.RequestBody;
  131. import org.springframework.web.bind.annotation.RestController;
  132.  
  133. import com.mindtree.dto.ResultDTO;
  134. import com.mindtree.entity.Employee;
  135. import com.mindtree.service.EmployeeService;
  136.  
  137. @RestController
  138. public class EmployeeController {
  139.  
  140. @Autowired
  141. private EmployeeService employeeService;
  142.  
  143. @PostMapping("/employee/")
  144.  
  145. public ResponseEntity<ResultDTO> save(@RequestBody Employee e)
  146. {
  147. ResultDTO result = new ResultDTO();
  148.  
  149. int id = (int) employeeService.save(e);
  150. if(id>0)
  151. {
  152. result.setStatusCode(HttpStatus.OK.value());
  153. result.setStatus("Success");
  154. result.setMessage("Added Successfully");
  155. return new ResponseEntity<ResultDTO>(result,HttpStatus.OK);
  156. }
  157. else
  158. {
  159. result.setStatusCode(HttpStatus.BAD_REQUEST.value());
  160. result.setStatus("Failure");
  161. result.setMessage("Mismatch");
  162. return new ResponseEntity<ResultDTO>(result,HttpStatus.BAD_REQUEST);
  163. }
  164.  
  165. }
  166.  
  167.  
  168.  
  169. @GetMapping("/employee/{id}")
  170. public ResponseEntity<ResultDTO> get(@PathVariable("id") int id) //Response Entity to accompany http response along with return body
  171. {
  172. ResultDTO result = new ResultDTO();
  173. Employee e = employeeService.get(id);
  174. if(e != null)
  175. {
  176. result.setStatusCode(HttpStatus.OK.value());
  177. result.setStatus("Success");
  178. result.setMessage("Added Successfully");
  179. return new ResponseEntity<ResultDTO>(result,HttpStatus.OK);
  180. }
  181. else
  182. {
  183. result.setStatusCode(HttpStatus.BAD_REQUEST.value());
  184. result.setStatus("Failure");
  185. result.setMessage("Mismatch");
  186. return new ResponseEntity<ResultDTO>(result,HttpStatus.NOT_FOUND);
  187. }
  188. }
  189.  
  190.  
  191.  
  192.  
  193. @GetMapping(path = "/employee"/*,produces = {"application/xml"}*/)
  194. public ResponseEntity<?> list()
  195. {
  196. List<Employee> result = employeeService.list();
  197. if(result.isEmpty())
  198. {
  199. return ((BodyBuilder)ResponseEntity.noContent()).body("No Employee Found");
  200. }
  201. return new ResponseEntity<List<Employee>>(result, HttpStatus.OK);
  202. }
  203.  
  204.  
  205.  
  206.  
  207. @PutMapping("/employee/{id}")
  208. public ResponseEntity<ResultDTO> update(@PathVariable("id") int id,@RequestBody Employee e)
  209. {
  210. ResultDTO result = new ResultDTO();
  211. String m = employeeService.update(id,e);
  212. if(m == null)
  213. {
  214. result.setStatusCode(HttpStatus.OK.value());
  215. result.setStatus("Success");
  216. result.setMessage("Succesfully Updated Employee "+id);
  217. return new ResponseEntity<ResultDTO>(result,HttpStatus.OK);
  218. }
  219. else
  220. {
  221. result.setStatusCode(HttpStatus.NOT_FOUND.value());
  222. result.setStatus("Failure");
  223. result.setMessage(m);
  224. return new ResponseEntity<ResultDTO>(result,HttpStatus.NOT_FOUND);
  225. }
  226.  
  227. }
  228.  
  229.  
  230.  
  231.  
  232.  
  233. @DeleteMapping("/employee/{id}")
  234. public ResponseEntity<?> delete(@PathVariable("id") int id)
  235. {
  236. ResultDTO result = new ResultDTO();
  237. String m = employeeService.delete(id);
  238. if(m == null)
  239. {
  240. result.setStatusCode(HttpStatus.OK.value());
  241. result.setStatus("Success");
  242. result.setMessage("Succesfully Deleted Employee "+id);
  243. return new ResponseEntity<ResultDTO>(result,HttpStatus.OK);
  244. }
  245. else
  246. {
  247. result.setStatusCode(HttpStatus.NOT_FOUND.value());
  248. result.setStatus("Failure");
  249. result.setMessage(m);
  250. return new ResponseEntity<ResultDTO>(result,HttpStatus.NOT_FOUND);
  251. }
  252. }
  253. }
  254. //-------------------------------------------------------------------------------------------------------------------------------
  255.  
  256. //EmployeeDaoImpl.java
  257.  
  258. package com.mindtree.dao.daoImpl;
  259.  
  260. import java.util.List;
  261.  
  262. import javax.persistence.criteria.CriteriaBuilder;
  263. import javax.persistence.criteria.CriteriaQuery;
  264. import javax.persistence.criteria.Root;
  265.  
  266. import org.hibernate.Session;
  267. import org.hibernate.SessionFactory;
  268. import org.hibernate.query.Query;
  269. import org.springframework.beans.factory.annotation.Autowired;
  270. import org.springframework.stereotype.Repository;
  271. import org.springframework.transaction.annotation.Transactional;
  272.  
  273. import com.mindtree.dao.EmployeeDao;
  274. import com.mindtree.entity.Employee;
  275.  
  276. @Repository
  277. public class EmployeeDaoImpl implements EmployeeDao{
  278.  
  279. @Autowired
  280. private SessionFactory sessionFactory;
  281. @Transactional
  282. public int save(Employee employee)
  283. {
  284. sessionFactory.getCurrentSession().save(employee);
  285. System.out.println(employee.getId());
  286. return employee.getId();
  287.  
  288. }
  289. public Employee get(int id) {
  290. return sessionFactory.getCurrentSession().get(Employee.class, id);
  291. }
  292.  
  293. public List<Employee> list() {
  294. Session session = sessionFactory.getCurrentSession();
  295. CriteriaBuilder cb = session.getCriteriaBuilder();
  296. CriteriaQuery<Employee> cq = cb.createQuery(Employee.class);
  297. Root<Employee> root = cq.from(Employee.class);
  298. cq.select(root);
  299. Query<Employee> query = session.createQuery(cq);
  300. return query.getResultList();
  301. }
  302.  
  303. public String update(int id, Employee employee) {
  304. Session session = sessionFactory.getCurrentSession();
  305. Employee emp = session.byId(Employee.class).load(id);
  306. String m = null;
  307. if(emp!=null)
  308. {
  309. emp.setUsername(employee.getUsername());
  310. emp.setPassword(employee.getPassword());
  311. emp.setFullname(employee.getFullname());
  312. emp.setEmail(employee.getEmail());
  313. emp.setGender(employee.getGender());
  314. emp.setDob(employee.getDob());
  315. emp.setSecuQues(employee.getSecuQues());
  316. emp.setSecuAnswer(employee.getSecuAnswer());
  317. session.update(emp);
  318. session.flush();
  319. }
  320. else
  321. {
  322. m = "Does not Exist";
  323. }
  324.  
  325. return m;
  326. }
  327. public String delete(int id) {
  328. Session session = sessionFactory.getCurrentSession();
  329. String m = null;
  330. Employee emp = session.byId(Employee.class).load(id);
  331. if(emp!=null)
  332. {
  333. session.delete(emp);
  334. }
  335. else
  336. {
  337. m = "Not Found";
  338. }
  339. return m;
  340. }
  341. }
  342. //-------------------------------------------------------------------------------------------------------------------------------
  343.  
  344. //EmployeeDao.java
  345.  
  346. package com.mindtree.dao;
  347.  
  348. import java.util.List;
  349.  
  350. import com.mindtree.entity.Employee;
  351.  
  352. public interface EmployeeDao {
  353.  
  354. public int save(Employee employee);
  355. public Employee get(int id);
  356. public List<Employee> list();
  357. public String update(int id, Employee employee);
  358. public String delete(int id);
  359.  
  360. }
  361. //----------------------------------------------------------------------------------------------------------------------------------
  362.  
  363. //ResultDTO.java
  364.  
  365. package com.mindtree.dto;
  366.  
  367. public class ResultDTO {
  368.  
  369. private String Status;
  370. private int StatusCode;
  371. private String Message;
  372.  
  373.  
  374. public String getStatus() {
  375. return Status;
  376. }
  377. public void setStatus(String status) {
  378. Status = status;
  379. }
  380. public int getStatusCode() {
  381. return StatusCode;
  382. }
  383. public void setStatusCode(int i) {
  384. StatusCode = i;
  385. }
  386. public String getMessage() {
  387. return Message;
  388. }
  389. public void setMessage(String message) {
  390. Message = message;
  391. }
  392.  
  393.  
  394. }
  395. //----------------------------------------------------------------------------------------------------------------------------------
  396.  
  397. //Employee.java
  398.  
  399. package com.mindtree.entity;
  400.  
  401. import javax.persistence.Entity;
  402. import javax.persistence.GeneratedValue;
  403. import javax.persistence.GenerationType;
  404. import javax.persistence.Id;
  405.  
  406. @Entity
  407. public class Employee {
  408. @Id
  409. @GeneratedValue(strategy=GenerationType.IDENTITY)
  410. int id;
  411. String username;
  412. String password;
  413. String fullname;
  414. String email;
  415. String dob;
  416. String gender;
  417. String secuQues;
  418. String secuAnswer;
  419.  
  420. public String getUsername() {
  421. return username;
  422. }
  423. public void setUsername(String username) {
  424. this.username = username;
  425. }
  426. public String getPassword() {
  427. return password;
  428. }
  429. public void setPassword(String password) {
  430. this.password = password;
  431. }
  432. public String getFullname() {
  433. return fullname;
  434. }
  435. public void setFullname(String fullname) {
  436. this.fullname = fullname;
  437. }
  438. public String getEmail() {
  439. return email;
  440. }
  441. public void setEmail(String email) {
  442. this.email = email;
  443. }
  444. public String getDob() {
  445. return dob;
  446. }
  447. public void setDob(String dob) {
  448. this.dob = dob;
  449. }
  450. public String getGender() {
  451. return gender;
  452. }
  453. public void setGender(String gender) {
  454. this.gender = gender;
  455. }
  456. public String getSecuQues() {
  457. return secuQues;
  458. }
  459. public void setSecuQues(String secuQues) {
  460. this.secuQues = secuQues;
  461. }
  462. public String getSecuAnswer() {
  463. return secuAnswer;
  464. }
  465. public void setSecuAnswer(String secuAnswer) {
  466. this.secuAnswer = secuAnswer;
  467. }
  468. public int getId() {
  469. return id;
  470. }
  471.  
  472.  
  473.  
  474. }
  475. //---------------------------------------------------------------------------------------------------------------------------------
  476.  
  477. //EmployeeServiceImpl.java
  478.  
  479. package com.mindtree.service.serviceImpl;
  480.  
  481. import java.util.ArrayList;
  482. import java.util.List;
  483.  
  484. import org.springframework.beans.factory.annotation.Autowired;
  485. import org.springframework.stereotype.Service;
  486. import org.springframework.transaction.annotation.Transactional;
  487.  
  488. import com.mindtree.dao.EmployeeDao;
  489. import com.mindtree.entity.Employee;
  490. import com.mindtree.service.EmployeeService;
  491.  
  492. @Service
  493. @Transactional
  494. public class EmployeeServiceImpl implements EmployeeService{
  495.  
  496. @Autowired
  497. EmployeeDao dao;
  498.  
  499. public int save(Employee em) {
  500. Employee emp = new Employee();
  501. emp.setUsername(em.getUsername());
  502. emp.setPassword(em.getPassword());
  503. emp.setFullname(em.getFullname());
  504. emp.setEmail(em.getEmail());
  505. emp.setGender(em.getGender());
  506. emp.setDob(em.getDob());
  507. emp.setSecuQues(em.getSecuQues());
  508. emp.setSecuAnswer(em.getSecuAnswer());
  509. int empid = dao.save(emp);
  510. return empid;
  511. }
  512.  
  513. public Employee get(int id) {
  514. Employee emp = dao.get(id);
  515. Employee e = new Employee();
  516. if(emp != null)
  517. {
  518. e.setUsername(emp.getUsername());
  519. e.setPassword(emp.getPassword());
  520. e.setFullname(emp.getFullname());
  521. e.setEmail(emp.getEmail());
  522. e.setGender(emp.getGender());
  523. e.setDob(emp.getDob());
  524. e.setSecuQues(emp.getSecuQues());
  525. e.setSecuAnswer(emp.getSecuAnswer());
  526. }
  527. else
  528. {
  529. e = null;
  530. }
  531. return e;
  532. }
  533.  
  534. @Transactional
  535. public List<Employee> list() {
  536. List<Employee> e = dao.list();
  537. List<Employee> emp = new ArrayList<Employee>();
  538. if(!e.isEmpty())
  539. {
  540. for(Employee em: e)
  541. {
  542. Employee emp1 = new Employee();
  543. emp1.setUsername(em.getUsername());
  544. emp1.setPassword(em.getPassword());
  545. emp1.setFullname(em.getFullname());
  546. emp1.setEmail(em.getEmail());
  547. emp1.setGender(em.getGender());
  548. emp1.setDob(em.getDob());
  549. emp1.setSecuQues(em.getSecuQues());
  550. emp1.setSecuAnswer(em.getSecuAnswer());
  551. emp.add(emp1);
  552. }
  553. }
  554. return emp;
  555. }
  556.  
  557. @Transactional
  558. public String update(int id, Employee e) {
  559. String m = dao.update(id, e);
  560. return m;
  561.  
  562. }
  563.  
  564. @Transactional
  565. public String delete(int id) {
  566. String m = dao.delete(id);
  567. return m;
  568. }
  569.  
  570. }
  571. //---------------------------------------------------------------------------------------------------------------------------------
  572.  
  573. //EmployeeService.java
  574.  
  575. package com.mindtree.service;
  576.  
  577. import java.util.List;
  578.  
  579. import com.mindtree.entity.Employee;
  580.  
  581. public interface EmployeeService {
  582.  
  583. int save(Employee e);
  584.  
  585. Employee get(int id);
  586.  
  587. List<Employee> list();
  588.  
  589. String update(int id, Employee e);
  590.  
  591. String delete(int id);
  592.  
  593. }
  594. //-------------------------------------------------------------------------------------------------------------------------------
  595.  
  596. //pom.xml
  597.  
  598. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  599. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  600. <modelVersion>4.0.0</modelVersion>
  601. <groupId>com.mindtree</groupId>
  602. <artifactId>Employee</artifactId>
  603. <packaging>war</packaging>
  604. <version>0.0.1-SNAPSHOT</version>
  605. <name>Employee Maven Webapp</name>
  606. <url>http://maven.apache.org</url>
  607. <properties>
  608. <org.springframework.version>5.0.7.RELEASE</org.springframework.version>
  609. <jackson.databind.version>2.9.5</jackson.databind.version>
  610. <hibernate.version>5.2.12.Final</hibernate.version>
  611. <mysql.connector.version>5.1.46</mysql.connector.version>
  612. </properties>
  613. <dependencies>
  614.  
  615. <!-- Jackson -->
  616. <dependency>
  617. <groupId>com.fasterxml.jackson.core</groupId>
  618. <artifactId>jackson-databind</artifactId>
  619. <version>${jackson.databind.version}</version>
  620. </dependency>
  621. <!-- Spring -->
  622.  
  623. <dependency>
  624. <groupId>org.springframework</groupId>
  625. <artifactId>spring-webmvc</artifactId>
  626. <version>${org.springframework.version}</version>
  627. </dependency>
  628. <dependency>
  629. <groupId>org.springframework</groupId>
  630. <artifactId>spring-tx</artifactId>
  631. <version>${org.springframework.version}</version>
  632. </dependency>
  633. <dependency>
  634. <groupId>org.springframework</groupId>
  635. <artifactId>spring-orm</artifactId>
  636. <version>${org.springframework.version}</version>
  637. </dependency>
  638.  
  639. <!-- Hibernate -->
  640. <dependency>
  641. <groupId>org.hibernate</groupId>
  642. <artifactId>hibernate-core</artifactId>
  643. <version>${hibernate.version}</version>
  644. </dependency>
  645.  
  646. <!-- MySQL -->
  647. <dependency>
  648. <groupId>mysql</groupId>
  649. <artifactId>mysql-connector-java</artifactId>
  650. <version>${mysql.connector.version}</version>
  651. </dependency>
  652.  
  653.  
  654. <!-- Servlet -->
  655. <dependency>
  656. <groupId>javax.servlet</groupId>
  657. <artifactId>servlet-api</artifactId>
  658. <version>2.5</version>
  659. <scope>provided</scope>
  660. </dependency>
  661.  
  662. <!-- Test -->
  663. <dependency>
  664. <groupId>junit</groupId>
  665. <artifactId>junit</artifactId>
  666. <version>4.12</version>
  667. <scope>test</scope>
  668. </dependency>
  669. </dependencies>
  670.  
  671. <build>
  672. <finalName>Employee</finalName>
  673. </build>
  674. </project>
  675. //------------------------------------------------------------------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement