然后创建struts.xml(string配置文件) 要注意这里集成了spring插件,把全部spring配置文件注入到contextloaderplugin中
http://struts.apache.org/dtds/struts-config_1_2.dtd">
type="org.springframework.web.struts.delegatingactionproxy"
parameter="action"
scope="request">
配置spring配置文件
http://www.springframework.org/dtd/spring-beans.dtd">
配置action将service注入到action
http://www.springframework.org/dtd/spring-beans.dtd">
配置service将dao注入到service
http://www.springframework.org/dtd/spring-beans.dtd">
配置dao 注入session工厂
http://www.springframework.org/dtd/spring-beans.dtd">
数据源属性文件(注意不是hibernate的配置文件,是为了让spring读入的)
hibernate.dialect=org.hibernate.dialect.sqlserverdialect
hibernate.driverclassname=com.mysql.jdbc.driver
hibernate.url=jdbc:mysql://127.0.0.1:3306/ssh
hibernate.username=root
hibernate.password=5719
hibernate.showsql=true
hibernate.maxactive=50
hibernate.maxidle=30
hibernate.maxwait=1000
log4j配置文件(简单)
log4j.rootlogger=error,console,file
log4j.appender.console=org.apache.log4j.consoleappender
log4j.appender.console.layout=org.apache.log4j.patternlayout
log4j.appender.console.layout.conversionpattern=%-5p %d [%f,%l] - %m%n
log4j.appender.file=org.apache.log4j.rollingfileappender
log4j.appender.file.file=f:\\ssh.log
#log4j.appender.file.maxfilesize=100000kb
#log4j.appender.file.maxbackupindex=1
log4j.appender.file.layout=org.apache.log4j.patternlayout
log4j.appender.file.layout.conversionpattern=%-5p %d [%f,%l] - %m%n
下面是类文件
package com.action;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import org.apache.commons.logging.log;
import org.apache.commons.logging.logfactory;
import org.apache.struts.action.actionform;
import org.apache.struts.action.actionforward;
import org.apache.struts.action.actionmapping;
import org.apache.struts.actions.dispatchaction;
import com.service.loginservice;
import com.vo.loginvo;
public class loginaction extends dispatchaction {
private log logger = logfactory.getlog(loginaction.class);
private loginservice loginservice;
public void setloginservice(loginservice loginservice) {
this.loginservice = loginservice;
}
public actionforward login(actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse response) {
try {
loginvo loginvo = (loginvo) form;
string username = loginvo.getusername();
string password = loginvo.getpassword();
system.out.println(username password);
if(loginservice.validate(username, password)) {
return mapping.findforward("index");
}
return mapping.findforward("error");
} catch (exception e) {
logger.error(e);
return mapping.findforward("error");
}
}
public actionforward save(actionmapping mapping,actionform form,httpservletrequest request,httpservletresponse response) {
try {
loginvo loginvo = (loginvo) form;
loginservice.saveuser(loginvo);
return mapping.findforward("index");
} catch (exception e) {
logger.error(e);
return mapping.findforward("error");
}
}
}
package com.dao;
import java.util.list;
import org.springframework.orm.hibernate3.support.hibernatedaosupport;
import com.po.loginpo;
public class logindao extends hibernatedaosupport {
@suppresswarnings("unchecked")
public string getpassword(string username) {
string hql = "from loginpo l where l.username=?";
list list = getsession().createquery(hql).setstring(0,username).list();
if(list!=null && list.size()>0) {
loginpo loginpo = (loginpo) list.get(0);
return loginpo.getpassword();
}
return null;
}
public void save(loginpo loginpo) {
getsession().save(loginpo);
}
}
package com.po;
import java.io.serializable;
public class loginpo implements serializable {
private static final long serialversionuid = 1l;
private integer id = null;
private string username = null;
private string password = null;
public integer getid() {
return id;
}
public string getpassword() {
return password;
}
public string getusername() {
return username;
}
public void setid(integer id) {
this.id = id;
}
public void setpassword(string password) {
this.password = password;
}
public void setusername(string username) {
this.username = username;
}
}
package com.service;
import com.dao.logindao;
import com.po.loginpo;
import com.vo.loginvo;
public class loginservice {
private logindao logindao;
public void setlogindao(logindao logindao) {
this.logindao = logindao;
}
public boolean validate(string username,string password) {
string pass = logindao.getpassword(username);
if(pass!=null) {
if(pass.equals(password)) {
return true;
}
}
return false;
}
public void saveuser(loginvo loginvo) {
loginpo loginpo = new loginpo();
loginpo.setusername(loginvo.getusername());
loginpo.setpassword(loginvo.getpassword());
system.out.println(loginvo.getusername() "-" loginvo.getpassword() ":save succeed...");
logindao.save(loginpo);
//故意制造异常,测试事务。
//logindao.save(null);
}
}
package com.vo;
import org.apache.struts.action.actionform;
public class loginvo extends actionform {
private static final long serialversionuid = 1l;
private string username = null;
private string password = null;
public string getpassword() {
return password;
}
public string getusername() {
return username;
}
public void setpassword(string password) {
this.password = password;
}
public void setusername(string username) {
this.username = username;
}
}
hibernate映射文件
"">
jsp页面文件
<%@ page contenttype="text/html;charset=utf-8" language="java" %>
translator.compile((net.sf.hibernate.engine.sessionfactoryimplementor)sessionfactory, collections.empty_map, false);
return "select count(*) from (" translator.getsqlstring() ") tmp_count_t";
}
// 用于hibernate 3.x
protected string getcountsql(string originalhql, org.hibernate.sessionfactory sessionfactory) throws exception {
querytranslatorimpl querytranslator = new querytranslatorimpl(originalhql, originalhql,
collections.empty_map, (org.hibernate.engine.sessionfactoryimplementor)sessionfactory);
querytranslator.compile(collections.empty_map, false);
return "select count(*) from (" querytranslator.getsqlstring() ") tmp_count_t";
}
string strhql = "select count(*) from tb_po";
long count = (long)session.createquery(strhql).uniqueresult();
public category getparentcategory() {
return this.parentcategory;
}
public void setparentcategory(category parentcategory) {
this.parentcategory = parentcategory;
}
public set getchildcategories() {
return this.childcategories;
}
public void setchildcategories(set childcategories) {
this.childcategories = childcategories;
}
//为了节省空间省略了id,name属性的访问方法
}
cascade="save-update"
inverse="true"
>
column="category_id"
class="mypack.category"
/>
我觉得这种方式其实和上边的一对多,一对一关系一样,只不过两个用的都是同一个类罢了.看一下例子理解上应该很简单.
hi_value
next_value
100
public "-//hibernate/hibernate mapping dtd 2.0//en"
"">
public "-//hibernate/hibernate mapping dtd 2.0//en"
"">
public "-//hibernate/hibernate mapping dtd 2.0//en"
"">
private