如何在Spring Hibernate中首次运行服务器时自动创建数据库和表,而不执行任何操作? 这是我的xml文件。但不是创建数据库。如果已经存在db,则此代码在我第一次操作时自动创建表。至少我需要在启动服务器时自动创建表。
@{
AjaxOptions options = new AjaxOptions
{
HttpMethod = "Post",
UpdateTargetId = "formContent"
};
}
@using (Ajax.BeginForm("_AcceptarPeticio", "PeticioUsuaris", null,options ))
{
}
例外如下:
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/test" />
<property name="username" value="***" />
<property name="password" value="***" />
</bean>
<bean id="hibernate3AnnotatedSessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.current_session_context_class">thread</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
</property>
</bean>
答案 0 :(得分:0)
如果您希望在部署应用程序时让数据库处于已知状态,请使用:
protected void onPostExecute(final String result) {
m_ProgressView.setVisibility(View.GONE);
try {
m_oResponseobject = new JSONObject(result);// getting response from server
JSONArray posts = m_oResponseobject.optJSONArray("dealList");
for (int i = 0; i < posts.length(); i++) {
JSONObject post = posts.getJSONObject(i);
item = new CDealAppDatastorage();
item.setM_szHeaderText(post.getString("dealname"));
item.setM_szsubHeaderText(post.getString("dealcode"));
item.setM_n_Image(m_n_FormImage[i]);
s_oDataset.add(item);
}
// LoadMore button
Button btnLoadMore = new Button(getActivity());
btnLoadMore.setText("LOAD MORE DEALS");
btnLoadMore.setBackgroundResource(R.drawable.button_boarder);
btnLoadMore.setTextAppearance(getActivity(), android.R.style.TextAppearance_DeviceDefault_Small);
btnLoadMore.setTextColor(Color.WHITE);
btnLoadMore.setGravity(Gravity.CENTER);
// Adding Load More button to lisview at bottom
if(listview.getCount()>0){
m_ListView.addFooterView(btnLoadMore);
}
m_oAdapter = new CDealAppListingAdapter(getActivity(), s_oDataset);// create adapter object and add arraylist to adapter
m_ListView.setAdapter(m_oAdapter);//adding adapter to recyclerview
m_oAdapter.notifyDataSetChanged();
}
如果您只想生成数据库模式,请使用:
hibernate.hbm2ddl.auto=create-drop
在您的xml中,您已指定该属性两次。
其他的事情是:
hibernate.hbm2ddl.auto=create
未知数据库&#39;测试&#39; - 你创建了一个名为test的数据库吗?
我强烈建议您使用一些独立工具测试连接。例如,您可以在intellij的数据库视图中添加jdbc url并检查它是否正确。