開發工具采用MYECLIPS
<hibernate
<session
<!
<property name=
root
</property>
<property name=
jdbc:mysql://localhost:
</property>
<property name=
net
</property>
<property name=
<property name=
org
</property>
<!
<mapping resource=
<mapping resource=
<mapping resource=
</session
mapping為JAVABEAN所對應的映射
下面我們繼續HIBERNATE程序的下步編寫
import net
import net
import net
import net
/** * Description of the Class * *
@author tonny * @created
*/public class HibernateUtil
{
private final static SessionFactory sessionFactory;
static
{
try
{
sessionFactory =
new Configuration(nfigure()
}
catch (HibernateException ex)
{
throw new RuntimeException(
}
}
private HibernateUtil(){ }
/** * Description of the Field
*/
private final static ThreadLocal
session = new ThreadLocal();
/** * Description of the Method
* * @return
Description of the Return Value *
@exception HibernateException
Description of the Exception */
public static Session currentSession()
throws HibernateException
{
Session s = (Session) session
if (s == null)
{
s = sessionFactory
session
} return s;
}
/** * Description of the Method
* * @exception HibernateException
Description of the Exception */
public static void closeSession()
throws HibernateException {
Session s = (Session) session
session
if (s != null)
{
s
}
}
public static void init()
{
}
}
創建sessionFactory
import net
import net
import net
import org
import org
import onfig
import com
public class HibernatePlugin
implements org
{
public void init(ActionServlet servlet
ModuleConfig config)
{
HibernateUtil
}
public void destroy()
{
try
{
HibernateUtil
}
catch(HibernateException hex)
{
hex
}
}
}
以上為HIBERNATE基本配置
import com
public class DAOFactory
{
private static DAOFactory instance;
public synchronized static DAOFactory getInstance()
{
if
(instance == null)
{
instance = new DAOFactory();
}
return instance;
}
private DAOFactory()
{
}
public ItemDAO getItemDAO()
{
return new ItemDAOHibernate();
}
public ReviewDAO getReviewDAO()
{
return new ReviewDAOHibernate();
}
public UserDAO getUserDAO()
{
return new UserDAOHibernate();
}
}
struts
<controller contentType=
debug=
nocache=
processorClass=
<message
<plug
<plug
<set
<set
<set
value=
<set
value=
<set
value=
</plug
下面我們定義服務層
public class ServiceFactory
{
private static ServiceFactory instance;
public synchronized static ServiceFactory getInstance()
{
if (instance == null)
{
instance = new ServiceFactory();
}
return instance;
}
private ServiceFactory()
{
}
public
IService getService()
{
return new ServiceImp();
}
}
import com
import com
import com
import java
import javax
public interface IService
{
public UserContainer login(UserForm userForm);
public boolean logout(UserContainer userContainer);
public boolean addBlog(BlogForm blogForm
public boolean removeBlog(Long id);
public boolean addReview(Long topicId
public boolean updateBlog(Long id
public boolean removeReview(Long id);
public List getItems();
public ItemView getItem(Long id);
public ItemView getEditItem(Long id);
public List search(SearchForm searchForm);
/** * @param id * @param userForm */
public boolean addUser(UserForm userForm);
}
import com
import com
import com
import com
import java
import com
public class ServiceImp implements IService
{
public UserContainer login(UserForm userForm)
{
UserDAO userDAO=DAOFactory
User user=userDAO
if(user==null)return new UserContainer(
if(!user
return new UserContainer(
return new UserContainer(userForm
}
public boolean logout(UserContainer userContainer)
{
userContainer
userContainer
return true;
}
public boolean addBlog(BlogForm blogForm
{
ItemDAO itemDAO=DAOFactory
Item item=new Item(blogForm
blogForm
FileUpload
itemDAO
return true;
}
public boolean removeBlog(Long id)
{
ReviewDAO reviewDAO=DAOFactory
From:http://tw.wingwit.com/Article/program/Java/ky/201311/28319.html