We'll work with spring service and repository next time.But this service layer is business logic layer which directly work with Front-end side and this service layer work with dao layer for database functionality.And this two layer work with model to represent data.And this data also viewed by user from this model.Here service and dao layer will be independent and will not be dependent on view layer.This is also called multi-tier architecture.
Now we implement service and dao layer using different type of models.
To continue this post please visit my previous post Spring MVC step by step with Model Introduction-Part2
To implement this two layer's we need to modify WelcomeController.java class
*******************************WelcomeController.java********************************
package com.flopcoder.controller;
import com.flopcoder.model.User;
import com.flopcoder.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class WelcomeController {
UserService userService = new UserService();
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("user",userService.getUserById(1));
model.addAttribute("message", "Welcome to learn spring Step By Step for Dao and Service Layer. This is part 3.");
return "hello";
}
}
****************************************End*******************************
Here we see that setting user is removed from this class. And here we using a service class to get user.
This service class will be like this.
***********************************UserService.java*******************************
package com.flopcoder.service;
import com.flopcoder.dao.UserDao;
import com.flopcoder.model.User;
/**
* Created by Flop Coder on 6/1/14.
*/
public class UserService {
UserDao userDao = new UserDao();
public User getUserById(Integer id)
{
return userDao.getUserById(1);
}
}
****************************************End*******************************
Here from this service class we need to access dao layer.UserDao will give data by using The User Model
UserDao.java class will be
***************************************UserDao.java*******************************
package com.flopcoder.dao;
import com.flopcoder.model.User;
/**
* Created by Flop Coder on 6/1/14.
*/
public class UserDao {
public User getUserById(Integer id)
{
User user = new User();
user.setId(1);
user.setName("FlopCoder");
user.setAddress("Bangladesh");
user.setEmail("flopcoder@gmail.com");
user.setMobile("01684398208");
user.setPassword("secret");
user.setUserName("flopcoder");
return user;
}
}
****************************************End*******************************
At last we access data from userDao class. Actually this dao layer provide us data from database.Now our project structure will be like this.
Thanks.
Now we implement service and dao layer using different type of models.
To continue this post please visit my previous post Spring MVC step by step with Model Introduction-Part2
To implement this two layer's we need to modify WelcomeController.java class
*******************************WelcomeController.java********************************
package com.flopcoder.controller;
import com.flopcoder.model.User;
import com.flopcoder.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
@Controller
@RequestMapping("/")
public class WelcomeController {
UserService userService = new UserService();
@RequestMapping(method = RequestMethod.GET)
public String sayHello(ModelMap model) {
model.addAttribute("user",userService.getUserById(1));
model.addAttribute("message", "Welcome to learn spring Step By Step for Dao and Service Layer. This is part 3.");
return "hello";
}
}
****************************************End*******************************
Here we see that setting user is removed from this class. And here we using a service class to get user.
This service class will be like this.
***********************************UserService.java*******************************
package com.flopcoder.service;
import com.flopcoder.dao.UserDao;
import com.flopcoder.model.User;
/**
* Created by Flop Coder on 6/1/14.
*/
public class UserService {
UserDao userDao = new UserDao();
public User getUserById(Integer id)
{
return userDao.getUserById(1);
}
}
Here from this service class we need to access dao layer.UserDao will give data by using The User Model
UserDao.java class will be
***************************************UserDao.java*******************************
package com.flopcoder.dao;
import com.flopcoder.model.User;
/**
* Created by Flop Coder on 6/1/14.
*/
public class UserDao {
public User getUserById(Integer id)
{
User user = new User();
user.setId(1);
user.setName("FlopCoder");
user.setAddress("Bangladesh");
user.setEmail("flopcoder@gmail.com");
user.setMobile("01684398208");
user.setPassword("secret");
user.setUserName("flopcoder");
return user;
}
}
At last we access data from userDao class. Actually this dao layer provide us data from database.Now our project structure will be like this.
Thanks.
0 Comments