java 后台开发流程

这篇文章为了奠基一下被我打入冷宫两个月左右的iOS开发,因为之前由于iOS项目停止的原因,被调至后台开发,两个月中也学习到了很多关于java、sql、js、jsp的内容,感谢我的同事悉心指教!

eclipse(编程工具)+navicat(数据库)+zookeeper(服务协调)=开发环境,大致的开发流程如下:

1、数据库建表

2、新建实体类

3、新建Dao并测试Dao,需要配置Mapper(xml)

4、新建Service接口(定义接口)

5、Service接口的实现并测试,需要配置dubbo注册service生产者

6、新建controller,需要配置dubbo注册service消费者

7、前端页面(js、jsp)

8、起服务并调试


1、数据库建表

1)一般在表结构中添加int类型的id作为表的主键,并设置为自增长。

2)需要注意控制好字段的长度、是否可以为null,并注释。


2、新建实体类

package com.clt.wsxc.commons.domain.mp;

import java.util.Date;

import com.alibaba.fastjson.annotation.JSONField;
import com.clt.wsxc.commons.domain.Pagination;

/**
 *@Description: 微信关注统计实体类
 *@Author:YXQ
 *@Since:2015年6月16日
 */
public class WechatBind extends Pagination{
	
	private static final long serialVersionUID = 8262554316156565954L;

	// 主键id
	private int bindId;
	
	// 类型 1001:跑男 ,1002:用户
	private String type;
	
	private String typeStr;
	
	// 被扫描者编号
	private String number;
	
	// 被扫描者姓名
	private String name;
	
	// 扫描者的微信openid
	private String openId;
	
	// 扫描者是否消费 0未消费 1已消费
	private int haveConsumed;
	
	private String haveConsumedStr;
	
	// 扫描者姓名
	private String consumerName;
	
	// 扫描者电话
	private String consumerPhone;
	
	// 更新时间
	@JSONField(format = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;

	public int getBindId() {
		return bindId;
	}

	public void setBindId(int bindId) {
		this.bindId = bindId;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTypeStr() {
		int i = Integer.valueOf(this.type);
		if(i == 1001){
			typeStr = "跑男";
		}else if(i == 1002){
			typeStr = "用户";
		}else{
			typeStr = "其他";
		}
		return typeStr;
	}

	public void setTypeStr(String typeStr) {
		this.typeStr = typeStr;
	}

	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 String getOpenId() {
		return openId;
	}

	public void setOpenId(String openId) {
		this.openId = openId;
	}

	public Date getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}

	public int getHaveConsumed() {
		return haveConsumed;
	}

	public void setHaveConsumed(int haveConsumed) {
		this.haveConsumed = haveConsumed;
	}

	public String getHaveConsumedStr() {
		if(this.haveConsumed == 0){
			haveConsumedStr = "暂未消费";
		}else{
			haveConsumedStr = "已消费";
		}
		return haveConsumedStr;
	}

	public void setHaveConsumedStr(String haveConsumedStr) {
		this.haveConsumedStr = haveConsumedStr;
	}

	public String getConsumerName() {
		return consumerName;
	}

	public void setConsumerName(String consumerName) {
		this.consumerName = consumerName;
	}

	public String getConsumerPhone() {
		return consumerPhone;
	}

	public void setConsumerPhone(String consumerPhone) {
		this.consumerPhone = consumerPhone;
	}
	
	
}

1)实体类一般会继承分页基类pagination(自定义的分页基类,传参数时可以用该基类,返回时需要用PaginationEntity,把对象放在items中)

2)生成serialVersionUID(相当于java类的身份证,主要用于版本控制。Java的序列化机制是通过在运行时判断类的serialVersionUID来验证版本一致性,版本升级时反序列化仍保持对象的唯一性。

3)实体类的字段设为私有,只提供开放的get与set方法,这是为了提高实体类的安全性。

4)Date类型的字段,用@JSONField(format = "yyyy-MM-dd HH:mm:ss")格式化输出。


3、新建Dao并测试Dao,需要配置Mapper(xml)

package com.clt.wsxc.dao.mp;

