<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<Spring BackEnd>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Configuration
@Configuration
@EnableTransactionManagement
@PropertySource("classpath:application.properties") public class WebRootConfig { @Autowired private Environment env; @Bean public LocalContainerEntityManagerFactoryBean entityManagerFactory(DataSource ds, JpaVendorAdapter jpaVendorAdapter){ LocalContainerEntityManagerFactoryBean emfb = new LocalContainerEntityManagerFactoryBean();
emfb.setDataSource(ds);
emfb.setJpaVendorAdapter(jpaVendorAdapter);
emfb.setPackagesToScan("lk.ijse.hotelbackend.entity");
return emfb; } @Bean public DataSource dataSource(){ DriverManagerDataSource ds = new DriverManagerDataSource();
ds.setDriverClassName(env.getRequiredProperty("javax.persistence.jdbc.driver"));
ds.setUrl(env.getRequiredProperty("javax.persistence.jdbc.url"));
ds.setUsername(env.getRequiredProperty("javax.persistence.jdbc.user"));
ds.setPassword(env.getRequiredProperty("javax.persistence.jdbc.password"));
return ds; } @Bean public JpaVendorAdapter jpaVendorAdapter(){ HibernateJpaVendorAdapter jpa = new HibernateJpaVendorAdapter();
jpa.setDatabase(Database.MYSQL);
jpa.setDatabasePlatform(env.getRequiredProperty("hibernate.dialect"));
jpa.setGenerateDdl(true);
jpa.setShowSql(true);
return jpa; } @Bean public PlatformTransactionManager transactionManager(EntityManagerFactory emf){ return new JpaTransactionManager(emf);
}
}
webappconfig
@Configuration
@EnableWebMvc
@ComponentScan("lk.ijse.hotelbackend")
@EnableJpaRepositories(basePackageClasses = {RoomRepository.class,
FacilitiesRepository.class,
RoomFacilitiesRepository.class,
UserRepository.class})
public class WebAppConfig implements WebMvcConfigurer {
@Override
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
}
webappinitializer
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{WebRootConfig.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebAppConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
Customer Controller
@RestController @CrossOrigin @RequestMapping("/api/crud-hibernate/v1/customer") public class CustomerController { @Autowired private CustomerService customerService; @RequestMapping(value = "add") public void addCusto(HttpServletResponse resp, HttpServletRequest req) { String id = req.getParameter("id"); String name = req.getParameter("name"); String address = req.getParameter("address"); double salary = Double.parseDouble(req.getParameter("salary")); Customer custome = new Customer(); custome.setAddress(address); custome.setId(id); custome.setSalary(salary); custome.setName(name); boolean b = customerService.addCustomer(custome); if (b) { try { resp.getWriter().print("Done"); } catch (IOException e) { e.printStackTrace(); } }else{ try { resp.getWriter().print("Error"); } catch (IOException e) { e.printStackTrace(); } } } @RequestMapping(value = "delete") public void deleteCusto(HttpServletResponse resp, HttpServletRequest req) { } @RequestMapping(value = "update") public void updateCusto(HttpServletResponse resp, HttpServletRequest req) { } @RequestMapping(value = "search") public void searcCusto(HttpServletResponse resp, HttpServletRequest req) { } @RequestMapping(value = "/find-all", method = RequestMethod.GET) public List<Customer> findAllCustomers() { return customerService.findAll(); } }
Customer Entity
@Entity public class Customer { private String id; private String name; private String address; private double salary; public Customer() { }@ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "orderID",referencedColumnName = "orderID",insertable = false,updatable = false) private Orders orders; @ManyToOne(cascade = CascadeType.ALL) @JoinColumn(name = "itemCode",referencedColumnName = "itemCode",insertable = false,updatable = false) private Item item;
@ManyToOne(cascade = CascadeType.ALL) private Customer customer; @OneToMany(cascade = CascadeType.ALL) private List<OrderDetail> orderDetailList;
@Id @Column(name = "id", nullable = false, length = 10) public String getId() { return id; } public void setId(String id) { this.id = id; } @Basic @Column(name = "name", nullable = true, length = 15) public String getName() { return name; } public void setName(String name) { this.name = name; } @Basic @Column(name = "address", nullable = true, length = 20) public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } }
CustomerRepository
@Repository @Transactional public class CustomerRepository { @Autowired private SessionFactory sessionFactory; private Session currentSession() { return sessionFactory.getCurrentSession(); } public boolean add(Customer dto) { Serializable save = currentSession().save(dto); if (save != null) { return true; } return false; } public boolean delete(String id) { currentSession().delete(searchCustomer(id)); return true; } public Customer searchCustomer(String id) { Customer customer = (Customer) currentSession().get(id, Customer.class); return customer; } public boolean updateCustomer(Customer dto){ currentSession().update(dto); return true; } public List<Customer> findAll() { return currentSession().createQuery("from Customer a").list(); } }
CustomerService
@Service public class CustomerService { @Autowired CustomerRepository customerRepository; public boolean addCustomer(Customer customer){ return customerRepository.add(customer); } public boolean updateCustomer(Customer customer){ return customerRepository.updateCustomer(customer); } public boolean deleteCustomer(String id){ return customerRepository.delete(id); } public Customer serachCustomer(String id){ return customerRepository.searchCustomer(id); } public List<Customer> findAll(){ return customerRepository.findAll(); } }
Property File
boot
boot
spring.datasource.url=jdbc:mysql://localhost/SpringBootPos?createDatabaseIfNotExist=true&useSSL=falsespring.datasource.username=rootspring.datasource.password=kavinduk0spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=truespring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL57Dialect
mvc
javax.persistence.jdbc.driver=com.mysql.jdbc.Driver
javax.persistence.jdbc.url=jdbc:mysql://localhost:3306/xxx?createDatabaseIfNotExist=true
javax.persistence.jdbc.user=root
javax.persistence.jdbc.password=kavinduk0
@Service @Transactional(propagation = Propagation.SUPPORTS)
@Transactional(propagation = Propagation.REQUIRED)
No comments:
Post a Comment