How to develop web based on TeaFramework

    The previous blogs introduced the implementation of TeaFramework. This blog will introduce how to use Teaframework for web development and write a demo, including a complete set of additions, deletions, changes, paging, AOP, etc., based on LayUI to create a set of interfaces ( Thanks @Xianxin's LayUI). The demo has been uploaded to the code cloud, address: https://gitee.com/lxkm/TeaFrameWorkHelloWorld, the general interface of the demo is as follows:

    Let's experience the minimalist web development journey.

1. Environment Construction

    1. Install maven

    2. Since Teaframework has not been released to the central warehouse, you need to download a copy of Teaframework code locally, git address: https://gitee.com/lxkm/teaframework.git, execute mvn install to install Teaframework to the local warehouse.

    3. Download the demo code from https://gitee.com/lxkm/TeaFrameWorkHelloWorld.git, execute mvn install, create an empty library in mysql, execute the sql statement in /src/main/resources/db, and then Execute mvn tomcat7:run to start the web service with the maven tomcat plug-in. The browser can access http://localhost:8080 to see the interface.

2. Development process

    1. teaFramework.properties configuration, database type mysql, database username, database password, and package scan path.

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

    2. web.xml configuration

  The forwarding of web requests is implemented through the filter org.teaframework.web.filter.TeaDispatcherFilter. TeaDispatcherFilter can configure unintercepted resource files or pictures, etc., and can also configure encoding.

<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>

    In addition, you need to configure the listener to start the TeaFramework when the web container starts.

<listener>
		<listener-class>org.teaframework.web.listener.TeaContextListener</listener-class>
	</listener>

    3. Annotation

    @Namespace: indicates the URL prefix

    @Component: indicates that the class needs the bean container to instantiate

    @Inject: inject properties, the default is injected according to the type, if you want to inject according to the bean, you can write @Inject("beanName")

    @TeaDao: Annotated on the interface of the dao layer, indicating that it needs to be dynamically proxied to generate the implementation class

    @Transcation: The annotation is on the method, indicating that the method requires transaction control, and the annotation is on the class, indicating that all public methods of the class need to be included in transaction management.

    4. Control layer

    @Namespace("/userManage") marks the prefix of the request, the annotation is on the class, and the url can be written in the form of /namespace/method. For example, there is a findUserList method under UserController in the demo, then the url to access this method can be Write it like this: /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);
	}
}

    5. Service layer

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);
	}

}

    6, dao layers

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);
}

    7、AOP

    The example of AOP here intercepts the new and modified methods of the dao layer, including createUser, createDate, updateUser, updateDate, and four attributes are 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");
		}
	}
}

    Well, the entire back-end code for web development through Teaframework has been introduced. The entire development process is basically unconfigured. It is completely free to assemble through the code through annotations and dynamic sql parts, and through paging, sorting and other practical functions, hurry up and download it. Take a simple web development journey.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325444078&siteId=291194637