Advertisement
Guest User

Untitled

a guest
May 28th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:mvc="http://www.springframework.org/schema/mvc"
  5. xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
  13. http://www.springframework.org/schema/context
  14. http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  15.  
  16. <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
  17. <context:component-scan base-package="someproject" />
  18. <!-- <context:annotation-config /> -->
  19.  
  20.  
  21. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
  22. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
  23. <property name="url" value="jdbc:mysql://localhost:3306/somedb" />
  24. <property name="username" value="xxx" />
  25. <property name="password" value="yyy" />
  26. </bean>
  27.  
  28. <bean id="transactionManager"
  29. class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  30. <property name="dataSource" ref="dataSource"></property>
  31. </bean>
  32.  
  33. @Service
  34. public class BusinessManagerImpl implements BusinessManager{
  35.  
  36. @Autowired
  37. private UserDao userDao;
  38.  
  39. @Autowired
  40. private ProductDao productDao;
  41. ....
  42. @Override
  43. @Transactional(propagation=Propagation.REQUIRED)
  44. public User addUser(User user) throws Exception {
  45. // TODO Auto-generated method stub
  46. User tempUser = userDao.addUser(user);
  47. productDao.getAllProducts();
  48.  
  49. return tempUser;
  50. }
  51.  
  52. @Service
  53. public class UserDaoImpl implements UserDao {
  54.  
  55. private DataSource dataSource;
  56.  
  57. @Autowired
  58. public UserDaoImpl(DataSource dataSource) {
  59. super();
  60. setDataSource(dataSource);
  61. }
  62.  
  63. public void setDataSource(DataSource dataSource) {
  64. this.dataSource = dataSource;
  65. }
  66.  
  67. private JdbcTemplate getJdbcTemplate(){
  68. JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  69.  
  70. return jdbcTemplate;
  71. }
  72. ...
  73. @Override
  74. @Transactional(propagation=Propagation.MANDATORY)
  75. public User addUser(final User user) {
  76. KeyHolder holder = new GeneratedKeyHolder();
  77.  
  78. final String sql = "insert into user (username, password) "
  79. + " VALUES (?, ?)";
  80.  
  81. getJdbcTemplate().update(new PreparedStatementCreator() {
  82.  
  83. @Override
  84. public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
  85. PreparedStatement ps = connection.prepareStatement(sql.toString(), Statement.RETURN_GENERATED_KEYS);
  86.  
  87. int index = 1;
  88.  
  89. ps.setString(index++, user.getUsername());
  90. ps.setString(index++, user.getPassword());
  91.  
  92.  
  93. return ps;
  94. }
  95. }, holder);
  96.  
  97. int seq = holder.getKey().intValue();
  98. user.setSeq(seq);
  99. return user;
  100. }
  101.  
  102. @Service
  103. public class ProductDaoImpl implements ProductDao {
  104.  
  105. private DataSource dataSource;
  106.  
  107. @Autowired
  108. public ProductDaoImpl(DataSource dataSource) {
  109. super();
  110. setDataSource(dataSource);
  111. }
  112.  
  113. public void setDataSource(DataSource dataSource) {
  114. this.dataSource = dataSource;
  115. }
  116.  
  117. @Override
  118. @Transactional(propagation=Propagation.MANDATORY)
  119. public List<Product> getAllProducts() throws Exception {
  120.  
  121. if(true)
  122. throw new RuntimeException("on purpose");
  123.  
  124. JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
  125. List<Product> products = jdbcTemplate.query(
  126. "select * from product",
  127. new ProductRowMapper());
  128.  
  129. return products;
  130. }
  131.  
  132. @RestController
  133. public class RestUserController {
  134. private static Logger logger = LoggerFactory.getLogger(RestUserController.class);
  135.  
  136. @Autowired
  137. private BusinessManager businessManager;
  138.  
  139. @RequestMapping(value = "/adduser", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
  140. public ResponseEntity<String> createEmployee(@RequestBody User user)
  141. {
  142. logger.debug("adding user:"+user);
  143. User addedUser=null;
  144. try {
  145. addedUser = businessManager.addUser(user);
  146. return new ResponseEntity(addedUser, HttpStatus.CREATED);
  147. } catch (Exception e) {
  148. // TODO Auto-generated catch block
  149. e.printStackTrace();
  150. }
  151.  
  152. return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
  153.  
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement