# Jparrow **Repository Path**: jonnyle/Jparrow ## Basic Information - **Project Name**: Jparrow - **Description**: 一款IOC、AOP、ORM、注解事务MVC轻量级框架。麻雀虽小,五脏俱全。 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2017-03-23 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README #Jparrow Framework ## 简介 ### 一款轻量级 Java Web 框架 - 支持 IOC、AOP、ORM、DAO、MVC 等特性 - 基于 Servlet 3.0 - 使用 Java 注解取代 XML 配置 ### 使用 ### 配置文件 创建Jparrow.properties配置文件,如下: jparrow.framework.jdbc.driver=com.mysql.jdbc.Driver jparrow.framework.jdbc.url=jdbc:mysql://localhost:3306/jparrow jparrow.framework.jdbc.username=root jparrow.framework.jdbc.password=lijun520 jparrow.framework.app.base_package=org.jparrow.test jparrow.framework.app.jsp_path=/WEB-INF/view/ jparrow.framework.app.asset_path=/asset/ ### Service ```java package org.jparrow.test.service; import java.util.List; import java.util.Map; import org.jparrow.framework.annotation.Service; import org.jparrow.framework.annotation.Transaction; import org.jparrow.framework.cache.Cache; import org.jparrow.framework.cache.CacheManager; import org.jparrow.framework.cache.DefaultCacheManager; import org.jparrow.framework.helper.DataBaseHelper; import org.jparrow.framework.util.CollectionUtil; import org.jparrow.test.model.User; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @author supda * @Date 2017-3-24 上午10:52:15 * UserService */ @Service public class UserService { private static final Logger LOGGER = LoggerFactory.getLogger(UserService.class); //缓存 private final CacheManager cacheManager; private final Cache> userListCache; private final Cache userCache; public UserService(){ cacheManager = new DefaultCacheManager("user_list_cache", "user_cache"); userListCache = cacheManager.getCache("user_list_cache"); userCache = cacheManager.getCache("user_cache"); } /** * 获取用户列表 */ public List getUserList(){ List userList = userListCache.get("user_list"); if (userList == null) { String sql = "SELECT * FROM user"; userList = DataBaseHelper.queryEntityList(User.class, sql); if (CollectionUtil.isNotEmpty(userList)) { userListCache.put("user_list", userList); } } return customerList; } /** * 查找用户 */ public User getUser(long id){ User user = userCache.get(id); if (user == null) { String sql = "SELECT * FROM user WHERE id = ?"; user = DataBaseHelper.queryEntity(User.class, sql, new Object[]{id}); if (user != null) { userCache.put(id, user); } } return user; } /** * 创建用户 */ @Transaction public boolean createUser(Map fieldMap){ boolean result =DataBaseHelper.insertEntity(User.class, fieldMap); if(result ){ cacheManager.destroyCacheAll(); } return result; } /** * 更新用户 */ @Transaction public boolean updateUser(long id, Map fieldMap){ boolean result = DataBaseHelper.updateEntity(User.class, id, fieldMap); if (result) { cacheManager.destroyCacheAll(); } return result; } /** * 删除用户 */ @Transaction public boolean deleteUser(long id){ boolean result = DataBaseHelper.deleteEntity(User.class, id); if (result) { userListCache.clear(); userCache.remove(id); } return result; } } ``` ### 编写 Action 类 ```java package org.jparrow.test.controller; import java.util.List; import java.util.Map; import org.jparrow.framework.annotation.Action; import org.jparrow.framework.annotation.Controller; import org.jparrow.framework.annotation.Inject; import org.jparrow.framework.bean.Data; import org.jparrow.framework.bean.Param; import org.jparrow.framework.bean.View; import org.jparrow.test.model.User; import org.jparrow.test.service.UserService; /** * 处理用户管理相关请求 * * @author supda * @Date 2017-3-27 上午9:18:20 * */ @Controller public class UserController { @Inject private UserService userService; /** * 进入用户列表界面 */ @Action("get:/user") public View index(Param param){ List users = userService.getUserList(); return new View("user.jsp").addModel("users", users); } /** * 显示用户基本信息 */ @Action("get:/user_show") public View show(Param param){ long id = param.getLong("id"); User user = userService.getUser(id); return new View("user_show.jsp").addModel("user", user); } /** * 进入创建用户页面 */ @Action("get:/user_create") public View create(Param param){ return new View("user_create.jsp"); } /** * 处理创建用户请求 */ @Action("post:/user_create") public Data createSubmit(Param param){ Map fieldMap = param.getMap(); boolean result = userService.createUser(fieldMap); return new Data(result); } /** * 进入编辑用户界面 */ @Action("get:/user_edit") public View edit(Param param){ long id = param.getLong("id"); User user = userService.getUser(id); return new View("user_edit.jsp").addModel("user", user); } /** * 处理编辑用户请求 */ @Action("put:/user_edit") public Data editSubmit(Param param){ long id = param.getLong("id"); Map fieldMap = param.getMap(); boolean result = userService.updateUser(id, fieldMap); return new Data(result); } /** * 处理删除用户请求 */ @Action("delete:/user_edit") public Data delete(Param param){ long id = param.getLong("id"); boolean result = userService.deleteUser(id); return new Data(result); } } ``` ### 编写视图 ## 提高 TODO ## 示例 ## 附录 ## 参考 -smart框架