<hibernate-mapping package="com.digital.entity">
<class name="UserInfo" table="user_info" catalog="digital">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native">generator>
id>
<property name="userName" type="java.lang.String">
<column name="userName" length="16" not-null="true" />
property>
<property name="password" type="java.lang.String">
<column name="password" length="16" not-null="true" />
property>
class>
hibernate-mapping>
使用Annotation完成UserInfo实体类与数据表的映射关系
@Entity
@Table(name = "user_info", catalog = "digital")
public class UserInfo {
对类中的属性进行映射 -主键属性
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", unique = true, nullable = false)
public int getId() {
对非主属性
@Column(name = "userName", length = 16)
public String getUserName() {
return userName;
使用注解类后,就不在需要UserInfo.hbx.xml映射文件删除
—————————————————————————————————————————————————————————
目的
实现:
声明hibernate事务管理器、定义事务通知、定义切面、将事务通知和切面组合起来
—————————————————————————————————————————————————————————
并且将各个命名空间关联到项目里
<bean id="dataSource"
class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver" />
<property name="jdbcUrl" value="jdbc:mysql:///digital" />
<property name="user" value="root" />
<property name="password" value="123456" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
bean>
配置Hibernate的SessionFactory(引用ref bean注入数据源dataSource、设置Hibernate基本属性,配置方言mysql,实例化SessionFactory)
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
prop>
props>
property>
<property name="mappingResources">
<list>
<value>com/digital/entity/UserInfo.hbm.xmlvalue>
list>
property>
bean>
<bean id="transactionManager"
class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
bean>
<context:annotation-config />
<context:component-scan base-package="com.digital" />
<tx:annotation-driven transaction-manager="transactionManager" />
简单的介绍事务的规则(传播行为)
常用required
Required
Support
Never
事务的管理主要任务:创建回滚提交事务 是否需要创建事务及如何创建事务有事务的传播行为控制
—————————————————————————————————————————————————————————
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" />
tx:attributes>
tx:advice>
<aop:config>
<aop:pointcut id="serviceMethods" expression="execution(* com.digital.service.*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="serviceMethods" />
aop:config>
—————————————————————————————————————————————————————————
public interface UserInfoDAO {
public List search(UserInfo cond);
}
public class UserInfoDAOImpl implements UserInfoDAO {
SessionFactory sessionFactory;
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
<bean id="userInfoDAO" class="com.digital.dao.impl.UserInfoDAOImpl">
<property name="sessionFactory" ref="sessionFactory" />
bean>
@Repository("userInfoDAO")
public class UserInfoDAOImpl implements UserInfoDAO {
// 通过@Autowired注解注入Spring容器中的SessionFactory实例
@Autowired
SessionFactory sessionFactory;
—————————————————————————————————————————————————————————
public interface UserInfoService {
public List login(UserInfo cond);
}
public class UserInfoServiceImpl implements UserInfoService {
UserInfoDAO userInfoDAO;
public void setUserInfoDAO(UserInfoDAO userInfoDAO) {
this.userInfoDAO = userInfoDAO;
}
<bean id="userInfoService" class="com.digital.service.impl.UserInfoServiceImpl">
<property name="userInfoDAO" ref="userInfoDAO" />
bean>
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
//使用@Transactional注解实现事务管理
@Transactional
//使用@Service注解在Spring容器中注册名为userInfoService的UserInfoServiceImpl实例
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
//使用@Transactional注解实现事务管理
@Transactional
//使用@Service注解在Spring容器中注册名为userInfoService的UserInfoServiceImpl实例
@Service("userInfoService")
public class UserInfoServiceImpl implements UserInfoService {
//使用@Autowired注解注入UserInfoDAOImpl实例
@Autowired
UserInfoDAO userInfoDAO;
—————————————————————————————————————————————————————————
public class UserInfoAction extends ActionSupport {
UserInfo ui;
public UserInfo getUi() {
return ui;
}
public void setUi(UserInfo ui) {
this.ui = ui;
}
UserInfoService userInfoService;
public void setUserInfoService(UserInfoService userInfoService) {
this.userInfoService = userInfoService;
}
public String doLogin() throws Exception {
List uiList = userInfoService.login(ui);
if (uiList.size() > 0) {
// 登录成功,转发到到index.jsp
return "index";
} else {
// 登录失败,重定向到login.jsp
return "login";
}
}
<bean name="uiAction" class="com.digital.action.UserInfoAction" scope="prototype">
<property name="userInfoService" ref="userInfoService" />
bean>
<struts>
<constant name="struts.i18n.encoding" value="utf-8">constant>
<package name="digital" namespace="/" extends="struts-default">
<action name="doLogin" class="uiAction" method="doLogin">
<result name="index" type="dispatcher">index.jspresult>
<result name="login" type="redirect">login.jspresult>
action>
package>
struts>
//使用@Controller注解在Spring容器中注册UserInfoAction实例
//使用@Scope("prototype")指定原型模式
@Controller
@Scope("prototype")
public class UserInfoAction extends ActionSupport {
UserInfo ui;
使用@Autowired:对userInfoService注入实例化对象
// 使用@Autowired注解注入UserInfoServiceImpl实例
@Autowired
UserInfoService userInfoService;
@Action(value=”/doLogin”,results={
@Result(name=”index”,type=”dispatcher”,location=”/index.jsp”),
@Result(name=”login”,type=”redirect”,location=”/login.jsp”)})
public String doLogin()throws Exception
{
List uiList = userInfoService.login(ui);
if(uiList.size()>0)
{
return "index";
}
else
return "login";
}
—————————————————————————————————————————————————————————
目的:
步骤:
实现步骤
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
/*
filter-mapping>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>digital-1display-name>
<welcome-file-list>
<welcome-file>index.htmlwelcome-file>
<welcome-file>index.htmwelcome-file>
<welcome-file>index.jspwelcome-file>
<welcome-file>default.htmlwelcome-file>
<welcome-file>default.htmwelcome-file>
<welcome-file>default.jspwelcome-file>
welcome-file-list>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
listener>
<context-param>
<param-name>contextConfigLocationparam-name>
<param-value>classpath:applicationContext.xmlparam-value>
context-param>
<filter>
<filter-name>struts2filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilterfilter-class>
filter>
<filter-mapping>
<filter-name>struts2filter-name>
<url-pattern>/*url-pattern>
filter-mapping>
web-app>
—————————————————————————————————————————————————————————
<struts>
<constant name="struts.i18n.encoding" value="utf-8">constant>
<package name="digital" namespace="/" extends="struts-default">
<action name="doLogin" class="uiAction" method="doLogin">
<result name="index" type="dispatcher">index.jspresult>
<result name="login" type="redirect">login.jspresult>
action>
package>
struts>
@Action(value=”/doLogin”,results={
@Result(name=”index”,type=”dispatcher”,location=”/index.jsp”),
@Result(name=”login”,type=”redirect”,location=”/login.jsp”)})