3.0.dtd" >
< hibernate-configuration >
< session-factory >
< mapping class ="com.xkland.domain.Role" />
< mapping class ="com.xkland.domain.User" />
< mapping class ="com.xkland.domain.Group" />
</ session-factory >
</ hibernate-configuration >
再下一步,将上面的三个Manager类交给Spring管起来,配置src\main\resources\spring\serviceContext.xml,如下:
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN" "http://www.springframework.org/dtd/spring-beans-2.0.dtd" >
< beans default-lazy-init ="true" default-autowire ="byName" >
< bean id ="roleManager" class ="com.xkland.manager.RoleManager" />
< bean id ="userManager" class ="com.xkland.manager.UserManager" />
< bean id ="groupManager" class ="com.xkland.manager.GroupManager" />
</ beans >
SpringSide开发实战(四):打通数据持久层的任督二脉(7)
时间:2011-05-07 blogjava 海边沫沫
最后一步,编写一个Action类,用Spring将上面的三个Manager注入到Action中,测试能 否顺利的操作数据库。Action类的代码如下:
package com.xkland.action;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.xkland.manager. * ;
import com.xkland.domain. * ;
public class WelcomeAction extends Action {
private RoleManager roleManager;
private UserManager userManager;
private GroupManager groupManager;
// 以下代码的作用是注入三个Manager
public void setUserManager(UserManager userManager) {
this .userManager = userManager;
}
public void setRoleManager(RoleManager roleManager) {
this .roleManager = roleManager;
}
public void setGroupManager(GroupManager groupManager) {
this .groupManager = groupManager;
}
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
) {
// 以下代码测试能否添加role
Role role = new Role();
role.setName( " 第一个角色 " );
role.setPrivilegesFlag( " 1,2,3,4, " );
roleManager.save(role);
// 以下代码测试能否添加user
User user = new User();
user.setAnswer( " aa " );
user.setEmail( " aa " );
user.setQq( " aa " );
user.setName( " abcdefg " );
user.setPassword( " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa " );
user.setQuestion( " aa " );
user.setMonicker( " abcdefg " );
user.setRole(roleManager.get( 1 ));
userManager.save(user);
// 以下代码测试能否添加gr
|