NutzCN Logo
问答 @IocBean(fields = { "dao" }) 这样写是干什么用
发布于 2786天前 作者 qq_7db27eef 3480 次浏览 复制 上一个帖子 下一个帖子
标签:
这是 action
package com.weike.action;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.beans.BeanInfo;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.security.MessageDigest;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.imageio.ImageReadParam;
import javax.imageio.ImageReader;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.img.Images;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.annotation.AdaptBy;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Fail;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;
import org.nutz.mvc.upload.FieldMeta;
import org.nutz.mvc.upload.TempFile;
import org.nutz.mvc.upload.UploadAdaptor;

import com.alibaba.druid.util.StringUtils;
import com.weike.ServiceImpl.MainServiceImpl;
import com.weike.bean.School;
import com.weike.bean.School.School_field;
import com.weike.bean.utils.form_class;
import com.weike.bean.utils.form_class.formControl_type;
import com.weike.utils.PsImage;
import com.weike.utils.Toolkit;
import com.weike.bean.fileImport;



import com.weike.service.mainService;
import com.weike.bean.shuoFile;

@IocBean
public class main_action {
@Inject()
	private Dao dao;
@Inject
	private MainServiceImpl mainService;


    @At("/admin")
    @Ok("beetl:haha/index.html")
    @Fail("void") // beelt的机制导致只能使用void,详细原因看nutzbook中的代码吧
	 public Object main(@Param("page")int page2,@Param("ajaxCmd")String view) {
		

        return mainService.main();
    }

}
这是 service


package com.weike.service;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.upload.TempFile;

import com.weike.bean.School;
import com.weike.bean.School.School_field;
import com.weike.bean.shuoFile;
import com.weike.bean.utils.form_class;
import com.weike.bean.utils.form_class.formControl_type;
import com.weike.utils.PsImage;
import com.weike.utils.PublicVar;

@IocBean(fields = { "dao" })
public interface mainService  {
	
	 public  NutMap main() ;
	
	 public  void  dele() ;
	 
	 public  List query() ;
	 
	 
	 public Object uploadimg(TempFile tf, int x, int y, int w, int h) throws IOException ;
	 
	 public Object upload_bgimg (TempFile tf) throws IOException ;

	
}


这是   BaseServiceImpl


package com.weike.ServiceImpl;

import java.util.List;

import org.nutz.dao.Cnd;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.QueryResult;
import org.nutz.dao.pager.Pager;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Lang;
import org.nutz.lang.util.NutMap;
import org.nutz.service.IdEntityService;

import com.weike.bean.School;
import com.weike.common.page.Pagination;

/**
 * @author 科技㊣²º¹³
 * 2014年2月3日 下午4:48:45
 * http://www.rekoe.com
 * QQ:5382211
 */

public abstract class BaseServiceImpl<T> extends IdEntityService<T> {
	
	
	 
	 
	 
	 

	protected final static int DEFAULT_PAGE_NUMBER = 20;

	public BaseServiceImpl() {
		super();
	}

	public BaseServiceImpl(Dao dao) {
		super(dao);
	}

	public Pagination getObjListByPager(Integer pageNumber, int pageSize, Condition cnd) {
		pageNumber = getPageNumber(pageNumber);
		Pager pager = dao().createPager(pageNumber, pageSize);
		List<T> list = dao().query(getEntityClass(), cnd, pager);
		pager.setRecordCount(dao().count(getEntityClass(), cnd));
		return new Pagination(pageNumber, pageSize, pager.getRecordCount(), list);
	}
	public QueryResult getObjListByPager(Integer pageNumber, int pageSize, Class cls) {
		
		  QueryResult qr = new QueryResult();
	        Pager pager = dao().createPager(pageNumber, pageSize);
	        pager.setRecordCount(dao().count(cls));
	        qr.setPager(pager);
	        qr.setList(dao().query(School.class, null, pager));
		
		return qr;
	}
	public Pagination getObjListByPager(Integer pageNumber, Condition cnd) {
		return getObjListByPager(pageNumber, DEFAULT_PAGE_NUMBER, cnd);
	}

	protected int getPageNumber(Integer pageNumber) {
		return Lang.isEmpty(pageNumber) ? 1 : pageNumber;
	}
	
	public void delete(String[] ids) {
		dao().clear(getEntityClass(), Cnd.where("id", "in", ids));
	}



	
}


这是    MainServiceImpl 


package com.weike.ServiceImpl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import javax.imageio.ImageIO;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
import org.nutz.dao.Cnd;
import org.nutz.dao.QueryResult;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.nutz.mvc.upload.TempFile;

import com.weike.bean.School;
import com.weike.bean.shuoFile;
import com.weike.bean.School.School_field;
import com.weike.bean.utils.form_class;
import com.weike.bean.utils.form_class.formControl_type;
import com.weike.service.mainService;
import com.weike.utils.PsImage;
import com.weike.utils.PublicVar;
@IocBean(fields = { "dao" })
public class MainServiceImpl extends BaseServiceImpl<School> implements  mainService {
	
public NutMap main() {

		NutMap map = new NutMap();
		form_class formmodel = new form_class();
		formmodel.setName("dd");
		formmodel.setType(formControl_type.INPUT);

		QueryResult qr = getObjListByPager(1, 4, School.class);

		String[] bArray = { School_field.tx, School_field.name, School_field.synopsis, School_field.bgpic, "" };
		String[] aArray = { "头像", "姓名", "简介", "背景图", "地区" };
		ArrayList a1 = new ArrayList();
		int[] typeArray = { 3, 1, 2, 4, 5 };

		for (int i = 0; i < 5; i++) {
			String name = bArray[i];
			String text = aArray[i];
			int type1 = typeArray[i];

			form_class ss = form_class.setmodel(name, type1, text);

			a1.add(ss);
		}
		form_class ss2 = (form_class) a1.get(0);

		map.put("page", qr);
		map.put("type", formControl_type.INPUT);
		map.put("addpage", a1);
		return map;

		// TODO Auto-generated method stub

	}


}
	
	

@IocBean(fields = { "dao" }) 请教一下,MainServiceImpl 里这么写 有什么作用,为什么 写成 @IocBean 就报空指针了呢??

4 回复

注入父类的dao属性

@wendal 这样他的父类 BaseServiceImpl 就可以用这个 dao了是吧

@wendal 谢谢ㄟ(▔=▔)ㄏ

添加回复
请先登陆
回到顶部