Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.infy.controller;
- import java.sql.SQLException;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Scope;
- import org.springframework.http.HttpStatus;
- import org.springframework.http.ResponseEntity;
- import org.springframework.stereotype.Controller;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.bind.annotation.PathVariable;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.bind.annotation.RequestMethod;
- import org.springframework.web.bind.annotation.ResponseBody;
- import org.springframework.web.context.WebApplicationContext;
- import com.infy.dto.UserDTO;
- import com.infy.service.IUserService;
- import com.infy.service.*;
- //users/user
- @Controller
- public class UserController {
- @Autowired
- WebApplicationContext context;
- @Autowired
- IUserService userService;
- @RequestMapping(value="/signUp",method = RequestMethod.POST,produces="application/json",consumes="application/json")
- public ResponseEntity <Map<String,String>> signUp(@RequestBody UserDTO userDto) {
- //System.out.println(context.isPrototype("userController"));
- System.out.println(context.getBean("dataSource"));
- Map<String,String> message=new HashMap<String,String>();
- try
- {
- message.put("message",userService.addUser(userDto));
- }
- catch(Exception e)
- {
- message.put("message","Unexpected error accured.");
- System.out.println(e.getStackTrace());
- return new ResponseEntity<Map<String,String>>(message, HttpStatus.NOT_ACCEPTABLE);
- }
- return new ResponseEntity<Map<String,String>>(message, HttpStatus.OK);
- }
- }
- package com.infy.service;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.util.Calendar;
- import java.util.Date;
- import java.util.List;
- import javax.management.RuntimeErrorException;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.context.annotation.Scope;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Isolation;
- import org.springframework.transaction.annotation.Propagation;
- import org.springframework.transaction.annotation.Transactional;
- import com.infy.constants.SignUpConstants;
- import com.infy.dto.UserDTO;
- import com.infy.mapper.UserMapper;
- @Service
- public class UserServiceImpl implements IUserService {
- @Autowired
- UserMapper userMapper;
- @Override
- @Transactional(propagation = Propagation.REQUIRED, timeout = 1, isolation = Isolation.READ_COMMITTED,rollbackFor=Exception.class)
- public String addUser(UserDTO userDto) throws Exception {
- // step 1: validate it
- // Step 2: check if the user id is not already exist;
- // Step 3: If user Id does not exist then insert it into the db
- // step 4: If exist return false;
- // step 5: something wrong then roll back
- if (validateUser(userDto)) {
- if (!isUserExist(userDto)) {
- //Date date=new Date();
- Date date=new Date();
- userDto.setInsertDate(new Timestamp(date.getTime()));
- userDto.setLastUpdate(new Timestamp(date.getTime()));
- return Integer.toString(userMapper.addUser(userDto));
- } else {
- return SignUpConstants.USER_ALREADY_EXIST;
- }
- } else {
- return SignUpConstants.VALIDATION_FALIURE;
- }
- }
- public boolean validateUser(UserDTO userDto) {
- if (userDto.getBirthDate() != null && userDto.getCity() != null && userDto.getEmail() != null
- && userDto.getFullName() != null && userDto.getMobileNo() != null && userDto.getGender() != null
- && userDto.getPassword() != null && userDto.getState() != null) {
- return true;
- } else {
- return false;
- }
- }
- @Transactional(propagation = Propagation.REQUIRED)
- public boolean isUserExist(UserDTO userDto)throws Exception {
- if (userDto != null && userMapper.getExistingUser(userDto) > 0) {
- return true;
- // TODO Auto-generated catch block
- }
- return false;
- }
- }
- package com.infy.mapper;
- import java.util.List;
- import org.apache.ibatis.annotations.Insert;
- import org.apache.ibatis.annotations.Options;
- import org.apache.ibatis.annotations.Select;
- import com.infy.dto.UserDTO;
- public interface UserMapper {
- @Insert("INSERT INTO n_users (full_name,gender,birth_date,mobile_no,email,password,city,state,insert_date,last_update) VALUES (#{fullName},#{gender},#{birthDate},#{mobileNo},#{email},#{password},#{city},#{state},#{insertDate},#{lastUpdate})")
- public int addUser(UserDTO userDto) throws Exception;
- @Select("Select count(*) from n_users where email=#{email}")
- public int getExistingUser(UserDTO userDto);
- }
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://www.springframework.org/schema/mvc
- http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx">
- <!-- <context:annotation-config /> -->
- <!-- Enable Annotation based Declarative Transaction Management -->
- <context:component-scan base-package="com.infy" />
- <mvc:annotation-driven />
- <tx:annotation-driven transaction-manager="transactionManager" />
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="typeAliasesPackage" value="com.infy.dto"/>
- <property name="mapperLocations" value="classpath*:com/infy/mapper/*.xml" />
- </bean>
- <bean id="sqlSession" class="org.mybatis.spring.SqlSessionTemplate">
- <constructor-arg index="0" ref="sqlSessionFactory" />
- </bean>
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.infy.mapper" />
- </bean>
- <mvc:interceptors>
- <bean class="com.infy.interceptors.RequestHandlerInterceptor" />
- </mvc:interceptors>
- <bean id="dataSource"
- class="org.springframework.jdbc.datasource.DriverManagerDataSource" >
- <property name="driverClassName" value="com.mysql.jdbc.Driver" />
- <property name="url" value="jdbc:mysql://localhost:3306/sample" />
- <property name="username" value="**" />
- <property name="password" value="**" />
- </bean>
- </beans>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement