bearjava
bearjava · 共 36 篇文章
Hibernate get() load()的区别
hibernate中get方法和load方法的根本区别在于: 如果你使用load方法,hibernate认为该id对应的对象(数据库记录)在数据库中是一定存在的,所以它可以放心的使用,它可以放心的使用代理来延迟加载该对象。在用到对象中的其他属性数据时才查询数据库,但是万一数据库中不存在该记录,那没办法,只能抛异常,所说的load方法抛异常是指在使用该对象的数据时,数据库中不存在该数据时抛异常,而不是在创建这个对象时。由于session中的缓存对于hibernate来说是个相当廉价的资源,所以在load时会先查一下session缓存看看该id对应的对象是否存在,不存在则创建代理。所以如果你知道…
2009-01-13 22:04 · Open Source · 评论(0)
Lucene检索入门
Apache Lucene is a high-performance, full-featured text search engine library written entirely in Java. It is a technology suitable for nearly any application that requires full-text search, especially cross-platform. /** * 这个实例包含了lucene所有核心用法 */ public class LuceneTest { public static void main(St…
2008-12-07 14:39 · Open Source · 评论(0)
Java enum
使用enum可以方便的替代常量类。 /**enum可以单独定义*/ public enum Week { SUNDAY,MONDAY,TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY } public class ENUMTest { /**enum也可以定义在class里面*/ public enum Color{RED,ORANGLE,YELLO,GREEN,CYAN,BLUE,PURPLE} public static void main(String[] args){ for(Week w:Week.values()){ /**ordina…
2008-12-07 14:18 · Java · 评论(0)
Java Reflection
public class Customer implements Serializable{ private static final long serialVersionUID = 1L; private long id; private String name = "Mike"; private int age = 20; /**私有构造器也能通过反射访问*/ private Customer(){ } public Customer(String name,int age){ this.name = name; this.age = age; } public long getId()…
2008-12-07 14:10 · Java · 评论(0)
JDK动态代理
代理模式是给一个对象提供代理,以控制对它的访问,如只有授权用户才可以访问某个对象,给对象添加日志、事务功能。 JDK动态代理是提供运行时实现代理模式的一种方法,但是只支持接口类型。 Cglib可以支持类的代理。 /**业务接口*/ public interface IHello { public void hello(String name); } /**业务接口实现*/ public class HelloSpeaker implements IHello { @Override public void hello(String name) { System.out.println("He…
2008-12-07 13:45 · Java · 评论(0)
String str = new String("ABC")
public class Test{ public static void main(String[] args) { String str=new String("ABC"); } } 它的虚拟机指令集: Compiled from "Test.java" public class Test extends java.lang.Object{ public Test(); Code: 0: aload_0 1: invokespecial #1; //Method java/lang/Object."<init>":()V 4: return public static void main…
2008-12-06 11:58 · Java · 评论(0)
图形验证码
public class AuthImg extends HttpServlet { private static final long serialVersionUID = 1L; private Font mFont = new Font("Arial Black", Font.PLAIN, 16); public void init() throws ServletException { super.init(); } Color getRandColor(int fc,int bc) { Random random = new Random(); if(fc>255) fc=255;…
2008-12-05 00:35 · Java · 评论(0)
Spring的Servlet启动监听器
public class StartupListener extends ContextLoaderListener implements ServletContextListener { private static final Log log = LogFactory.getLog(StartupListener.class); private IMaintainService maintainService; public void setMaintainService(IMaintainService maintainService) { this.maintainService =…
2008-12-05 00:33 · Java · 评论(0)
MD5加密
public class SecurityByMD5 { private static final String[] HEXDIGITS = {"0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"}; public static void main(String[] args) { String result = encryptCardNo("fff"); System.out.println(result); } public static String encryptCardNo(String cardNo){ r…
2008-12-05 00:28 · Java · 评论(0)
扩展Acegi框架 自定义权限处理
Acegi默认不支持RBAC权限模型,需要自己扩展。对于数据的获取默认是SQL,也需要扩展。 public class RdbmsEntryHolder implements Serializable{ private static final long serialVersionUID = 2317309106087370323L; //保护的URL模式 private String url; //要求的角色集合 private ConfigAttributeDefinition cad; public String getUrl() { return url; } public Conf…
2008-12-05 00:26 · Java · 评论(0)
制作启动界面
以前写的坦克大战的启动代码。 public class Splash extends JFrame{ private static final long serialVersionUID = 1L; JPanel panel = new JPanel(); JLabel lblIcon = new JLabel(new ImageIcon("images/boot.jpg")); public JProgressBar bar = new JProgressBar(); public boolean status = true; Timer timer = new Timer(); publ…
2008-12-05 00:17 · Java · 评论(0)
设置JTable的列宽
public class JTableUtil { //自动设置列宽 public static void fitTableColumns(JTable table) { JTableHeader header = table.getTableHeader(); int rowCount = table.getRowCount(); Enumeration columns = table.getColumnModel().getColumns(); while (columns.hasMoreElements()) { TableColumn column = (TableColumn) c…
2008-12-05 00:08 · Java · 评论(1)
自己设计的JdbcTemplate
自己写的Jdbc模板,使用了Template,Strategy,Call Back等,可以自动设置参数,并可以完成ResultSet到JTable以及List的自动映射。极大减少重复代码,比较经典。 PreparedStatementCallback 是用于回调的接口 public interface PreparedStatementCallback { public Object doInPreparedStatement(PreparedStatement pstmt) throws SQLException,ClassNotFoundException; } public class…
2008-12-04 23:59 · Java · 评论(1)
XML解析之Digester
Digester package lets you configure an XML -> Java object mapping module, which triggers certain actions called rules whenever a particular pattern of nested XML elements is recognized. A rich set of predefined rules is available for your use, or you can also create your own. 映射XML对象 public class U…
2008-12-04 23:26 · Java · 评论(1)
构建CVS项目到Version One
以前用CVS的时候,一个新的项目需要频繁修改,还有很多文件、包、初始测试文件需要移除,这些对于项目搭建的辅助部分和正式的开发版本是不一样的,应该从DEV Version中移除,然后构建一个Version1的CVS项目。 记得刚开始大家都手动去删除Entries,Repository,Root这些东西,并且项目的任何一个位置都有这烦人的东东,后来我终于受不了,自己写了一个工具类来递归删除它们,重新构建Version1的项目。尽管现在自己开发时都用SVN不用CVS了,还是贴出来共享吧。 public class DeleteFile { private static String NODIR =…
2008-11-30 23:57 · Java · 评论(0)