import java.util.List;

import org.springframework.stereotype.Repository;

import com.clt.wsxc.commons.domain.PaginationEntity;
import com.clt.wsxc.commons.domain.mp.WechatBind;
import com.clt.wsxc.dao.base.BaseDao;
/**
 *@Description: 微信关注统计Dao类
 *@Author:YXQ
 *@Since:2015年6月16日
 */
@Repository
public class WechatBindDao extends BaseDao<WechatBind>{

	@Override
	public void insert(WechatBind domain) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(WechatBind domain) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void delete(WechatBind domain) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public WechatBind selectOne(WechatBind domain) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<WechatBind> selectList(WechatBind domain) {
		// TODO Auto-generated method stub
		return getSqlSession().selectList("mp.WechatBindMapper.selectList", domain);
	}
	
	// 查询分页
    public PaginationEntity<WechatBind> selectPage(WechatBind domain) {
        List<WechatBind> list = selectList(domain);
        domain.setIsPaging(true);
        int count = getSqlSession().selectOne("mp.WechatBindMapper.selectCount",
        				domain);
        return new PaginationEntity<WechatBind>(count, list, domain);
    }

}

// Dao

1)Spring注解:@Repository,它用于将数据访问层 (DAO 层 ) 的类标识为 Spring Bean,只需将该注解标注在 DAO类上即可。

2)使用getSqlSession()声明sql语句

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="mp.WechatBindMapper">
    <resultMap type="WechatBind" id="WechatBindMap">
        <result column="bind_id" property="bindId" />
        <result column="type" property="type" />
        <result column="number" property="number" />
        <result column="openId" property="openId" />
        <result column="createTime" property="create_time" />
    </resultMap>
    
    <select id="selectList" parameterType="WechatBind" resultType="WechatBind">
        select
            type,
            bindId,
            number,
            openId,
            createTime,
            name,
            consumerName,
            consumerPhone,
            userId,
            haveConsumed
        from(
            select 
                type,
                bindId,
                number,
                openId,
                user.createTime,
                name,
                consumerName,
                consumerPhone,
                userId,
                haveConsumed
        from
            (
            select
            uw.type type,
            uw.bind_id bindId,
            uw.number number,
            uw.openId openId,
            uw.create_time createTime,
            ri.Runman_Name name,
            ui.User_Name consumerName,
            ui.User_Phone consumerPhone,
            ui.User_Id userId,
            oi.Service_Id haveConsumed
        from user_wechatbind uw,runman_info ri, user_member um, user_info ui
        left join order_info oi 
        on ui.User_Id = oi.User_Id
        where uw.type in (1001, 1002)
        and uw.number = ri.Runman_Id
        and uw.openId = um.User_UserName
        and um.User_Id = ui.User_Id
        and um.User_Type = 2  
        <if test="name != null and name != ''">and ri.Runman_Name like '%${name}%'</if>
     ) user
     left join order_info oi 
     on user.userId = oi.User_Id           
) t
group by bindId
order by number 
    </select>
    
    <select id="selectCount" parameterType="WechatBind" resultType="Integer">
        SELECT COUNT(*)
        FROM user_wechatbind uw
        where uw.type = 1001
        or uw.type = 1002
        <!-- <where>
            <if test="name != null and name != ''">
                and name like '%${name}%'
            </if>
        </where> -->
    </select>
    
</mapper>


4、新建Service接口(定义接口)

package com.clt.wsxc.api.mp;

import com.clt.wsxc.commons.domain.PaginationEntity;
import com.clt.wsxc.commons.domain.mp.WechatBind;
import com.clt.wsxc.commons.exception.extend.BizException;
import com.clt.wsxc.commons.exception.extend.SystemException;

/**
 *@Description: 微信关注统计接口
 *@Author:YXQ
 *@Since:2015年6月16日
 */
public interface WechatBindService {
	// 分页
	PaginationEntity<WechatBind> WechatBindPagination(WechatBind wechatBind)throws BizException,SystemException;
}
1) 关键字为interface,只需申明方法


5、Service接口的实现并测试,需要配置dubbo注册service生产者

