实在是要忙公司的项目.
eweb框架虽然也在持续开发中,
不过因为新版本的文档资料还没有重新编写,
所以只能先放出一个正在开发中的演示,虽然是演示,
不过该版本已经比较稳定了.
这次放出的压缩包解开之后有三个东西:
源代码:eweb4j-petstore-sources.jar
war部署包:eweb4j-petstore.war
数据库脚本(mysql):petstore_db.sql
如果需要其他数据库,自己写一个吧,才两个表,一个外键关系而已.
放一张截图吧:
各位请放心下载哇.
上几句代码吧:
package com.cfuture08.eweb4j.demo.petstore.pet.web; import java.util.Arrays; import com.cfuture08.eweb4j.demo.petstore.AbstractAction; import com.cfuture08.eweb4j.demo.petstore.pet.Pet; import com.cfuture08.eweb4j.demo.petstore.pet.PetException; import com.cfuture08.eweb4j.mvc.annotation.POST; import com.cfuture08.eweb4j.mvc.annotation.Path; /** * 添加宠物信息控制器 * * @author weiwei * */ public class AddPetInfoAction extends AbstractAction { /** * 接收页面表单数据,提供验证规则 */ private Pet pet = null; /** * 处理HTTP请求 POST http://{host}:{port}/eweb/pets/ * * @return */ @Path("pets/") @POST public String doPost() { request.setAttribute("types", Arrays.asList("猫", "狗", "猪")); try { petService.create(pet); } catch (PetException e) { request.setAttribute("error", e.getMessage()); return "../error.jsp"; } return "redirect:list"; } public Pet getPet() { return pet; } public void setPet(Pet pet) { this.pet = pet; } }
package com.cfuture08.eweb4j.demo.petstore; import javax.servlet.http.HttpServletRequest; import com.cfuture08.eweb4j.demo.petstore.master.MasterService; import com.cfuture08.eweb4j.demo.petstore.pet.PetService; import com.cfuture08.eweb4j.ioc.annotation.Ioc; /** * 宠物信息抽象控制器 * * @author weiwei * */ public abstract class AbstractAction { /** * 通过IOC容器注入 */ @Ioc("petService") protected PetService petService; /** * 通过IOC容器注入 */ @Ioc("masterService") protected MasterService masterService; /** * 声明且提供setter方法,框架就能自动注入 */ protected HttpServletRequest request; public PetService getPetService() { return petService; } public void setPetService(PetService petService) { this.petService = petService; } public MasterService getMasterService() { return masterService; } public void setMasterService(MasterService masterService) { this.masterService = masterService; } public HttpServletRequest getRequest() { return request; } public void setRequest(HttpServletRequest request) { this.request = request; } }
package com.cfuture08.eweb4j.demo.petstore.pet; import java.util.List; import com.cfuture08.eweb4j.orm.dao.DAO; import com.cfuture08.eweb4j.orm.dao.DAOException; import com.cfuture08.eweb4j.orm.dao.cascade.CascadeDAO; import com.cfuture08.eweb4j.orm.dao.delete.DeleteDAO; import com.cfuture08.eweb4j.orm.dao.factory.DAOFactory; import com.cfuture08.eweb4j.orm.dao.insert.InsertDAO; import com.cfuture08.eweb4j.orm.dao.select.DivPageDAO; import com.cfuture08.eweb4j.orm.dao.select.SelectDAO; import com.cfuture08.eweb4j.orm.dao.update.UpdateDAO; /** * 宠物信息服务接口实现类 * * @author weiwei * */ public class PetServiceImpl implements PetService { private DivPageDAO divPageDAO; private CascadeDAO cascadeDAO; private SelectDAO selectDAO; private InsertDAO insterDAO; private UpdateDAO updateDAO; private DeleteDAO deleteDAO; // 这是一个用来拼凑sql语句的接口, // 是在ORM映射之下的.并不是直接拼凑原生SQL private DAO dao; public PetServiceImpl() { this.divPageDAO = DAOFactory.getDivPageDAO(); this.cascadeDAO = DAOFactory.getCascadeDAO(); this.selectDAO = DAOFactory.getSelectDAO(); this.insterDAO = DAOFactory.getInsertDAO(); this.updateDAO = DAOFactory.getUpdateDAO(); this.deleteDAO = DAOFactory.getDeleteDAO(); this.dao = DAOFactory.getDAO(Pet.class); } /** * 分页获取宠物信息列表 * * @param pageNum * @return * @throws PetException */ public ListgetPage(int pageNum) throws PetException { List list = null; try { list = divPageDAO.divPage(Pet.class, pageNum, 10); if (list != null) { // 级联查询出所有宠物的主人信息 cascadeDAO.select(list.toArray(new Pet[] {}), "master"); } } catch (DAOException e) { throw new PetException("数据库错误", e); } return list; } public long count() throws PetException { long count = 0; try { count = selectDAO.selectCount(Pet.class); } catch (DAOException e) { throw new PetException("数据库错误", e); } return count; } /** * 添加宠物信息 * * @param pet * @throws PetException */ public void create(Pet pet) throws PetException { if (pet == null) throw new PetException("要添加的宠物信息不能为空"); try { Pet db_pet = selectDAO.selectOne(pet, "name"); if (db_pet != null) throw new PetException("宠物名字已经存在,请重填"); db_pet = selectDAO.selectOne(pet, "number"); if (db_pet != null) throw new PetException("宠物编号已经存在,请重填"); pet.setMaster(null); insterDAO.insert(pet); } catch (DAOException e) { throw new PetException("数据库错误", e); } } /** * 更新 * * @param petId * @param pet * @throws PetException */ public void update(Pet pet) throws PetException { if (pet == null) throw new PetException("宠物信息已被删除或丢失"); try { Pet db_pet = dao.selectAll().where().field("name") .equal(pet.getName()).and("id").notEqual(pet.getId()) .queryOne(); // 也可以 // String condition = String.format("name = '%s' and id <> '%s'", // pet.getName(), pet.getId()); // Pet db_pet = // selectDAO.selectOneByWhere(Pet.class, // condition); if (db_pet != null) throw new PetException("宠物名字已经存在,请重填"); dao.clear(); db_pet = dao.selectAll().where().field("number").equal(pet.getNumber()) .and("id").notEqual(pet.getId()).queryOne(); // 也可以 // condition = String.format("number = '%s' and id <> '%s'", // pet.getNumber(), pet.getId()); // db_pet = selectDAO.selectOneByWhere(Pet.class, // condition); if (db_pet != null) throw new PetException("宠物编号已经存在,请重填"); pet.setMaster(null); updateDAO.update(pet); } catch (DAOException e) { throw new PetException("数据库错误", e); } } /** * 删除 * * @param petId * @throws PetException */ public void remove(long petId) throws PetException { try { deleteDAO.deleteById(Pet.class, petId); } catch (DAOException e) { e.printStackTrace(); throw new PetException("数据库错误", e); } } /** * 获取详细信息 * * @param petId * @return * @throws PetException */ public Pet getDetail(long petId) throws PetException { Pet pet = null; try { pet = selectDAO.selectOneById(Pet.class, petId); // 级联查询出所有宠物的主人信息 cascadeDAO.select(pet, "master"); } catch (DAOException e) { throw new PetException("数据库错误", e); } return pet; } }
package com.cfuture08.eweb4j.demo.petstore.pet; import com.cfuture08.eweb4j.demo.petstore.master.Master; import com.cfuture08.eweb4j.mvc.validate.annotation.Chinese; import com.cfuture08.eweb4j.mvc.validate.annotation.Enum; import com.cfuture08.eweb4j.mvc.validate.annotation.Int; import com.cfuture08.eweb4j.mvc.validate.annotation.Length; import com.cfuture08.eweb4j.mvc.validate.annotation.Required; import com.cfuture08.eweb4j.mvc.validate.annotation.Size; import com.cfuture08.eweb4j.orm.config.annotation.Id; import com.cfuture08.eweb4j.orm.config.annotation.One; import com.cfuture08.eweb4j.orm.config.annotation.Table; @Table("t_pet") public class Pet { @Id private long id; @Required(mess="编号必填") @Length(min = 6, max = 6, mess="编号6位数字或文字的组合") private String number; @Required(mess="宠物名字必填,2-32位,中文") @Chinese(mess="宠物名字要求全中文") @Length(min=2,max=32,mess="宠物名字,2-32位") private String name; @Required(mess="年龄必填,1-5岁") @Int(mess="年龄,必须是数字") @Size(min = 1, max = 5, mess="年龄,1-5岁") private int age; @Required(mess="宠物类型必填") @Enum(words = { "猫", "狗" },mess="宠物类型, 只能添加猫和狗两种类型") private String type;//只能添加猫和狗两种类型 @One(column = "master_id") private Master master; public long getId() { return id; } public void setId(long id) { this.id = id; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getType() { return type; } public void setType(String type) { this.type = type; } public Master getMaster() { return master; } public void setMaster(Master master) { this.master = master; } @Override public String toString() { return "PetModel [id=" + id + ", number=" + number + ", name=" + name + ", age=" + age + ", type=" + type + ", master=" + master + "]"; } }下.