This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UserDaoImpl extends CommonDaoImpl implements UserDAO { | |
@Override | |
public int createUser(User user) throws HibernateException { | |
return super.saveEntity(user); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Entity | |
public class User { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private int id; | |
private String firstName; | |
private String lastName; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Tests { | |
private static final Logger logger = Logger.getLogger(Tests.class.getName()); | |
private UserDAO userDao; | |
public Tests() { | |
this.userDao = new UserDaoImpl(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version='1.0' encoding='utf-8'?> | |
<!DOCTYPE hibernate-configuration PUBLIC | |
"-//Hibernate/Hibernate Configuration DTD//EN" | |
"http://hibernate.sourceforge.net/hibernate-configuration-5.0.dtd"> | |
<hibernate-configuration> | |
<session-factory> | |
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> | |
<property name="hibernate.connection.url">jdbc:mysql://localhost:3306/testDB</property> | |
<property name="hibernate.connection.username">root</property> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="UTF-8"?> | |
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | |
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | |
<modelVersion>4.0.0</modelVersion> | |
<groupId>com.jeetprksh</groupId> | |
<artifactId>hibernate-examples</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<name>hibernate-examples</name> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create table if not exists `users` ( | |
`id` int not null auto_increment, | |
`firstName` varchar(20), | |
`lastName` varchar(20), | |
`age` int, | |
primary key (`id`) | |
); | |
insert into `users` values (1, "john", "doe", 29); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Service | |
public class UserServiceImpl implements UserService { | |
@Autowired | |
private UserDao userDao; | |
@Override | |
@Transactional(readOnly = true) | |
public ServerResponse getUser(Long userId) { | |
ServerResponse response = new ServerResponse(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Repository | |
public class UserDaoImpl implements UserDao { | |
@Autowired | |
private SessionFactory sessionFactory; | |
@Override | |
public UserDTO getUser(Long userId) { | |
TypedQuery<UserDTO> typedQuery = sessionFactory.getCurrentSession().createQuery("from UserDTO where id=" + userId.toString()); | |
return typedQuery.getSingleResult(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@RestController() | |
@RequestMapping("user") | |
public class UserController { | |
@Autowired | |
private UserService service; | |
@RequestMapping(value = "{userId}", method = RequestMethod.GET) | |
public @ResponseBody ServerResponse getUser(@PathVariable("userId") Long userId) { | |
return this.service.getUser(userId); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
@EnableWebMvc | |
@ComponentScan("com.spring5.app") | |
public class WebMvcConfig extends WebMvcConfigurationSupport { | |
@Override | |
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) { | |
converters.add(customJackson2HttpMessageConverter()); | |
super.addDefaultHttpMessageConverters(converters); | |
} |
NewerOlder