如何使用APPFUSE 2来开发

 
Create POJO and Gen DB table
 
1.      Create a POJO class Person (in the src/main/java/**/model 目录下)
 
package com.mycompany.model;
import org.appfuse.model.BaseObject;
import javax.persistence.*;
 
@Entity
public class Person extends BaseObject {
    private Long id;
    private String firstName;
    private String lastName;
 
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Long getId() {  
        return id;
    }
 
    @Column(name="first_name", length=50)   
    public String getFirstName() {
        return firstName;
    }
 
    @Column(name="last_name", length=50)
    public String getLastName() {
        return lastName;
}
。。。。。。
}
 
POJO扩展BaseObject是可选的,但建议扩展它,因为它会强制你必须在POJO里写toString(), equals() and hashCode() 方法。
如果你计划把POJO放进user's session里,或在web service里使用它,那么POJO还应该实现java.io.Serializable接口
 
 
2.      添加下列代码到 src/main/resources/hibernate.cfg.xml 文件
<mapping class="com.mycompany.model.Person"/>
       保存之后,执行命令: mvn test-compile hibernate3:hbm2ddl(前提是database要打开),将会在database里看到添加了Person Table
 
Create Hibernate DAO
 
1.      创建 DAO 的单元测试 PersonDaoTest(in your src/test/java/**/dao directory)
package com.mycompany.dao;
 
import org.appfuse.dao.BaseDaoTestCase;
import org.springframework.dao.DataAccessException;
import com.mycompany.model.Person;
import java.util.List;
 
public class PersonDaoTest extends BaseDaoTestCase {
    private PersonDao personDao = null;
    public void setPersonDao(PersonDao personDao) {
        this.personDao = personDao;
    }
 
    public void testFindPersonByLastName() throws Exception {
        List<Person> people = personDao.findByLastName("Raible");
        assertTrue(people.size() > 0);
    }
 
    public void testAddAndRemovePerson() throws Exception {
        Person person = new Person();
        person.setFirstName("Country");
         person.setLastName("Bry");
        person = personDao.save(person);
        flush();
 
        person = personDao.get(person.getId());
        assertEquals("Country", person.getFirstName());
        assertNotNull(person.getId());
        log.debug("removing person...");
        personDao.remove(person.getId());
        flush();
             
              // 测试在 get removed person 时抛出 exception
         try {
            personDao.get(person.getId());
            fail("Person found in database");
        } catch (DataAccessException dae) {
            log.debug("Expected exception: " + dae.getMessage());
            assertNotNull(dae);
        }
    }
}
 
2.      DbUnit Maven Plugin 可以用来在测试运行前将数据插入数据库中 , 你只需将需加入的表或记录的信息放到文件 src/test/resources/sample-data.xml ,如:
    <table name='person'>
     <column>id</column>
     <column>first_name</column>
     <column>last_name</column>
     <row>
        <value>1</value>
        <value>Matt</value>
        <value>Raible</value>
     </row>
    </table>
 
3.      Step 1创建的 PersonDaoTest.java是无法编译的,因为还没有创建PersonDao ( 但要养成好习惯,先写单元测试!PersonDao接口及其实现类(in your src/main/java/**/dao directory))。因此我们 创建
 
PersonDao.java:
package com.mycompany.dao;
import com.mycompany.model.Person;
import java.util.List;
import org.appfuse.dao.GenericDao;
 
// 注:扩展 GenericDao 则包含了它的 CRUD 方法!!
public interface PersonDao extends GenericDao<Person, Long> {
    public List<Person> findByLastName(String lastName);
}
 
PersonDaoHibernate.java:
package com.mycompany.dao.hibernate;
 
import com.mycompany.model.Person;
import com.mycompany.dao.PersonDao;
import org.appfuse.dao.hibernate.GenericDaoHibernate;
import java.util.List;
 
public class PersonDaoHibernate extends GenericDaoHibernate<Person, Long> implements PersonDao {
    public PersonDaoHibernate() {
        super(Person.class);
    }
    public List<Person> findByLastName(String lastName) {
        return getHibernateTemplate().find("from Person where lastName=?", lastName);
    }
}
 
