在bean包下建立User类和User.hbm.xml文件,实现User类和数据库表User的映射关系,具体User类不多说,User.hbm.xml如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> <hibernate-mapping> //实现User类和表的映射 <class name="com.whl.bean.User"> <id name="id" type="java.lang.Integer" column="id"> <generator class="increment"/> </id> <property name="username" /> <property name="userpass" column="userpass"/> </class> </hibernate-mapping>
7.applicationContext.xml文件中实现前面所有需要注入的对象
1 <?xml version="1.0" encoding="UTF-8"?> 2 3 <beans xmlns="http://www.springframework.org/schema/beans" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:aop="http://www.springframework.org/schema/aop" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans.xsd 8 http://www.springframework.org/schema/context 9 http://www.springframework.org/schema/context/spring-context.xsd 10 http://www.springframework.org/schema/aop 11 http://www.springframework.org/schema/aop/spring-aop.xsd"> 12 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> 13 <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 14 <property name="url" value="jdbc:mysql://localhost:3306/test"></property> 15 <property name="username" value="root"></property> 16 <property name="password" value="123456" /> 17 </bean> 18 19 <bean id="sessionFactory" 20 class="org.springframework.orm.hibernate5.LocalSessionFactoryBean"> 21 <property name="dataSource" ref="dataSource"></property> 22 <property name="hibernateProperties"> 23 <props> 24 <prop key="hibernate.dialect"> 25 org.hibernate.dialect.MySQL5InnoDBDialect 26 </prop> 27 <prop key="hibernate.hbm2ddl.auto">update</prop> 28 </props> 29 </property> 30 <property name="mappingResources"> 31 <list> 32 <value>com/whl/bean/User.hbm.xml</value> 33 </list> 34 </property> 35 </bean> 36 37 38 <bean id="userDao" class="com.whl.daoimpl.UserDaoImpl"> 39 <property name="sessionFactory" ref="sessionFactory"></property> 40 </bean> 41 42 43 <bean id="userService" class="com.whl.serviceimpl.UserServiceImpl"> 44 <property name="userDao" ref="userDao"></property> 45 </bean> 46 47 48 <bean id="LoginAction" class="com.whl.action.UserLoginAction"> 49 <property name="userService" ref="userService"></property> 50 </bean> 51 52 53 </beans>
8 在action类中实现真实的操作,并根据其结果做出相应的应答
package com.whl.action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; import com.whl.bean.User; import com.whl.service.IUserService; public class UserLoginAction extends ActionSupport implements ModelDriven<User> { /** * */ private static final long serialVersionUID = 1L; private User user=new User(); private IUserService userService; public String userLongin(){ User u = userService.getUserByNamePass(user.getUsername(), user.getUserpass()); if (u!=null) { ActionContext ac = ActionContext.getContext(); ac.put("user", u.getUsername()); return SUCCESS; } return INPUT; } public void setUserService(IUserService userService) { this.userService = userService; } @Override public User getModel() { return user; } }
时间: 2024-10-13 05:36:25