angularjs实例

AngularJS +Java EE 7教程源码案例

时间:2015-06-23 14:07来源:网络整理 作者:KKWL 点击:
JavaEE 7教程 AngularJS +Java EE 7教程源码案例 这是一个简单的分页列表案例,提供REST服务。技术栈如下: 该案例源码下载地址:Github 后端:JavaEE7 实体模型代码如下: @Entity public class Per

JavaEE 7教程 AngularJS +Java EE 7教程源码案例

  这是一个简单的分页列表案例,提供REST服务。技术栈如下:

该案例源码下载地址:Github

后端:JavaEE7

实体模型代码如下:

@Entity

public class Person {

    @Id

    private Long id;

 

    private String name;

 

    private String description;

 

}

@Entity是JPA元注解,进行持久化数据库使用。持久化配置如下:

<?xml version="1.0" encoding="UTF-8"?>

<persistence version="2.1"

             xmlns=""

             xmlns:xsi=""

             xsi:schemaLocation=" ">

    <persistence-unit name="myPU" transaction-type="JTA">

        <properties>

            <property name="javax.persistence.schema-generation.database.action" value="drop-and-create"/>

            <property name="javax.persistence.schema-generation.create-source" value="script"/>

            <property name="javax.persistence.schema-generation.drop-source" value="script"/>

            <property name="javax.persistence.schema-generation.create-script-source" value="sql/create.sql"/>

            <property name="javax.persistence.schema-generation.drop-script-source" value="sql/drop.sql"/>

            <property name="javax.persistence.sql-load-script-source" value="sql/load.sql"/>

        </properties>

    </persistence-unit>

</persistence>

Java EE 7有两个新特性: 使用属性javax.persistence.schema-generation.*你可以标准方式运行SQL,如果你没有提供数据库源,它会绑定缺省的数据库。在这个案例中,我们使用内部Wildfly H2 数据库配置

下面是提供REST服务代码:

@Stateless

@ApplicationPath("/resources")

@Path("persons")

public class PersonResource extends Application {

    @PersistenceContext

    private EntityManager entityManager;

 

    private Integer countPersons() {

        Query query = entityManager.createQuery("SELECT COUNT(p.id) FROM Person p");

        return ((Long) query.getSingleResult()).intValue();

    }

 

    @SuppressWarnings("unchecked")

    private List<Person> findPersons(int startPosition, int maxResults, String sortFields, String sortDirections) {

        Query query = entityManager.createQuery("SELECT p FROM Person p ORDER BY " + sortFields + " " + sortDirections);

        query.setFirstResult(startPosition);

        query.setMaxResults(maxResults);

        return query.getResultList();

    }

 

    public PaginatedListWrapper<Person> findPersons(PaginatedListWrapper<Person> wrapper) {

        wrapper.setTotalResults(countPersons());

        int start = (wrapper.getCurrentPage() - 1) * wrapper.getPageSize();

        wrapper.setList(findPersons(start,

                                    wrapper.getPageSize(),

                                    wrapper.getSortFields(),

                                    wrapper.getSortDirections()));

        return wrapper;

    }

 

    @GET

    @Produces(MediaType.APPLICATION_JSON)

    public PaginatedListWrapper<Person> listPersons(@DefaultValue("1")

                                                    @QueryParam("page")

                                                    Integer page,

                                                    @DefaultValue("id")

                                                    @QueryParam("sortFields")

                                                    String sortFields,

                                                    @DefaultValue("asc")

                                                    @QueryParam("sortDirections")

                                                    String sortDirections) {

        PaginatedListWrapper<Person> paginatedListWrapper = new PaginatedListWrapper<>();

        paginatedListWrapper.setCurrentPage(page);

        paginatedListWrapper.setSortFields(sortFields);

        paginatedListWrapper.setSortDirections(sortDirections);

        paginatedListWrapper.setPageSize(5);

        return findPersons(paginatedListWrapper);

    }

}

 

 

------分隔线----------------------------