java Spring框架id生成器

项目需求:
java Spring框架id生成器_第1张图片
指定oid的前缀xx或者yy等,后面的19是本机时间年份的前两个数子,后面的五位数字是自增的,当在id是3的后面添加内容时,只需要添加前缀即刻,其他的是自动填充,并且如果oid是xx1900005的话,后面就必须是6

1结构图
java Spring框架id生成器_第2张图片
2 Spring xml文件配置



	 
	
	 
    
    
        
        
        
        
    
    
	
		
	
	
	
		
	

3 bean文件(order)

package com.sheng.damo4;

public class Order {
	private int id;
	private String oid;
	private String name;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getOid() {
		return oid;
	}
	public void setOid(String oid) {
		this.oid = oid;
	}
	public String getName() {
		return name;
	}
	public Order(int id, String oid, String name) {
		super();
		this.id = id;
		this.oid = oid;
		this.name = name;
	}
	public void setName(String name) {
		this.name = name;
	}
	
}

4 Dao(orderDao)


public interface OrderDao {
	public void addOrder(String oid,String name);
}

5 orderDaoImp

package com.sheng.damo4;

import java.sql.SQLException;
import java.util.Date;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ScalarHandler;

public class OrderDaoImp implements OrderDao {
	private QueryRunner queryRunner;

	public void setQueryRunner(QueryRunner queryRunner) {
		this.queryRunner = queryRunner;
	}

	@Override
	public void addOrder(String prefix, String name) {

		try {
			String sql = "INSERT INTO ordersd(oid,name) VALUES(?,?)";
			String oid = getoOid1(prefix);
			Object[] params = new Object[] { oid, name };
			queryRunner.update(sql, params);
		} catch (SQLException e) {

			e.printStackTrace();
		}
	}

	// 开始准备字符串的拼接
	private String getoOid1(String prefix) {
		String pre = prefix;
		String mid = getYearFix();
		String num = getNum();
		return pre + mid + num;
	}
	//判断是0000几
	private String getNum() {
		String oid = getOid();
		String targetStr = oid.substring(4);
		targetStr = targetStr.replace("0", "");
		Integer num = Integer.parseInt(targetStr) + 1;
		int count = LengthNum(num);
		int preCount = 5 - count;
		StringBuffer sb = new StringBuffer("");

		for (int i = 0; i < preCount; i++) {
			sb.append(0);
		}
		return sb.toString() + num;

	}
	//拿到年份19
	private String getYearFix() {
		Date date = new Date();
		String dateStr = date.toString();
		return dateStr.substring(dateStr.length() - 2);
	}
	//查询数据库中id最大的
	private String getOid() {

		try {
			String sql = "select oid from ordersd Where oid is not null order by id desc limit 0,1 ";
			return queryRunner.query(sql, new ScalarHandler());
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}

	// 判断一个数是几位数
	public int LengthNum(int num) {
		int count = 0; // 计数
		while (num >= 1) {
			num /= 10;
			count++;
		}
		return count;
	}

}

6 测试类


import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.sheng.damo2.Person;

public class TestDamo {
	
	@Test
	public void show() {
		ApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "applicationContext.xml" });
		OrderDao order=(OrderDao) context.getBean("orderDao");
		System.out.println(order);
		order.addOrder("xx", "name");
	}
}

以上代码,copy下来就可以直接用哈,如有疑问,欢迎留言哈

你可能感兴趣的:(java,Spring框架,java,Spring框架id生成器)