Action 代码
@Action("login")
@Scope("prototype")
public class UserAction extends ActionSupport implements ModelDriven<User> {
@Resource
private User user;
private UserService userService;
@Override
public String execute() throws Exception {
userService.login(user.getUserName(), user.getPassword());
return NONE;
}
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
}
applicationContext.xml代码
<?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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<context:annotation-config />
<context:component-scan base-package="dws.d">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!-- 引入配置文件 -->
<context:property-placeholder location="classpath:config.properties"
ignore-unresolvable="true" />
<!-- C3P0连接池配置 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property value="${jdbc.driver}" name="driverClass" />
<property value="${jdbc.url}" name="jdbcUrl" />
<property value="${jdbc.username}" name="user" />
<property value="${jdbc.password}" name="password" />
<property value="${jdbc.initialPoolSize}" name="initialPoolSize" />
<property value="${jdbc.maxPoolSize}" name="maxPoolSize" />
<property value="${jdbc.minPoolSize}" name="minPoolSize" />
<property value="${jdbc.maxStatements}" name="maxStatements" />
<property value="${jdbc.maxStatementsPerConnection}" name="maxStatementsPerConnection" />
<property value="${jdbc.acquireIncrement}" name="acquireIncrement" />
<property value="${jdbc.acquireRetryAttempts}" name="acquireRetryAttempts" />
<property value="${jdbc.autoCommitOnClose}" name="autoCommitOnClose" />
<property value="${jdbc.acquireRetryDelay}" name="acquireRetryDelay" />
<property value="${jdbc.maxIdleTimeExcessConnections}" name="maxIdleTimeExcessConnections" />
<property value="${jdbc.maxIdleTime}" name="maxIdleTime" />
<property value="${jdbc.idleConnectionTestPeriod}" name="idleConnectionTestPeriod" />
<property value="${jdbc.breakAfterAcquireFailure}" name="breakAfterAcquireFailure" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<!-- 注入hibernate基本属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">${hibernate.dialect}</prop>
<prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
<prop key="hibernate.format_sql">${hibernate.format_sql}</prop>
<prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
</props>
</property>
<property name="annotatedPackages">
<list>
<value>
dws.d.entity
</value>
</list>
</property>
</bean>
<!-- 配置模板 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置事务 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 开启事务 -->
<tx:advice transaction-manager="transactionManager" id="myadvice">
<tx:attributes>
<tx:method name="*" isolation="DEFAULT" propagation="REQUIRED" />
</tx:attributes>
</tx:advice>
<!-- 配置AOP增强 -->
<aop:config>
<aop:advisor advice-ref="myadvice"
pointcut="execution(* dws.d.service.*ServiceImpl.*(..))" />
</aop:config>
</beans>
实体类代码
package dws.d.entity;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
@SuppressWarnings("serial")
@Entity
@Table(name = "user")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer ID;
private String UserName;
private String Password;
private Integer Character_ID;
private Integer Job_ID;
private Integer PersonInfo_ID;
public Integer getID() {
return ID;
}
public void setID(Integer iD) {
ID = iD;
}
public String getUserName() {
return UserName;
}
public void setUserName(String userName) {
UserName = userName;
}
public String getPassword() {
return Password;
}
public void setPassword(String password) {
Password = password;
}
public Integer getCharacter_ID() {
return Character_ID;
}
public void setCharacter_ID(Integer character_ID) {
Character_ID = character_ID;
}
public Integer getJob_ID() {
return Job_ID;
}
public void setJob_ID(Integer job_ID) {
Job_ID = job_ID;
}
public Integer getPersonInfo_ID() {
return PersonInfo_ID;
}
public void setPersonInfo_ID(Integer personInfo_ID) {
PersonInfo_ID = personInfo_ID;
}
public User() {
super();
}
public User(Integer iD, String userName, String password, Integer character_ID, Integer job_ID,
Integer personInfo_ID) {
super();
ID = iD;
UserName = userName;
Password = password;
Character_ID = character_ID;
Job_ID = job_ID;
PersonInfo_ID = personInfo_ID;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((Character_ID == null) ? 0 : Character_ID.hashCode());
result = prime * result + ((ID == null) ? 0 : ID.hashCode());
result = prime * result + ((Job_ID == null) ? 0 : Job_ID.hashCode());
result = prime * result + ((Password == null) ? 0 : Password.hashCode());
result = prime * result + ((PersonInfo_ID == null) ? 0 : PersonInfo_ID.hashCode());
result = prime * result + ((UserName == null) ? 0 : UserName.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (Character_ID == null) {
if (other.Character_ID != null)
return false;
} else if (!Character_ID.equals(other.Character_ID))
return false;
if (ID == null) {
if (other.ID != null)
return false;
} else if (!ID.equals(other.ID))
return false;
if (Job_ID == null) {
if (other.Job_ID != null)
return false;
} else if (!Job_ID.equals(other.Job_ID))
return false;
if (Password == null) {
if (other.Password != null)
return false;
} else if (!Password.equals(other.Password))
return false;
if (PersonInfo_ID == null) {
if (other.PersonInfo_ID != null)
return false;
} else if (!PersonInfo_ID.equals(other.PersonInfo_ID))
return false;
if (UserName == null) {
if (other.UserName != null)
return false;
} else if (!UserName.equals(other.UserName))
return false;
return true;
}
}