Posts

Showing posts from April, 2017

4. DAO

@Repository public class LoginDaoImpl implements LoginDao{ @PersistenceContext private EntityManager entityManager; public EntityManager getEntityManager() { return entityManager; } public void setEntityManager(EntityManager entityManager) { this.entityManager = entityManager; } @Override @Transactional public User createUser(User newUser) { //get password hash newUser.setPassword(getHashedPassword(newUser)); entityManager.persist(newUser); entityManager.flush(); return newUser; } @Override @Transactional public User authenticateUser(User user) { Query query = entityManager.createQuery("FROM User WHERE userName = :userName AND password = :password"); query.setParameter("userName", user.getUserName()); query.setParameter("password", getHashedPassword(user)); User u = null; try { u = (User) query.getSingleResult(); } catch (Exception e) { // there is no record for th...

3. Controller

@RestController //@CrossOrigin(origins = "http://localhost:3000") public class LoginController { private LoginService loginService; public LoginService getLoginService() { return loginService; } @Autowired public void setLoginService(LoginService loginService) { this.loginService = loginService; } @RequestMapping(value = "/user/create", method = RequestMethod.POST) public ResponseEntity<String> createUser(@RequestBody User newUser){ User createdUser = loginService.createUser(newUser); return new ResponseEntity<String>("User Created", HttpStatus.OK); } @RequestMapping(value = "/user/login", method = RequestMethod.POST) public ResponseEntity<User> logUser(@RequestBody User user){ User loggedUser = loginService.authenticateUser(user); if(loggedUser != null){ return new ResponseEntity<User>(loggedUser, HttpStatus.ACCEPTED); }else{ //user does not exsi...

2. Model classes

JPA related Stuff 1. Model class annotations @Entity @Table(name = "user") public class User {  @Id @Column(name = "id", unique = true, nullable = false) @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column(name = "user_name") private String userName; @Column(name = "password") private String password; public User() { super(); } public User(int id, String userName, String password) { super(); this.id = id; this.userName = userName; this.password = password; } }

1. java prject

Getting Started with the project Go to https://start.spring.io/ Add dependencies:- Web, JPA, MySQL Download and Extract the project Import to Eclipse as an existing maven project Create following packages: model, controller, service, dao, config Goto src/main/resources and add followings to the application.properties file # Database db.driver: com.mysql.jdbc.Driver db.url: jdbc:mysql://localhost:3306/loginapp db.username: root db.password: 64956495 # Hibernate hibernate.dialect: org.hibernate.dialect.MySQL5Dialect hibernate.show_sql: true #hibernate.hbm2ddl.auto: create-drop hibernate.hbm2ddl.auto: validate #hibernate.hbm2ddl.auto: update entitymanager.packagesToScan: com        7. Create DatabaseConfig.java file inside config package package com.viraj.loginapp.config; import java.util.Properties; import javax.sql.DataSource; import org.springframework.beans.factory.annotation.Autowired; import org.springframew...