首页 > 其他 > 详细

SSH三大框架注解整合(二)

时间:2014-05-17 20:52:25      阅读:402      评论:0      收藏:0      [点我收藏+]

5.使用spring注解注入service,DAO
 action:

@ParentPackage(value = "struts-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class BookAction extends ActionSupport implements ModelDriven<Book>{
 
//模型驱动
public Book book = new Book();
public void setBook(Book book) {
this.book = book;
}
 
@Override
public Book getModel() {
return book;
}
 
@Autowired
@Qualifier("bookService")
private BookService bookService;
@Override
@Action(value="addBook",results={@Result(name="success",location="/success.jsp")})
public String execute() throws Exception {
bookService.save(book);
return SUCCESS;
}

 service:

@Service("bookService")
@Transactional
public class BookService {
 
@Autowired
@Qualifier("bookDAO")
private BookDAO bookDAO;
 
public void save(Book book) {
bookDAO.save(book);
}

}
6.DAO完成数据操作,首先要有hibernateTemplate,通过spring注解注入 

@Repository("bookDAO")
public class BookDAO {
@Autowired
@Qualifier("hibernateTemplate") 
private HibernateTemplate hibernateTemplate;
 
public void save(Book book) {
this.hibernateTemplate.save(book);
}

7.实体类,不再需要hbm.xml文件 
 
@Entity

@Table(name = "book")
public class Book {
@Id
private int id;
@Column(name="name")
private String name;
@Column(name="author")
private String author;
8.applicationContext.xml文件
 
<!-- 自动扫描 -->
<context:component-scan base-package="cn.yuzhi"></context:component-scan>
<!-- 注解注入 -->
<context:annotation-config></context:annotation-config>
 
<!-- 引入外部属性文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<property name="password" value="${jdbc.password}"></property>
</bean>
 
<!-- 定义Bean : SessionFactory  -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<!-- 将连接池注入到SessionFactory -->
<property name="dataSource" ref="dataSource"></property>
<!-- 配置hibernate常用属性 -->
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
</props>
</property>
<!-- 引用注解类 ,不再引用hbm文件-->
<property name="packagesToScan">
<list>
<value>cn.yuzhi.domain</value>
</list>
</property>
</bean>
 
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
 
<!-- spring 采用声明式事务管理  -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>

SSH三大框架注解整合(二),布布扣,bubuko.com

SSH三大框架注解整合(二)

原文:http://www.cnblogs.com/aimeng-q/p/3733149.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!