4.      src/ main / webapp / WEB-INF/applicationContext .xml 添加下列代码来定义 personDao bean
    <bean id="personDao" class="com.mycompany.dao.hibernate.PersonDaoHibernate">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean> 
 
5.      执行命令 mvn test -Dtest=PersonDaoTest 来进行单元测试
   注:执行命令 mvn test是运行所有单元测试
 
 
Create Service
 
在创建了DAO之后,下面的步骤是Manager Service class,它的作用是作为persistence (DAO) layer and the web layer 之间的桥梁,使presentation layer和database layer松耦合(例如i.e. for Swing apps and web services)。
 
Managers 就是用来放你的 business logic 的地方!
 
1.  创建 PersonManager interface in your src/main/java/**/service directory
package com.mycompany.service;
 
import com.mycompany.model.Person;
import java.util.List;
import org.appfuse.service.GenericManager;
 
public interface PersonManager extends GenericManager<Person, Long> {
    public List<Person> findByLastName(String lastName);
}
 
为什么要扩展 GenericManager??
这是因为 GenericManager 包含了 CRUD 的一些方法,你也可以不扩展它,自己写!
 
2.  Create manager 单元测试 PersonManagerImplTest
注意:该单元测试使用了 mock objects ,这是为了使你的 tests 不依赖 external dependencies ,也就是说你不需要创建一个 database 也可以 run these tests。这样做就使得在一个大的项目里,DAO和manager分工给不同的人来开发,那么开发manager的人甚至可以在dao impletement完成之前就可以写好manager的单元测试。
 
本例中使用的 mock objects jMock另外一个使用比较广泛的mock object libraryEasyMock
 
PersonManagerImplTest.java
package com.mycompany.service.impl;
。。。。
public class PersonManagerImplTest extends BaseManagerMockTestCase {
    private PersonManagerImpl manager = null;
    private Mock dao = null;
    private Person person = null;
 
    protected void setUp() throws Exception {
        dao = new Mock(PersonDao.class);
        manager = new PersonManagerImpl((PersonDao) dao.proxy());
    }
    protected void tearDown() throws Exception {
        manager = null;
}
 
    public void testGetPerson() {
        log.debug("testing getPerson");
 
        Long id = 7L;
        person = new Person();
 
        // set expected behavior on dao
              //!! 关键方法
        dao.expects(once()).method("get")
                .with(eq(id))
                .will(returnValue(person));
 
        Person result = manager.get(id);
        assertSame(person, result);
    }
 
    public void testGetPersons() {
        log.debug("testing getPersons");
 
        List people = new ArrayList();
 
        // set expected behavior on dao
        dao.expects(once()).method("getAll")
                .will(returnValue(people));
 
        List result = manager.getAll();
        assertSame(people, result);
    }
 
    public void testFindByLastName() {
        log.debug("testing findByLastName");
 
        List people = new ArrayList();
        String lastName = "Smith";
 
        // set expected behavior on dao
        dao.expects(once()).method("findByLastName")
                .with(eq(lastName))
                .will(returnValue(people));
 
        List result = manager.findByLastName(lastName);
        assertSame(people, result);
    }
 
    public void testSavePerson() {
        log.debug("testing savePerson");
 
        person = new Person();
 
        // set expected behavior on dao
        dao.expects(once()).method("save")
                .with(same(person))
                .will(returnValue(person));
 
        manager.save(person);
    }
 
    public void testRemovePerson() {
        log.debug("testing removePerson");
 
        Long id = 11L;
        person = new Person();
 
        // set expected behavior on dao
        dao.expects(once()).method("remove")
                .with(eq(id))
                .isVoid();
 
        manager.remove(id);
    }
}
 
3. Create Manager implement class PersonManagerImpl
package com.mycompany.service.impl;
。。。。
public class PersonManagerImpl extends GenericManagerImpl<Person, Long> implements PersonManager {
    PersonDao personDao;
 
    public PersonManagerImpl(PersonDao personDao) {
        super(personDao);
        this.personDao = personDao;
    }
 
