DataFieldMaxValueIncrementer 管理主键

package com.sise.spring.dao.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.support.AbstractLobCreatingPreparedStatementCallback;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.jdbc.support.incrementer.DataFieldMaxValueIncrementer;
import org.springframework.jdbc.support.lob.LobCreator;
import org.springframework.jdbc.support.lob.LobHandler;

import com.sise.spring.dao.OrderDao;
import com.sise.spring.entiey.Order;

public class OrderDaoImpl extends JdbcDaoSupport implements OrderDao {

 private LobHandler lobHander;
 //主键值产生器
 private DataFieldMaxValueIncrementer incre;
 public void setLobHander(LobHandler lobHander) {
  this.lobHander = lobHander;
 }
 public void setIncre(DataFieldMaxValueIncrementer incre) {
  this.incre = incre;
 }
 
 public void addOrder(final Order order){
  String sql = "insert into t_order(order_id,user_id,order_con) values(?,?,?)";
  getJdbcTemplate().execute(sql,new AbstractLobCreatingPreparedStatementCallback(this.lobHander) {
   
   @Override
   protected void setValues(PreparedStatement ps, LobCreator lobCreator)
     throws SQLException, DataAccessException {
    ps.setInt(1, incre.nextIntValue());
    ps.setInt(2, order.getUserId());
    lobCreator.setClobAsString(ps, 3, order.getOrderCon());
    
   }
  });
 }
 public List getOrders(int orderId) {
  // TODO Auto-generated method stub
  return null;
 }


}

 这个小小的例子主要的是解决并发的问题,在下面的例子我会做一个全部表的主键放到一个序列表上面来统一管理

 

 

我给出部分代码,全部代码我放在附件里

OrderDaoImpl.java

 

你可能感兴趣的:(java,DAO,spring,sql,jdbc)