论坛首页 Java企业应用论坛

Aesop —— 一个用于构建DAL的框架

浏览 3907 次
该帖已经被评为良好帖
作者 正文
   发表时间:2009-09-27   最后修改:2009-10-05

Aesop 是一个用于构建DAL的框架. 在介绍Aesop之前,让我们来看一个简单的例子:

EntityManager<Author> entityManager =
com.aesop.core.EntityManagerFactory.getInstance().getEntityManager(D_N);
Author author = new Author();
author.setFirstName("firstName");
author.setLastName("lastName");
Oid oid = entityManager.add(author);
Author dbAuthor = entityManager.get(oid);
System.out.println(dbAuthor);
dbAuthor.setFirstName("updatedFirstName");
entityManager.update(dbAuthor);
dbAuthor = entityManager.get(oid);
System.out.println(dbAuthor);
Condition cond = Q.eq(Author.FIRST_NAME, "updatedFirstName");
ResultSet resultSet = entityManager.query(Author._, null, cond);
System.out.println(resultList);
entityManager.delete(dbAuthor);
dbAuthor = entityManager.get(oid);
System.out.println(dbAuthor);


或者Active Record模式:

Author author = new Author();
author.setFirstName("firstName");
author.setLastName("lastName");
author.store();
Author dbAuthor = Author.load(author.getId());
System.out.println(dbAuthor);
dbAuthor.setFirstName("updatedFirstName");
Book book = new Book();
book.setName("bookName");
author.add(book, true);
dbAuthor.update();
dbAuthor = Author.load(author.getId());
System.out.println(dbAuthor);
List<Author> authorList =
Author.find(null, eq(Author.FIRST_NAME, "updatedFirstName"));
System.out.println(authorList);
author.remove(book, true);
author.delete();






更多,请参见:Aesop 手册


http://groups.google.com/group/aesop-dal

 

  • 大小: 25.8 KB
   发表时间:2009-09-28   最后修改:2009-09-28
框架太多,大家都审美疲劳了,建议加上和hibernate,ibatis等的比较。这样大家才会注意它。
0 请登录后投票
   发表时间:2009-10-04   最后修改:2009-10-04
几乎所有的的框架都有一个或几个配置文件,例如配置connection,cache或其他的一些信息,另外,大部框架还有一个table到class的mapping的配置xml。让我们就直接从创建EntityManager或session开始。

一个Ibatis的CRUD例子:
引用这个帖子作者的一个例子:http://www.iteye.com/topic/443910?page=2(敬请不要介意)

AuthorMapper.class
public interface AuthorMapper {  
  
    Author selectAuthor(long id);  
  
    void insertAuthor(Author author);  
  
    void updateAuthorIfNecessary(Author author); 
  
    void deleteAuthor(long id);  
  
}  



