一个使用servlet+反射实现的mvc框架与使用asm实现的数据库操作层框架
该框架目前处于开发阶段,框架实现方法简单,肯定存在不足之处希望以后可以继续完善
已经实现的功能有:
Controller控制层
请求转发
页面重定向
数据库的基本操作
通过注解获得cookie中的值
没有实现的功能:
数据库的存储过程调用、表关联
使用实例:
控制层代码
package com.demo.controller; import java.util.Date; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.demo.dao.UserDao; import com.demo.entity.UserEntity; import com.fw4j.jdbc.factory.DaoFactory; import com.fw4j.jdbc.factory.EntityFactory; import com.fw4j.web.annotation.Controller; import com.fw4j.web.annotation.Parameter; import com.fw4j.web.annotation.RequestMapping; @Controller @RequestMapping("/") public class DemoController { @RequestMapping("/index") @Action(type=Type.REQUEST,result="/view/index.jsp") public int doGet(){ return 0; } @RequestMapping("/list") @Action(type=Type.TOJSON) public Object list(){ List<Object> objectlist = new ArrayList<Object>(); Map<String,Object> valMap = new HashMap<String,Object>(); valMap.put("222", "222"); valMap.put("123", "123"); objectlist.add(valMap); objectlist.add(valMap); return objectlist; } @RequestMapping(value="/add",method=Method.POST) public String add(@Parameter({"name","pwd"}) String name, String pwd) throws Exception{ // DaoFactory.transaction(new TransactionCallback() { // @Override // public void invoke() throws SQLException { // UserEntity ue = null; // try { // ue = EntityFactory.getInstance(UserEntity.class); // ue.setName(name); // ue.setPwd(pwd); // ue.setLoginTime(new Date()); // DaoFactory.getSynConnectInstance(UserDao.class).saveOrUpdate(ue); // } catch (InstanceException e) { // e.printStackTrace(); // } catch (Exception e) { // e.printStackTrace(); // } // } // }); UserEntity ue = EntityFactory.getInstance(UserEntity.class); ue.setName(name); ue.setPwd(pwd); ue.setLoginTime(new Date()); DaoFactory.getInstance(UserDao.class).saveOrUpdate(ue); return "redirect:/show"; } @RequestMapping("/show") public String show(OutParameter oparam) throws Exception{ Listuel = (List)DaoFactory.getInstance(UserDao.class).getAll(); oparam.put("users", uel); return "/view/show.jsp"; } }
实体层:
package com.demo.entity; import java.util.Date; import com.fw4j.jdbc.annotation.Column; import com.fw4j.jdbc.annotation.Entity; import com.fw4j.jdbc.annotation.Key; import com.fw4j.jdbc.annotation.Table; @Entity @Table("users") public interface UserEntity { @Key @Column(value="id",type=Column.INTEGER) void setId(int id); int getId(); @Column(value="name",type=Column.STRING) void setName(String name); String getName(); @Column(value="pwd",type=Column.STRING) void setPwd(String pwd); String getPwd(); @Column(value="loginTime",type=Column.DATE) void setLoginTime(Date lt); Date getLoginTime(); }
数据库操作层:
package com.demo.dao; import java.util.List; import com.demo.entity.UserEntity; import com.fw4j.jdbc.annotation.Dao; import com.fw4j.jdbc.annotation.Query; import com.fw4j.jdbc.annotation.Where; import com.fw4j.jdbc.dao.BaseDao; @Dao(UserEntity.class) public interface UserDao extends BaseDao { @Query("select * from users") @Where("name = ? and pwd = ?") ListfindByNameAndPwd(String name,String pwd); }
配置文件:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>demo</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet-mapping> <servlet-name>fw4j</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <servlet> <servlet-name>fw4j</servlet-name> <servlet-class>com.fw4j.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> </web-app>
fw4j的独立配置文件
<?xml version="1.0" encoding="UTF-8"?> <fw4j> <Controller value="com.demo.controller"> <encoding>utf-8</encoding> <view>/WEB-INF/view/</view> </Controller> <JDBC pool="true"> <pool database="mysql"> <url>jdbc:mysql://localhost/webauth</url> <driver>com.mysql.jdbc.Driver</driver> <user>root</user> <pwd>123</pwd> <outOfData>10000</outOfData> </pool> <pool database="default"> <url>jdbc:h2:tcp://127.0.0.1:9094/~/test</url> <driver>org.h2.Driver</driver> <user>sa</user> <pwd></pwd> </pool> </JDBC> </fw4j>