package com.clt.wsxc.provider.mp.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;

import com.clt.wsxc.api.mp.WechatBindService;
import com.clt.wsxc.commons.domain.PaginationEntity;
import com.clt.wsxc.commons.domain.mp.WechatBind;
import com.clt.wsxc.commons.exception.extend.BizException;
import com.clt.wsxc.commons.exception.extend.SystemException;
import com.clt.wsxc.commons.exception.message.InternalCode;
import com.clt.wsxc.commons.exception.message.SystemExceptionMessage;
import com.clt.wsxc.dao.mp.WechatBindDao;
/**
 *@Description: 微信关注统计接口实现
 *@Author:YXQ
 *@Since:2015年6月16日
 */
public class WechatBindServiceImpl implements WechatBindService{
	// 内部异常编码,package001,java文件020
    final String INTERNAL_CODE = "001.020";
	
    private Logger log= LoggerFactory.getLogger (WechatBindServiceImpl.class);
    
    @Autowired
    private WechatBindDao wechatBindDao;
    
	@Override
	public PaginationEntity<WechatBind> WechatBindPagination(WechatBind wechatBind) throws BizException, SystemException {
		PaginationEntity<WechatBind> equPagination = null;
		try {
            equPagination = wechatBindDao.selectPage (wechatBind);
        } catch(DataAccessException e) {
            e.printStackTrace();
            log.error(e.toString());
            throw new SystemException(SystemExceptionMessage.SYSTEM_ERROR, InternalCode.MAN_SERVICE+INTERNAL_CODE+"01", null);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.toString());
            throw new SystemException(SystemExceptionMessage.SYSTEM_ERROR, InternalCode.MAN_SERVICE+INTERNAL_CODE+"02", null);
        }
		return equPagination;
	}

}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
	http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

<bean id="wechatBindService" class="com.clt.wsxc.provider.mp.service.WechatBindServiceImpl"/>
    <dubbo:service interface="com.clt.wsxc.api.mp.WechatBindService" ref="wechatBindService" version="1.0.0" loadbalance="${dubbo.service.loadbalance}" timeout="${dubbo.service.timeout}"/>

</beans>
1)关键字implements,包含申明的接口类

6、新建controller,需要配置dubbo注册service消费者

package com.clt.wsxc.mvc.mp.controller.wechatBind;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.clt.wsxc.api.mp.WechatBindService;
import com.clt.wsxc.commons.domain.PaginationEntity;
import com.clt.wsxc.commons.domain.mp.WechatBind;
import com.clt.wsxc.commons.mvc.controller.BaseController;
/**
 *@Description: 微信关注统计控制器
 *@Author:YXQ
 *@Since:2015年6月16日
 */
@Controller
@RequestMapping("/wechatBind")
public class WechatBindController extends BaseController{
	@Autowired
	private WechatBindService wechatBindService;
	
	@ResponseBody
	@RequestMapping("/page")
	public PaginationEntity<WechatBind> getParamPage(WechatBind wechatBind) throws Exception {
		// 排序
		wechatBind.setSortName("name");
		return wechatBindService.WechatBindPagination(wechatBind);
	}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
		http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd">
		
	<dubbo:application name="dubbo-mp-server" />
	
	<dubbo:registry address="${dubbo.registry.address}" check="false" file="false" />

<!-- 微信关注统计 -->
    <dubbo:reference id="wechatBindService" interface="com.clt.wsxc.api.mp.WechatBindService" version="1.0.0" check="false" />

</beans>
1)@controller 注解表示为控制器

2)@RequestMapping 请求路径映射,如果标注在某个controller的类级别上,则表明访问此类路径下的方法都要加上其配置的路径;最常用是标注在方法上,表明哪个具体的方法来接受处理某次请求。

3)继承于BaseController,统一错误日志和异常处理

7、前端页面(js、jsp)

因本人js不是特别拿手,就不多做解释了。

8、起服务并调试

打开本地网址,查看所写页面是否能正常展示。

尝试增删改查等方法(若已实现),查看是否会抛出异常。


阅读更多
换一批

没有更多推荐了,返回首页