SSH框架整合---注解的方式

来源:互联网 发布:挪威三文鱼事件知乎 编辑:程序博客网 时间:2024/05/22 06:19

首先,导入一个jar,struts2-convention-plugin-2.3.24.jar.只有导入了这个jar包,才能使用struts2框架的注解

其他的jar包请查看我的另一篇文章     SSH框架整合---xml方式


以下是配置文件

Web.xml 文件中要配置:(配置spring与struts2)


<?xml version="1.0" encoding="UTF-8"?>
<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_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>ssh-annotation</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>


<!-- 以下是配置spring -->
<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>


<!-- openSessionInViewFilter -->
<filter>
<filter-name>openSessionInView</filter-name>
<filter-class>org.springframework.orm.hibernate5.support.OpenSessionInViewFilter</filter-class>
</filter>


<filter-mapping>
<filter-name>openSessionInView</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>


<!-- struts2 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>


<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>


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">


<!-- 加载properties文件 -->
<context:property-placeholder location="classpath:db.properties" />


<!-- 开启注解扫描  @Respostory  @Service  @Controller-->
<context:component-scan base-package="cn.itcast"/>


<!-- 配置连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- 声明sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
<!-- 加载连接池 -->
<property name="dataSource" ref="dataSource" />
<property name="hibernateProperties">
<value>
hibernate.show_sql=true
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.hbm2ddl.auto=update
hibernate.format_sql=true
</value>
</property>
<!-- 加载注解类,只有配置了这个,实体类才能被扫描的到,才能使用注解 -->
<property name="packagesToScan">
<list>
<value>cn.itcast.domain</value>
</list>
</property>
</bean>

<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>

<!-- 事务注解驱动 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>



db.properties文件的配置(我使用的MySql)

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///sshtest
jdbc.username=root
jdbc.password=*****


好了,配置文件已经完成了,可以使用注解进行开发了

在Dao类中配置一个HibernateTemplate对象

代码如下

@Repository
public class UserDao extends HibernateDaoSupport implements IUserDao {

@Autowired
@Qualifier("sessionFactory")
public void setSuperSessionFactory(SessionFactory factory){
super.setSessionFactory(factory);
}

//添加的方法
public void add(User user){
this.getHibernateTemplate().save(user);
}

}


Service 编写

@Service
@Transactional //开启事务
public class UserService implements IUserService {


@Autowired
private IUserDao userDao;
@Override
public void add(User user) {
userDao.add(user);
}
}



编写action

@Controller
@Namespace("/")
@Scope("prototype")
@ParentPackage("struts-default")
public class UserAction extends ActionSupport implements ModelDriven<User> {


User user = new User();
@Override
public User getModel() {
// TODO Auto-generated method stub
return user;
}
//注入service
@Autowired
private IUserService userService;

@Action(value = "user_add",results={@Result(name="success",location="/index.jsp")})
public String add(){
userService.add(user);
return SUCCESS;
}


}

还有实体类的注解

@Entity
@Table(name="s_user")
public class User {


@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private int id;
private String name;
private String password;

好了 基本的注解开发就完成了

原创粉丝点击