配置Hibernate
1.右击项目—>MyEclipse—>Add Hibernate Capabilities
2.点击Next
3.点击Next
4.
Connect URL: jdbc:sqlserver://localhost:1433;databaseName=MyDB;user=sa;password=sa;
Driver Class: com.microsoft.sqlserver.jdbc.SQLServerDriver
Username: sa
Password: sa
Dialect: Microsoft SQL Server
5.点击Next
6.取消 Create SessionFactory class? 选择按钮的选择
7.点击Finish
8.拷贝hibernate-distribution-3.6.7.Final-dist.zip压缩包解压后的目录
<1>hibernate-distribution-3.6.7.Final\lib\required 下的 jar文件到lib下
<2>hibernate-distribution-3.6.7.Final\lib\jpa下的hibernate-jpa-2.0-api-1.0.1.Final.jar 到lib下
9.SQLserver的JDBC
拷贝sqljdbc4.jar到lib下
10编写News的Model
News.java:
package binary.models;
public class News
{
private int id;
private String title;
private String content;
public News(){}
public void setId(int id)
{
this.id = id;
}
public int getId()
{
return id;
}
public void setTitle(String title)
{
this.title = title;
}
public String getTitle()
{
return title;
}
public void setContent(String content)
{
this.content = content;
}
public String getContent()
{
return content;
}
}
11.在package binary.models; 的目录下添加xml文件
News.hbm.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-mapping package="binary.models" >
<class name="News" table="news_table" >
<id name="id" unsaved-value="null">
<generator class="identity" />
</id>
<property name="title" />
<property name="content" />
</class>
</hibernate-mapping>
12.修改hibernate.cfg.xml,红色部分为添加的内容
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.SQLServerDialect</property>
<property name="connection.url">jdbc:sqlserver://localhost:1433;databaseName=MyDB;user=sa;password=sa;</property>
<property name="connection.username">sa</property>
<property name="connection.password">sa</property>
<property name="connection.driver_class">com.microsoft.sqlserver.jdbc.SQLServerDriver</property>
<mapping resource="binary/models/News.hbm.xml" />
</session-factory>
</hibernate-configuration>
13.添加Action:
NewsDealer.java:
package binary.actions;
import org.hibernate.*;
import org.hibernate.cfg.Configuration;
import binary.models.News;
public class NewsDealer
{
public String execute() throws Exception
{
Configuration conf = new Configuration().configure();
SessionFactory sf = conf.buildSessionFactory();
Session sess = sf.openSession();
Transaction tx = sess.beginTransaction();
News n = new News();
n.setTitle("自定义标题");
n.setContent("自定义内容");
sess.save(n);
tx.commit();
sess.close();
return "success";
}
}
14.修改struts.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" namespace="/" extends="struts-default">
<action name="NewsDealer" class="binary.actions.NewsDealer" method="execute">
<result name="success">/index.jsp</result>
</action>
</package>
</struts>
15.访问http://localhost:8080/NewsDealer.action 就可以了
浙公网安备 33010602011771号