<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.hsqldb</groupId> <artifactId>hsqldb</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> </dependencies>
CREATE TABLE users(userId INTEGER NOT NULL,userName VARCHAR(100) NOT NULL,userEmail VARCHAR(100) DEFAULT NULL,address VARCHAR(100) DEFAULT NULL,PRIMARY KEY (userId));
insert into users(userId, userName, userEmail, address) values (1000, 'Dinesh', 'dinesh@gmail.com', 'Delhi'); insert into users(userId, userName, userEmail, address) values (1001, 'Kumar', 'kumar@gmail.com', 'Greater Noida'); insert into users(userId, userName, userEmail, address) values (1002, 'Rajput', 'rajput@gmail.com', 'Noida');
/**
*
*/
package com.dineshonjava.model;
/**
* @author Dinesh.Rajput
*
*/
public class User {
private Integer userId;
private String userName;
private String userEmail;
private String address;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserEmail() {
return userEmail;
}
public void setUserEmail(String userEmail) {
this.userEmail = userEmail;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [userId=" + userId + ", userName=" + userName
+ ", userEmail=" + userEmail + ", address=" + address + "]";
}
}
/**
*
*/
package com.dineshonjava.service;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.dineshonjava.model.User;
import com.dineshonjava.utils.UserRowMapper;
/**
* @author Dinesh.Rajput
*
*/
@Service
public class UserService {
@Autowired
private JdbcTemplate jdbcTemplate;
@Transactional(readOnly=true)
public List<User> findAll() {
return jdbcTemplate.query("select * from users",
new UserRowMapper());
}
@Transactional(readOnly=true)
public User findUserById(int id) {
return jdbcTemplate.queryForObject(
"select * from users where userId=?",
new Object[]{id}, new UserRowMapper());
}
public User create(final User user)
{
final String sql = "insert into users(userId,userName,userEmail,address) values(?,?,?,?)";
KeyHolder holder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {
@Override
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setInt(1, user.getUserId());
ps.setString(2, user.getUserName());
ps.setString(3, user.getUserEmail());
ps.setString(4, user.getAddress());
return ps;
}
}, holder);
int newUserId = holder.getKey().intValue();
user.setUserId(newUserId);
return user;
}
}
/**
*
*/
package com.dineshonjava.utils;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.springframework.jdbc.core.RowMapper;
import com.dineshonjava.model.User;
/**
* @author Dinesh.Rajput
*
*/
public class UserRowMapper implements RowMapper<User>{
@Override
public User mapRow(ResultSet rs, int rowNum) throws SQLException {
User user = new User();
user.setUserId(rs.getInt("userId"));
user.setUserName(rs.getString("userName"));
user.setUserEmail(rs.getString("userEmail"));
user.setAddress(rs.getString("address"));
return user;
}
}
/**
*
*/
package com.dineshonjava.controller;
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.dineshonjava.model.User;
import com.dineshonjava.service.UserService;
/**
* @author Dinesh.Rajput
*
*/
@RestController
public class UserController {
@Autowired
UserService userService;
@RequestMapping("/")
User home(User user) {
user = userService.create(user);
return user;
}
@RequestMapping("/users")
List<User> findAllUsers() {
List<User> users = userService.findAll();
return users;
}
}
package com.dineshonjava;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringBootDataBaseApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootDataBaseApplication.class, args);
}
}
spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/dojdb spring.datasource.username=root spring.datasource.password=root
# Number of ms to wait before throwing an exception if no connection is available. spring.datasource.tomcat.max-wait=10000 # Maximum number of active connections that can be allocated from this pool at the same time. spring.datasource.tomcat.max-active=50 # Validate the connection before borrowing it from the pool. spring.datasource.tomcat.test-on-borrow=true
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.zaxxer</groupId>
<artifactId>HikariCP</artifactId>
</dependency>
spring.datasource.jndi-name=java:tomcat/datasources/users
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>
@Entity
public class User implements Serializable{
@Id
private Integer userId;
private String userName;
private String userEmail;
private String address
// setters & getters}
package com.dineshonjava.domain;
import org.springframework.data.domain.*;
import org.springframework.data.repository.*;
public interface UserRepository extends CrudRepository {
}
@Service
public class UserService {
@Autowired
private UserRepository userRepository;
@Transactional(readOnly=true)
public List<User> findAll() {
return userRepository.findAll();
}
}
Spring Boot Related Topics
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…