Add JPA and MySQL configuration
No, hibernate related XML. Add some properties in application.properties file
Also add bellow dependencies
No, hibernate related XML. Add some properties in application.properties file
#Hibernate
spring.datasource.url= jdbc:mysql://localhost:3306/springboot
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username= root
spring.datasource.password= root
#JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.6</version> </dependency>
Create User Class
We extends AbstractPersistable class. Then we don't need
Primary key (It's automatically genarated)
Field-based annotation(@Column)
Primary key (It's automatically genarated)
Field-based annotation(@Column)
package com.sb.test.model;
import javax.persistence.Entity;
import org.springframework.data.jpa.domain.AbstractPersistable;
@Entity
public class User extends AbstractPersistable<Long>{
//Don't need to primary key and column annotation.
private String userId;
private String userName;
private String password;
public String getUserId() {
return userId;
}
public void setUserId(String userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
Create database springboot. Then run code
Automatically generate the table for the users. Column name separated by an underscore.
Create sql file to insert the data.
Under resource folder.
Not Yet
Create UserController class
Create UserService Interface
Create UserServiceImple class
Create UserRepository Interface
Create UserController class
Create UserController class
File Structure
Now Run Application.
Open ARC or Postman
URL - http://localhost:9080/springbootbasic/user/list
method -get
If you have data in the database then you can retrieve the data
Automatically generate the table for the users. Column name separated by an underscore.
Create sql file to insert the data.
Under resource folder.
Not Yet
Create UserController class
package com.sb.test.model;
package com.sb.basic.coontroller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sb.basic.model.User;
import com.sb.basic.service.UserService;
@RestController
@RequestMapping("/user")
public class UserController {
private UserService userService;
@Autowired
public UserController(UserService userService) {
this.userService = userService;
}
@RequestMapping("/list")
public List<User> userList(){
return userService.userList();
}
}
Create UserService Interface
package com.sb.basic.service;
import java.util.List;
import com.sb.basic.model.User;
public interface UserService {
List<User> userList();
}
package com.sb.basic.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
}
Create UserRepository Interface
package com.sb.basic.repository;
import org.springframework.data.jpa.repository.JpaRepository;
import com.sb.basic.model.User;
public interface UserRepository extends JpaRepository<User, Long>{
}
Create UserController class
package com.sb.basic.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
}
Create UserController class
package com.sb.basic.service.impl;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sb.basic.model.User;
import com.sb.basic.repository.UserRepository;
import com.sb.basic.service.UserService;
@Service
public class UserServiceImpl implements UserService {
private UserRepository userRepository;
//Better go with constructor injection so no need to put @Autwired again and again
@Autowired
public UserServiceImpl(UserRepository userRepository) {
this.userRepository = userRepository;
}
@Override
public List<User> userList() {
return userRepository.findAll();
}
}
File Structure
Now Run Application.
Open ARC or Postman
URL - http://localhost:9080/springbootbasic/user/list
method -get
If you have data in the database then you can retrieve the data
No comments:
Post a Comment