UserAction类代码:
package com.swift.action; import com.opensymphony.xwork2.ActionSupport; import com.swift.service.UserService; public class UserAction extends ActionSupport { private UserService userService; public void setUserService(UserService userService) { this.userService = userService; } @Override public String execute() throws Exception { System.out.println("action.................."); userService.add(); userService.update(); userService.delete(); userService.getOne(); userService.findAll(); userService.findYouWant(); return NONE; } }
调用UserService类中crud各种方法
UserService类代码:
package com.swift.service; import org.springframework.transaction.annotation.Transactional; import com.swift.dao.UserDao; @Transactional public class UserService { private UserDao userDao; public void setUserDao(UserDao userDao) { this.userDao = userDao; } public void add() { System.out.println("UserService.................add"); userDao.add(); } public void update() { System.out.println("UserService.................update"); userDao.update(); } public void delete() { System.out.println("UserService.................delete"); userDao.delete(); } public void getOne() { System.out.println("UserService.................get the 3rd"); userDao.getOne(); } public void findAll() { System.out.println("UserService.................find all"); userDao.findAll(); } public void findYouWant() { System.out.println("UserService.................find you want"); userDao.findYouWant(); } }
调用UserDao接口
package com.swift.dao; public interface UserDao { public void add(); public void update(); public void delete(); public void getOne(); public void findAll(); public void findYouWant(); }
接口实现类UserDaoImplements
package com.swift.dao; import java.util.List; import org.springframework.orm.hibernate5.HibernateTemplate; import com.swift.entity.User; public class UserDaoImplements implements UserDao{ private HibernateTemplate hibernateTemplate; private User user; public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } public void setUser(User user) { this.user = user; } @Override public void add() { //增加 user.setUsername("small-fly"); user.setAddress("War of Mercenaries"); hibernateTemplate.save(user); } @Override public void update() { //修改 user.setUsername("big-dog"); user.setAddress("The War of Mercenaries"); hibernateTemplate.update(user); } @Override public void delete() { hibernateTemplate.delete(user); } @Override public void getOne() { User user=hibernateTemplate.get(User.class, 2); System.out.println(user.getUsername()+" :: "+user.getAddress()); } @Override public void findAll() { List<User> list=(List<User>) hibernateTemplate.find("from User"); for(User user:list) { System.out.println(user.getUsername()+" :: "+user.getAddress()); } } @Override public void findYouWant() { List<User> list=(List<User>) hibernateTemplate.find("from User where userName=? and address=?","ram","The War of Mercenaries"); for(User user:list) { System.out.println(user.getUsername()+" :: "+user.getAddress()); } } }
运行Apach服务器,地址栏输入action
控制台结果
Spring Hibernate Struts2等的 xml配置文件和映射文件,以及实体类和前一篇随笔大致相同。
bean.xml的Spring核心配置文件略有不同,因为在接口实现类UserDaoImplements中,没有直接生成对象,而是通过bean.xml配置文件注入属性得到的对象user。
即配置文件中生成对象user并注入UserDaoImplements中