今天弄了一天,查了N次资料和baidu,终于做出来啦。自己被自己感动了。学习中,大家都别笑话。
实体类:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
@Entity
@Table(name="user")
public class User {
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
@Column(name="name",length=15)
private String name;
@Column(name="password",length=15)
private String password;
public User(){
}
public User(String name, String password) {
this.name = name;
this.password = password;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
DAO层的接口与实现类:
import com.iesgroup.annotation.entity.User;
public interface UserDao {
public void save(User user);
}
import javax.annotation.Resource;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;
import com.iesgroup.annotation.dao.UserDao;
import com.iesgroup.annotation.entity.User;
@Repository
public class UserDaoImpl implements UserDao {
@Resource private SessionFactory sessionFactory;
public void save(User user) {
// sessionFactory.getCurrentSession().persist(user);
sessionFactory.openSession().save(user);
}
}
这里的实现类在使用annotation的时候,最好不要给它起别名,UserDaoImpl的类名字只能是默认的userDaoImpl。否则的话会出现此bean无法创建的错误。
service层的接口与实现类
[
import com.iesgroup.annotation.entity.User;
public interface UserService {
public void addUser(User user);
}
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.iesgroup.annotation.dao.UserDao;
import com.iesgroup.annotation.entity.User;
import com.iesgroup.annotation.service.UserService;
@Service
@Transactional(propagation=Propagation.SUPPORTS)
public class UserServiceImpl implements UserService {
@Resource private UserDao userDao;
// @Transactional(propagation=Propagation.REQUIRES_NEW)
public void addUser(User user) {
userDao.save(user);
}
}
form:
import org.apache.struts.action.ActionForm;
public class UserForm extends ActionForm {
private int id;
private String name;
private String password;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
action:
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.springframework.stereotype.Controller;
import com.iesgroup.annotation.entity.User;
import com.iesgroup.annotation.form.UserForm;
import com.iesgroup.annotation.service.UserService;
@Controller("/add")
public class UserAction extends MappingDispatchAction {
@Resource private UserService userService;
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
UserForm userForm = (UserForm)form;
User user = new User(userForm.getName(),userForm.getPassword());
userService.addUser(user);
return mapping.findForward("success");
}
}
spring中配置数据源以及hibernate的一些配置项
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<context:annotation-config/>
<context:component-scan base-package="*"></context:component-scan>
<bean id="dataSource"
class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName"
value="com.mysql.jdbc.Driver">
</property>
<property name="url" value="jdbc:mysql://localhost:3306/user"></property>
<property name="username" value="root"></property>
<property name="password" value="2764038"></property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="annotatedClasses">
<list>
<value>com.iesgroup.annotation.entity.User</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
<!-- 配置事务管理 -->
<bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="userForm" type="com.iesgroup.annotation.form.UserForm"></form-bean>
</form-beans>
<global-exceptions />
<global-forwards />
<action-mappings>
<action path="/add" name="userForm" parameter="add">
<forward name="success" path="/success.jsp"/>
<forward name="add" path="/index.jsp"></forward>
</action>
</action-mappings>
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/>
</controller>
<message-resources parameter="com.iesgroup.annotation.ApplicationResources" />
</struts-config>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
这两天找了很久都没找到好的资料,自己把已有的一次资料整理了一下,做了出来,给正在寻找的朋友留个低而已。
只是把流程过了一遍而已,有什么不对的地方请大家多指教。