1.我们之前的纯xml的方式,我们的配置文件很多,我们可以使用注解结合xml的方式进行开发,这样的话,我们的配置文件,会少很多,同时,我们可以直接在类中看到配置,这样,我们就可以快速地搭建一个ssh整合的项目了

  首先,我们应该考虑:我们的哪些东西需要我们用注解形式进行替换?

  第一个:hibernate中的关系映射文件,我们可以使用jpa结合hibernate注解进行替换

  第二个:spring中,我们的自定义的对象,我们可以使用我们的spring中ioc和tx的注解进行替换

  第三个:我们的struts.xml可以使用struts的注解替换

  这个是我们的一个大体上的思路,现在我们就去实现它!

  我们的 实体类Customer

package com.itheima.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table; @Entity
@Table(name="cst_customer")
public class Customer {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name="cust_id")
private Long custId; //客户编号
@Column(name="cust_name")
private String custName; //客户名称
@Column(name="cust_source")
private String custSource; //客户信息来源
@Column(name="cust_industry")
private String custIndustry; //客户所属行业
@Column(name="cust_level")
private String custLevel; //客户级别
@Column(name="cust_address")
private String custAddress; //客户联系地址
@Column(name="cust_phone")
private String custPhone; //客户联系电话
public Long getCustId() {
return custId;
}
public void setCustId(Long custId) {
this.custId = custId;
}
public String getCustName() {
return custName;
}
public void setCustName(String custName) {
this.custName = custName;
}
public String getCustSource() {
return custSource;
}
public void setCustSource(String custSource) {
this.custSource = custSource;
}
public String getCustIndustry() {
return custIndustry;
}
public void setCustIndustry(String custIndustry) {
this.custIndustry = custIndustry;
}
public String getCustLevel() {
return custLevel;
}
public void setCustLevel(String custLevel) {
this.custLevel = custLevel;
}
public String getCustAddress() {
return custAddress;
}
public void setCustAddress(String custAddress) {
this.custAddress = custAddress;
}
public String getCustPhone() {
return custPhone;
}
public void setCustPhone(String custPhone) {
this.custPhone = custPhone;
}
@Override
public String toString() {
return "Customer [custId=" + custId + ", custName=" + custName + ", custSource=" + custSource
+ ", custIndustry=" + custIndustry + ", custLevel=" + custLevel + ", custAddress=" + custAddress
+ ", custPhone=" + custPhone + "]";
}
}

  我们的applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- spring的配置文件:导入约束 -->
<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
"> <!-- 自定义的java对象交给spring进行管理,我们使用注解的形式替换-->
<context:component-scan base-package="com.itheima"></context:component-scan>
<tx:annotation-driven/>
<!-- 第三方的jar包中的java对象交给spring进行管理 -->
<!-- 创建一个hibernateTemplate对象 -->
<bean id="hibernateTemplate" class="org.springframework.orm.hibernate5.HibernateTemplate">
<!-- 注入sessionFactory -->
<property name="sessionFactory" ref="sessionFactory"></property>
</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">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.hbm2ddl.auto">update</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.format_sql">true</prop>
</props>
</property>
<!-- 指定要扫描的包
packagesToScan 在加载配置文件的时候,自动扫描包中的java类
-->
<property name="packagesToScan" value="com.itheima.entity"></property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql:///ssh_280"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>
<!--配置hibernate的事务管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>

  我们的CustomerAction

package com.itheima.action;

import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller; import com.itheima.entity.Customer;
import com.itheima.service.CustomerService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven; @Controller
@ParentPackage("struts-default")
@Namespace("/customer")
@Scope("prototype")
public class CustomerAction extends ActionSupport implements ModelDriven<Customer> {
private Customer customer = new Customer();
@Autowired
private CustomerService customerService; private List<Customer> customers; public void setCustomers(List<Customer> customers) {
this.customers = customers;
} public List<Customer> getCustomers() {
return customers;
} public Customer getModel() {
return customer;
}
//进入添加页面
@Action(value="addCustomerUI",results={@Result(name="success",location="/jsp/customer/add.jsp")})
public String addCustomerUI(){
return this.SUCCESS;
}
//进入列表页面
@Action(value="getAllCustomer",results={@Result(name="success",location="/jsp/customer/list.jsp")})
public String getAllCustomer(){
customers = customerService.getAllCustomer();
return this.SUCCESS;
}
}

  我们的CustomerServiceImpl

package com.itheima.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.itheima.dao.CustomerDao;
import com.itheima.entity.Customer;
import com.itheima.service.CustomerService;
@Service
@Transactional
public class CustomerServiceImpl implements CustomerService{
@Autowired
private CustomerDao customerDao; public void addCustomer(Customer customer) {
customerDao.save(customer);
}
@Transactional(propagation=Propagation.SUPPORTS,readOnly=true)
public List<Customer> getAllCustomer() {
return customerDao.find();
}
}

  我们的CustomerDaoImpl

