源码:http://pan.baidu.com/s/1i4xVLE9(百度云)
步骤:一、创建数据库(mysql)
二、导入相应jar包(注意不同数据库jdbc.jar包)配置web.xml、spring、Hibernate
三、编写测试类
说明:各配置文件应正确指向,否则容易产生找不到sessionfactory的错误。
本文各配置文件的指向关系:
具体步骤:
一、创建数据库(mysql),记住数据用户名,密码。
二、配置web.xml、spring、Hibernate
1、web.xml
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:config/springAnnotation-*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>springMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--springMVC配置文件地址,config是src下的包 -->
<param-value>classpath*:config/springAnnotation-servlet.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- encoding filter for jsp page -->
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet-mapping>
<servlet-name>springMVC</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<filter>
<filter-name>openSession</filter-name>
<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>openSession</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
2、springAnnotation-core.xml配置
<beans>
<import resource="classpath*:config/springAnnotation-import.xml"/>
</beans>
3、springAnnotation-hibernate.xml配置
<beans>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/hollybrain"/>
<property name="username" value="root"/>
<property name="password" value="cxf8068221"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">create</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hiberante.format_sql">true</prop>
</props>
</property>
<property name="configLocations">
<list>
<value>
classpath*:config/hibernate.cfg.test.xml
</value>
</list>
</property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="transactionBese" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean" lazy-init="true" abstract="true">
<property name="transactionManager" ref="transactionManager"></property>
<property name="transactionAttributes">
<props>
<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="insert*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="modify*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
<prop key="get*">PROPAGATION_NEVER</prop>
</props>
</property>
</bean>
</beans>
3、springAnnotation-servlet.xml配置
<!-- 注解扫描包 -->
<context:component-scan base-package="com.chuck"></context:component-scan>
<!-- 开启注解 -->
<mvc:annotation-driven/>
<!-- 静态资源访问 -->
<mvc:resources location="/img/" mapping="/img/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/staticHtml/" mapping="/staticHtml/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/"></property>
<property name="suffix" value=".jsp"></property>
</bean>
4、hibernate.cfg.test.xml配置
<hibernate-configuration>
<session-factory>
<mapping class="com.chuck.entity.User"/>
</session-factory>
</hibernate-configuration>
5、springAnnotation-import.xml配置
<beans>
<bean id="userDao" class="com.chuck.dao.UserDAO">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="userManagerBase" class="com.chuck.service.UserManager">
<property name="userDao" ref="userDao"></property>
</bean>
<bean id="userManager" parent="transactionBese">
<property name="target" ref="userManagerBase"></property>
</bean>
</beans>
三、测试类的编写
1、user类
@Entity
@Table(name="tb_user")
public class User {
@Id
@GeneratedValue(generator = "system-uuid")
@GenericGenerator(name = "system-uuid", strategy = "uuid")
private String userName;
private String age;
private String password;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2、IUserDAO接口编写
public interface IUserDAO {
public void addUser(User user);
}
3、UserDAO类编写
public class UserDAO implements IUserDAO {
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
@Override
public void addUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
}
4、IUserManager接口编写
public interface IUserManager {
public void addUser(User user);
}
5、UserManager类编写
public class UserManager implements IUserManager{
private IUserDAO userDao;
public IUserDAO getUserDao() {
return userDao;
}
public void setUserDao(IUserDAO userDao) {
this.userDao = userDao;
}
public void addUser(User user) {
userDao.addUser(user);
}
}
6、LoginController测试类编写
@Controller
@RequestMapping("/user")
public class LoginController extends MultiActionController {
@Resource(name="userManager")//拿到spring配置文件里的bean
private IUserManager userManager;
@RequestMapping("/addUser")
public String addUser(User user,HttpSession httpSession){
userManager.addUser(user);
httpSession.setAttribute("userName",user.getUserName());
return "/welcome";
}
}
7、addUser.jsp页面编写
<form action="user/addUser" method="post">
姓名:<input type="text" name="userName"/>
年龄:<input type="text" name="age"/>
<input type="submit" value="登陆"/>
<br/>还没有账号?请点击<a>注册</a>
</form>