前面幾篇博客介紹了TeaFramework的實現,本篇博客將介紹若是利用Teaframework進行web開發,寫了一個demo,包括一套完整的增刪改查,分頁、AOP等等,基於LayUI作了一套界面(感謝@賢心的LayUI)。demo已經上傳至碼雲,地址:https://gitee.com/lxkm/TeaFrameWorkHelloWorld,demo的整體界面以下:css
下面就來體驗極簡的web開發旅程吧。java
1、環境搭建mysql
一、安裝mavengit
二、因爲Teaframework目前沒有發佈到中央倉庫,因此須要本地down一份Teaframework代碼,git地址:https://gitee.com/lxkm/teaframework.git,執行mvn install將Teaframework安裝至本地倉庫。web
三、將demo代碼從https://gitee.com/lxkm/TeaFrameWorkHelloWorld.git上down下來,執行mvn install,在mysql中創建一個空庫,執行/src/main/resources/db裏的sql語句,而後執行mvn tomcat7:run就能夠用maven tomcat插件啓動web服務了,瀏覽器訪問http://localhost:8080便可看到界面。sql
2、開發過程數據庫
一、teaFramework.properties配置,數據庫類型mysql、數據庫username、數據庫password、包的掃描路徑。後端
teaFramework.databaseType=mysql teaFramework.baseScanPackage=org.teaframework.helloworld dbcp.driverName=com.mysql.jdbc.Driver dbcp.username=root dbcp.password=12345 dbcp.url=jdbc:mysql://localhost/helloworld?useUnicode=true&characterEncoding=UTF-8 dbcp.initialSize=10 dbcp.maxTotal=30 dbcp.maxIdle=20 dbcp.maxWaitMillis=10000 dbcp.minIdle=10
二、web.xml配置瀏覽器
web請求的轉發經過過濾器org.teaframework.web.filter.TeaDispatcherFilter實現,TeaDispatcherFilter能夠配置不攔截的資源文件或者圖片等,也能夠配置編碼。tomcat
<filter> <filter-name>TeaDispatcherFilter</filter-name> <filter-class>org.teaframework.web.filter.TeaDispatcherFilter</filter-class> <init-param> <param-name>notIntercept</param-name> <param-value>.jsp,.png,.gif,.jpg,.js,.css,.jspx,.jpeg,.swf,.ico,.woff</param-value> </init-param> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>TeaDispatcherFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
另外須要配置listener在web容器啓動時啓動TeaFramework
<listener> <listener-class>org.teaframework.web.listener.TeaContextListener</listener-class> </listener>
三、註解說明
@Namespace:標示URL前綴
@Component:標示該類須要bean容器來實例化
@Inject:注入屬性,默認根據類型注入,若是要根據bean注入,能夠寫成@Inject("beanName")
@TeaDao:註解在dao層的接口上,標示須要被動態代理來生成實現類
@Transcation:註解在方法上,表示方法須要事務控制,註解在類上,表示類的全部公有方法須要被歸入事務管理。
四、控制層
@Namespace("/userManage")標示請求的前綴,註解在類之上,url的形式寫成/namespace/method的形式便可,例如:demo中UserController下有個findUserList方法,那麼訪問這個方法的url能夠寫成這樣:/userManage/findUserList
package org.teaframework.helloworld.controller; import java.util.ArrayList; import java.util.List; import java.util.Map; import org.teaframework.helloworld.base.PageResult; import org.teaframework.helloworld.domain.User; import org.teaframework.helloworld.service.UserService; import org.teaframework.helloworld.vo.UserVO; import org.teaframework.ioc.annotation.Component; import org.teaframework.ioc.annotation.Inject; import org.teaframework.web.annotation.JSON; import org.teaframework.web.annotation.Namespace; import org.teaframework.web.annotation.Param; @Namespace("/userManage") @Component public class UserController { @Inject private UserService userService; @JSON public PageResult<User> findUserList(UserVO userVO) { return new PageResult<User>(userService.findUserList(userVO)); } public String dispatch(@Param("mode") String mode, @Param("id") Long id, Map<String, Object> map) { if ("add".equals(mode)) { return "/addUserDlg.jsp"; } else if ("edit".equals(mode)) { map.put("user", userService.getUserById(id)); return "/updateUserDlg.jsp"; } return null; } @JSON public User addUser(User user) { return userService.addUser(user); } @JSON public Integer deleteUsers(@Param("ids") String ids) { List<Long> idList = new ArrayList<Long>(); if (null != ids && !"".equals(ids)) { String[] idArray = ids.split(","); for (String id : idArray) { idList.add(Long.parseLong(id)); } } return userService.deleteUserByIds(idList); } @JSON public User updateUser(User user) { return userService.updateUser(user); } }
五、service層
package org.teaframework.helloworld.service.impl; import java.util.List; import org.teaframework.aop.annotation.Transcation; import org.teaframework.helloworld.dao.UserDao; import org.teaframework.helloworld.domain.User; import org.teaframework.helloworld.service.UserService; import org.teaframework.helloworld.vo.UserVO; import org.teaframework.ioc.annotation.Component; import org.teaframework.ioc.annotation.Inject; import org.teaframework.orm.databind.util.DynamicSqlUtil; import org.teaframework.orm.pagehelper.OrderBy; import org.teaframework.orm.pagehelper.PageInfo; import org.teaframework.orm.pagehelper.PageUtil; @Transcation @Component public class UserServiceImpl implements UserService { @Inject private UserDao userDao; @Override public PageInfo<User> findUserList(UserVO userVO) { if (userVO.getField() != null && userVO.getSord() != null) { PageUtil.setPageParameter(userVO.getPage(), userVO.getLimit(), new OrderBy().addOrderPair(userVO.getField(), userVO.getSord())); } else { PageUtil.setPageParameter(userVO.getPage(), userVO.getLimit(), null); } String sql = "select * from users"; if (userVO.getId() != null) { sql += " where id=" + userVO.getId(); } DynamicSqlUtil.set(sql); return new PageInfo<User>(userDao.findUserList()); } @Override public User addUser(User user) { Long id = userDao.addUser(user); return getUserById(id); } @Override public User getUserById(Long id) { return userDao.getUserById(id); } @Override public Integer deleteUserByIds(List<Long> ids) { int result = 0; for (Long id : ids) { if (userDao.deleteUserById(id) > 0) { result++; } } return result; } public User updateUser(User user) { userDao.updateUser(user); return getUserById(user.getId()); } @Override public User getUserByUserName(String userName) { return userDao.getUserByUserName(userName); } }
六、dao層
package org.teaframework.helloworld.dao; import java.util.List; import org.teaframework.helloworld.domain.User; import org.teaframework.orm.annotation.AutoIncrement; import org.teaframework.orm.annotation.DynamicSQL; import org.teaframework.orm.annotation.SQL; import org.teaframework.orm.annotation.TeaDao; @TeaDao public interface UserDao { @DynamicSQL public List<User> findUserList(); @AutoIncrement @SQL("insert into users(userName,password,name,createDate,createUser,updateDate,updateUser,mobile) values(#userName#,#password#,#name#,#createDate#,#createUser#,#updateDate#,#updateUser#,#mobile#)") public Long addUser(User user); @SQL("select * from users where id=#id#") public User getUserById(Long id); @SQL("delete from users where id=#id#") public Integer deleteUserById(Long id); @SQL("select * from users where userName=#userName#") public User getUserByUserName(String userName); @SQL("update users set name=#name#,updateDate=#updateDate#,updateUser=#updateUser#,mobile=#mobile# where id=#id#") public void updateUser(User user); }
七、AOP
這裏AOP的例子,攔截dao層的新增和修改方法,包createUser、createDate、updateUser、updateDate,四個屬性set進去。
package org.teaframework.helloworld.aspect; import java.util.Date; import org.teaframework.aop.AbstractProxy; import org.teaframework.aop.BeanProxy; import org.teaframework.aop.Proxy; import org.teaframework.aop.annotation.Aspect; import org.teaframework.helloworld.base.BaseDomain; @Aspect(classRegex = "org.teaframework.helloworld.*.dao.*", beforeRegex = "add.*|update.*") public class DomainAspect extends AbstractProxy { @Override public void before(Proxy proxy) { BeanProxy beanProxy = (BeanProxy) proxy; if (beanProxy.getArgs() != null && beanProxy.getArgs()[0] instanceof BaseDomain) { BaseDomain domain = (BaseDomain) beanProxy.getArgs()[0]; domain.setCreateDate(new Date()); domain.setCreateUser("admin"); domain.setUpdateDate(new Date()); domain.setUpdateUser("admin"); } } }
好了,經過Teaframework進行web開發的整個後端代碼介紹完畢,整個開發的過程基本沒配置,徹底經過註解、動態sql部分徹底能夠自由經過代碼自由組裝,經過分頁、排序等等實用功能,趕快下載體驗簡單的web開發旅程吧。