package com.itheima.dao.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository; import com.itheima.dao.CustomerDao;
import com.itheima.entity.Customer;
@Repository
public class CustomerDaoImpl implements CustomerDao {
@Autowired
private HibernateTemplate hibernateTemplate; public void save(Customer customer) {
hibernateTemplate.save(customer);
} public List<Customer> find() {
return (List<Customer>) hibernateTemplate.find("from Customer");
}
}

  这样的话,我们的注解结合xml的形式就可以了,下次我们把这个项目改成maven项目

ssh整合之七注解结合xml形式的更多相关文章

  1. SSH整合开发的web.xml配置

    <?xml version="1.0" encoding="UTF-8"?><web-app version="2.5" ...

  2. 在myeclipse中maven项目关于ssh整合时通过pom.xml导入依赖是pom.xml头部会报错

    错误如下 ArtifactTransferException: Failure to transfer org.springframework:spring-jdbc:jar:3.0.5.RELEAS ...

  3. Hibernate 注解时 hibernate.hbm.xml的配置方法 以及与SSH整合里的配置方式

    ①纯Hibernate开发: 当你在Bean中写入注解后,需要告诉hibernate哪些类使用了注解. 方法是在hibernate.hbm.xml文件中配置 <!DOCTYPE hibernat ...

  4. 第五章 企业项目开发--mybatis注解与xml并用

    本章的代码建立在第四章<Java框架整合--切分配置文件>的项目代码之上,链接如下: http://www.cnblogs.com/java-zhao/p/5118184.html 在实际 ...

  5. SSH整合总结(xml与注解)

    本人自己进行的SSH整合,中间遇到不少问题,特此做些总结,仅供参考. 一.使用XML配置: SSH版本 Struts-2.3.31 Spring-4.3.5 Hibernate-4.2.21 引入ja ...

  6. SSH整合之全注解

    SSH整合之全注解 使用注解配置,需要我们额外引入以下jar包

  7. springboot整合redis(注解形式)

    springboot整合redis(注解形式) 准备工作 springboot通常整合redis,采用的是RedisTemplate的形式,除了这种形式以外,还有另外一种形式去整合,即采用spring ...

  8. ssh整合随笔(注解方式,Spring 管理action)

    Web.xml<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi=" ...

  9. SSH整合,applicationContext.xml中配置hibernate映射文件问题

    今天在applicationContext.xml中配置sessionFactory时遇到了各种头疼的问题,现在总结一下: 1.<property name="mappingDirec ...

随机推荐

  1. 炫丽的倒计时效果Canvas绘图与动画基础

    前言 想要在自己做的网页中,加入canvas动画效果,但是发现模板各种调整不好,觉得还是要对canvas有所了解,才可以让自己的网页变得狂拽炫酷吊炸天! 一.绘制基础 1 <!DOCTYPE h ...

  2. Java设计模式(四)Builder建造者模式

    一.场景描述 建造者模式同工厂模式.抽象工厂模式一样,用于创建继承类对象. 工厂模式:http://www.cnblogs.com/mahongbiao/p/8618970.html 抽象工厂模式:h ...

  3. 关于在linux下安装git,以及在idea上将项目部署到码云上

    GIT 基于对linux感兴趣,并且也考虑到以后从事开发后也会用到linux,着实在闲余之际学学linux.最近在用VM虚拟机环境下学做一个项目,在git上卡了一点时间,但同时也收获  了不少,下面写 ...

  4. java项目和java-web项目中文件和文件夹的含义

    1. java项目 .project:是工程构建配置文件 .classpath:保存的是项目所用的外部引用包的路径 .settings:记录项目配置变化的记录文件夹 src:sourcefolder项 ...

  5. SQL server 2008 安装提示:属性不匹配

    问题 安装SQL server 2008提示属性不匹配 解决方案 确保C盘以及其子文件夹C:\Program Files\Microsoft SQL Server和C:\Program Files ( ...

  6. 笔记:Maven 插件及配置详解

    插件使用配置 用户能够自己选择将某个插件目标绑定到生命周期的某个阶段上,例如创建项目的源码JAR包,内置的插件绑定关系并没有涉及这个任务,mavent-source-plugin 可以帮助我们完成该任 ...

  7. 二分查找(binary search)java实现及时间复杂度

    概述 在一个已排序的数组seq中,使用二分查找v,假如这个数组的范围是[low...high],我们要的v就在这个范围里.查找的方法是拿low到high的正中间的值,我们假设是m,来跟v相比,如果m& ...

  8. 大数据 --> 大数据关键技术

    大数据关键技术 大数据环境下数据来源非常丰富且数据类型多样,存储和分析挖掘的数据量庞大,对数据展现的要求较高,并且很看重数据处理的高效性和可用性. 传统数据处理方法的不足 传统的数据采集来源单一,且存 ...

  9. PHP 设计模式阅读清单

    社区文章推荐 S.O.L.I.D 面向对象设计和编程(OOD&OOP)笔记 浅谈 Laravel 设计模式 PHP 完整实战 23 种设计模式 Laravel Dependency Injec ...

  10. Android Service基础

    Service Service 是一个组件,用来执行长时间的后台操作,不提供用户界面. 另一个应用组件可以启动一个Service,它将持续地在后台运行,即便是用户转移到另一个应用它也不会停止. 另外, ...