Guest User

Untitled

a guest
Nov 22nd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. @Bean
  2. public DataSource dataSource() {
  3. BasicDataSource dataSource = new BasicDataSource();
  4. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  5. dataSource.setUrl("jdbc:mysql://localhost:3306/new");
  6. dataSource.setUsername("root");
  7. dataSource.setPassword("root");
  8.  
  9. return dataSource;
  10. }
  11.  
  12. private final Properties hibernateProperties() {
  13. Properties hibernateProperties = new Properties();
  14. //properties hibernateProperties.setProperty()
  15.  
  16. return hibernateProperties;
  17. }
  18.  
  19. @Bean
  20. public LocalSessionFactoryBean sessionFactory() throws ClassNotFoundException {
  21. LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
  22. sessionFactory.setDataSource(dataSource());
  23. sessionFactory.setPackagesToScan("mypackages");
  24. sessionFactory.setHibernateProperties(hibernateProperties());
  25.  
  26. return sessionFactory;
  27. }
  28.  
  29. @Bean
  30. public PlatformTransactionManager hibernateTransactionManager() throws ClassNotFoundException {
  31. HibernateTransactionManager transactionManager
  32. = new HibernateTransactionManager();
  33. transactionManager.setSessionFactory(sessionFactory().getObject());
  34. return transactionManager;
  35. }
  36.  
  37. @Repository
  38. public class EqualOpsDAOImpl implements EqualOpsDAO {
  39.  
  40. @Autowired
  41. private SessionFactory sessionFactory;
  42.  
  43.  
  44. public List<EqualOps> getEqualOps() {
  45. //get the current hibernate session
  46. Session currentSession = sessionFactory.getCurrentSession();
  47. //create query
  48. Query<EqualOps> theQuery = currentSession.createQuery("from EqualOps", EqualOps.class);
  49. //exercuty qry and get result
  50. List<EqualOps> equalOps = theQuery.getResultList();
  51. //return result
  52. return equalOps;
  53. }
  54. }
Add Comment
Please, Sign In to add comment