AuthorMapper.xml
<mapper namespace="domain.blog.mappers.AuthorMapper">  
  
  <parameterMap id="selectAuthor" type="domain.blog.Author">  
    <parameter property="id"/>  
  </parameterMap>  
   <!-- id 对应接口中的方法 -->  
  <resultMap id="selectAuthor" type="domain.blog.Author">  
    <id column="id" property="id"/>  
    <result property="username" column="username"/>  
    <result property="password" column="password"/>  
    <result property="email" column="email"/>  
    <result property="bio" column="bio"/>  
    <result property="favouriteSection" column="favourite_section"/>  
  </resultMap>  
  
  <select id="selectAuthor"  
          parameterMap="selectAuthor"  
          resultMap="selectAuthor">  
    select id, username, password, email, bio, favourite_section  
    from author where id = ?  
  </select>  
  
  <insert id="insertAuthor"  
          parameterType="domain.blog.Author">  
    insert into Author (id,username,password,email,bio)  
    values (#{id},#{username},#{password},#{email},#{bio})  
  </insert>  
  
  <update id="updateAuthorIfNecessary"  
          parameterType="domain.blog.Author">  
    update Author  
      <set>  
        <if test="username != null">username=#{username},</if>  
        <if test="password != null">password=#{password},</if>  
        <if test="email != null">email=#{email},</if>  
        <if test="bio != null">bio=#{bio}</if>  
      </set>  
    where id=#{id}  
  </update>
  
  <delete id="deleteAuthor">  
    delete from Author where id=#{id}  
  </delete>
  
</mapper>  


Client:
public class SimpleSqlSessionTest {  
    private static SqlSessionFactory sqlMapper;  
  
    @Before  
    public void setUp() throws Exception {  
        final String resource = "org/apache/ibatis/builder/MapperConfig.xml";  
        final Reader reader = Resources.getResourceAsReader(resource);  
        sqlMapper = new SqlSessionFactoryBuilder().build(reader);  
    }  
  
    @Test  
    public void shouldSelectOneAuthor() throws Exception {  
        SqlSession session = sqlMapper.openSession();  
        try {  
            Author author = (Author) session.selectOne( "domain.blog.mappers.AuthorMapper.selectAuthor",  
                    new Author(101));  
            assertEquals(101, author.getId());  
            assertEquals(Section.NEWS, author.getFavouriteSection());  
        } finally {  
            session.close();  
        }  
    }  
  
    @Test  
    public void shouldUpdateAuthorIfNecessary() throws Exception {  
        SqlSession session = sqlMapper.openSession();  
        Author original;  
        Author updated;  
        try {  
            original = (Author) session.selectOne( "domain.blog.mappers.AuthorMapper.selectAuthor", 101);  
            original.setEmail("new@email.com");  
            original.setBio(null);  
            session.update( "domain.blog.mappers.AuthorMapper.updateAuthorIfNecessary", original);  
  
            updated = (Author) session.selectOne( "domain.blog.mappers.AuthorMapper.selectAuthor", 101);  
            assertEquals(original.getEmail(), updated.getEmail());  
            session.commit();  
        } finally {  
            session.close();  
        }  
          
        try {  
            session = sqlMapper.openSession();  
            updated = (Author) session.selectOne( "domain.blog.mappers.AuthorMapper.selectAuthor", 101);  
            assertEquals(original.getEmail(), updated.getEmail());  
        } finally {  
            session.close();  
        }  
    }  

    @Test  
    public void shouldDeleteOneAuthor() throws Exception {  
        SqlSession session = sqlMapper.openSession();  
        try {  
            session.delete( "domain.blog.mappers.AuthorMapper.deleteAuthor",  
                    101);  
            Author author = (Author) session.selectOne( "domain.blog.mappers.AuthorMapper.selectAuthor", 101);  
            assertNull(author);  
        } finally {  
            session.close();  
        }  
    }  
}  



再来看一个Aesop的CRUD的例子:
public class SimpleSqlSessionTest {    
    private EntityManager<Author> entityManager;    
    
    @Before    
    public void setUp() throws Exception {    
        entityManager = EntityManagerFactory.getInstance().getEntityManager(domainName);   
    }    
    
    @Test    
    public void shouldSelectOneAuthor() throws Exception {
        Oid oid = Seid.valueOf(Author.ID, 101);
        Author author = entityManager.get(oid);
        assertEquals(101, author.getId());    
        assertEquals(Section.NEWS, author.getFavouriteSection());
    }    
    
    @Test    
    public void shouldUpdateAuthorIfNecessary() throws Exception {    
        Oid oid = Seid.valueOf(Author.ID, 101);
        Author original = entityManager.get(oid);
        String newEmail = "new@email.com";
        original.setEmail(newEmail);
        entityManager.update(original);

        Author updated = entityManger.get(oid);
        assertEquals(newEmail, updated.getEmail());  
    }    
  
    @Test    
    public void shouldDeleteOneAuthor() throws Exception {
        Oid oid = Seid.valueOf(Author.ID, 101);
        entityManager.delete(oid);
        Author author = entityManager.get(oid);
        assertNull(author);
    }    
}

  
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics