hibernate拦截器:
import com.yourClass; import org.apache.log4j.Logger; import org.hibernate.EmptyInterceptor; import javax.persistence.Table; import java.text.SimpleDateFormat; import java.util.Date; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * * 关于 hibernate 的拦截器 拿来做分表吧,暂时 不实现 等有需要的 时候实现 * * * */ public class MyInterceptor extends EmptyInterceptor{ private static Logger logger = Logger.getLogger(MyInterceptor.class); private static final long serialVersionUID = 1L; private static String name = null; @Override public String onPrepareStatement(String sql) { return super.onPrepareStatement(sqlFilter(sql)); } public static String sqlFilter(String sql) { if (name == null) { Table table = yourClass.class.getAnnotation(Table.class); name = table.name(); } if (sql.contains(name)) { String regex = "\\$\\{(.+?)\\}"; Matcher matcher = Pattern.compile(regex).matcher(sql); while (matcher.find()) { SimpleDateFormat sdf = new SimpleDateFormat(matcher.group(1));//匹配正则 sql = matcher.replaceAll(sdf.format(new Date())); } } return sql; } }
spr.xml配置
<bean id="myInterceptor" class="MyInterceptor"/> <bean id="sessionFactory" class="db.distributed.SessionFactoryCreator"> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop> <prop key="hibernate.show_sql">${sys.show_sql}</prop> <prop key="hibernate.format_sql">false</prop> </props> </property> <property name="entityInterceptor" ref="myInterceptor"/> </bean>
dao实体类
@Repository public class yourClassHibernate extends DaoSupport<yourClass> implements yourClassDao { /** * */ private static final long serialVersionUID = 1L; @Override public boolean createTable() { return getHibernateTemplate().execute(new HibernateCallback<Boolean>() { @Override public Boolean doInHibernate(Session session) throws HibernateException, SQLException { session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { String tableName = yourClass.class.getAnnotation(Table.class).name(); String sql = "CREATE TABLE " + tableName + " LIKE yourClassTableName;";//复制表结构 sql = MyInterceptor.sqlFilter(sql); PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.execute(); preparedStatement.close(); } }); return true; } }); } }dao接口:
public interface yourClassDao extends Dao<yourClass> { boolean createTable(); }
服务层实现类:
@Service public class yourClassServiceImpl extends ServiceSupport<yourClass> implements yourClassService { /** * */ private static final long serialVersionUID = 1L; /** * */ @Autowired yourClassDao dao; public boolean saveOrUpdate(yourClass t) { boolean result = false; try { t.setCreateTime(new Date()); result = dao.saveOrUpdate(t); } catch (RuntimeException e) { Throwable tr = e; while (tr.getCause() != null) { tr = tr.getCause(); } if (Pattern.compile("Table '.*' doesn't exist").matcher(tr.getMessage()).matches()) { dao.createTable(); } throw e; } return result; } }action里的调用:
try { yourClassService.saveOrUpdate(yourClass); }catch (Exception e) { logger.info("create yourClassTableName table"); yourClassService.saveOrUpdate(yourClass); }
yourClass:
@Entity @Table(name = "yourClassTableName_${yyyyMM}") @Level(Level.TYPE.PRIVATE) @org.hibernate.annotations.Entity(dynamicUpdate=true,dynamicInsert=true) public class youClassName extends AbstractItem implements ItemBase {}