    public List<Person> findByLastName(String lastName) {
        return personDao.findByLastName(lastName);
    }
}
 
4. src/ main / webapp / WEB-INF/applicationContext .xml 添加下列代码来定义 personDao bean
<bean id="personManager" class="com.mycompany.service.impl.PersonManagerImpl">
    <constructor-arg ref="personDao"/>
</bean>
 
5.  执行命令 mvn test -Dtest=PersonManagerImplTest 来进行单元测试
       注:执行命令 mvn test是运行所有单元测试
 
 
 
CSDN海神之光上传的代码均可运行,亲测可用,直接替换数据即可,适合小白; 1、代码压缩包内容 主函数:main.m; 调用函数:其他m文件;无需运行 运行结果效果图; 2、代码运行版本 Matlab 2019b或2023b;若运行有误,根据提示修改;若不会,私信博主; 3、运行操作步骤 步骤一:将所有文件放到Matlab的当前文件夹; 步骤二:双击打开main.m文件; 步骤三:点击运行,等程序运行完得到结果; 4、仿真咨询 如需其他服务,可私信博主或扫描博客文章底部QQ名片; 4.1 博客或资源的完整代码提供 4.2 期刊或参考文献复现 4.3 Matlab程序定制 4.4 科研合作 功率谱估计: 故障诊断分析: 雷达通信:雷达LFM、MIMO、成像、定位、干扰、检测、信号分析、脉冲压缩 滤波估计:SOC估计 目标定位:WSN定位、滤波跟踪、目标定位 生物电信号:肌电信号EMG、脑电信号EEG、心电信号ECG 通信系统:DOA估计、编码译码、变分模态分解、管道泄漏、滤波器、数字信号处理+传输+分析+去噪(CEEMDAN)、数字信号调制、误码率、信号估计、DTMF、信号检测识别融合、LEACH协议、信号检测、水声通信 1. EMD(经验模态分解,Empirical Mode Decomposition) 2. TVF-EMD(时变滤波的经验模态分解,Time-Varying Filtered Empirical Mode Decomposition) 3. EEMD(集成经验模态分解,Ensemble Empirical Mode Decomposition) 4. VMD(变分模态分解,Variational Mode Decomposition) 5. CEEMDAN(完全自适应噪声集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 6. LMD(局部均值分解,Local Mean Decomposition) 7. RLMD(鲁棒局部均值分解, Robust Local Mean Decomposition) 8. ITD(固有时间尺度分解,Intrinsic Time Decomposition) 9. SVMD(逐次变分模态分解,Sequential Variational Mode Decomposition) 10. ICEEMDAN(改进的完全自适应噪声集合经验模态分解,Improved Complementary Ensemble Empirical Mode Decomposition with Adaptive Noise) 11. FMD(特征模式分解,Feature Mode Decomposition) 12. REMD(鲁棒经验模态分解,Robust Empirical Mode Decomposition) 13. SGMD(辛几何模态分解,Spectral-Grouping-based Mode Decomposition) 14. RLMD(鲁棒局部均值分解,Robust Intrinsic Time Decomposition) 15. ESMD(极点对称模态分解, extreme-point symmetric mode decomposition) 16. CEEMD(互补集合经验模态分解,Complementary Ensemble Empirical Mode Decomposition) 17. SSA(奇异谱分析,Singular Spectrum Analysis) 18. SWD(群分解,Swarm Decomposition) 19. RPSEMD(再生相移正弦辅助经验模态分解,Regenerated Phase-shifted Sinusoids assisted Empirical Mode Decomposition) 20. EWT(经验小波变换,Empirical Wavelet Transform) 21. DWT(离散小波变换,Discraete wavelet transform) 22. TDD(时域分解,Time Domain Decomposition) 23. MODWT(最大重叠离散小波变换,Maximal Overlap Discrete Wavelet Transform) 24. MEMD(多元经验模态分解,Multivariate Empirical Mode Decomposition) 25. MVMD(多元变分模态分解,Multivariate Variational Mode Decomposition)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值