我这个程序的大体初衷就是在tomcat启动当中,在不重启tomcat的情况下,让hibernate帮我们动态建表,具体的实例就是:
注意下面的@Resource,这个非常重要,必须是这样,才能够获得正确的AnnotationSessionFactoryBean
@Controller
public class CustomAction extends BaseController {
@Resource
private DataSource dataSource;
@Resource
private AnnotationSessionFactoryBean annotationSessionFactoryBean;
下面就是Controller 的方法体了,
@RequestMapping("test111")
public String execute() throws MappingException, IOException,
ClassNotFoundException {
SessionFactoryImpl sessionFactory = (SessionFactoryImpl) this.baseDao
.getSessionFactory();
AnnotationConfiguration config = (AnnotationConfiguration) annotationSessionFactoryBean
.getConfiguration();
config.addAnnotatedClass(User9999.class);
Settings settings = sessionFactory.getSettings();
if (settings.isAutoUpdateSchema()) {
new SchemaUpdate(config,settings).execute(false, true);
}
return "test111";
}