目前能够通过简单的配置就可以使用的guice 模块,我所知的是这两个:guice-persis和warp-persist。
warp-persist支持的持久化框架相对多一些,包括hibernate/jpa/db4o等,并且其Dynamic Finder非常有特色,但是这个项目似乎已经停了,并且其相关的特点由guice-persist继承了(这两个persist框架的开发者好像与sitebricks的开发者是同一人?),只是支持的持久化持久化框架只有jpa。
虽然warp-persist功能更丰富,文档也要完善一些,而guice-persist的文档基本上可以说很简单,功能也比较少,但为了避免以后重复学习,我还是选择了guice-persist。
那么,先来说一下在sitebricks中的相关配置。需要了解sitebricks本身的配置的,请猛击这里和这里。
1、我建立了两个类来配置sitebricks: BricksListener.java和BricksModule.java:
BricksListener.java:
public class BricksListener extends GuiceServletContextListener {
protected Injector getInjector() {
System.out.println("\n>>>[BIRCKS] Sitebricks web application demo started.");
return Guice.createInjector(new BricksModule());
}
}
BricksModule.java:
public class BricksModule extends SitebricksModule {
@Override
protected void configureSitebricks() {
install(new JpaPersistModule("myFirstJpaUnit").addFinder(UserFinder.class));//这里加入Guice-persis的JpaPersistModule
bind(FlashCache.class).to(HttpSessionFlashCache.class).in(
Scopes.SINGLETON);
at("static/default.css").export("bricks.css");
at("/").show(Home.class);
at("/flow").show(Flow.class);
at("/hello").serve(Hello.class);
embed(NewCard.class).as("Card");
}
//下面是实现jpa持久化filter的配置
@Override
protected SitebricksServletModule servletModule() {
return new SitebricksServletModule() {
@Override
protected void configurePreFilters() {
filter("/*").through(PersistFilter.class);
}
};
}
}
2、不要忘了在pom.xml加上guice-persist的依赖和建立persistence.xml。
pom.xml:
<dependency>
<groupId>com.google.inject.extensions</groupId>
<artifactId>guice-persist</artifactId>
<version>${guice.version}</version>
</dependency>
<!--我使用的测试数据库及jpa2实现是objectdb-->
<dependency>
<groupId>com.objectdb</groupId>
<artifactId>objectdb</artifactId>
<version>${objectdb.version}</version>
</dependency>
persistence.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<!-- A JPA Persistence Unit -->
<persistence-unit name="myFirstJpaUnit"
transaction-type="RESOURCE_LOCAL">
<provider>com.objectdb.jpa.Provider</provider>
<!-- JPA entities must be registered here -->
<class>org.northstar.bricks.domain.User</class>
<exclude-unlisted-classes>true</exclude-unlisted-classes>
<properties>
<property name="javax.persistence.jdbc.url"
value="my.odb"/>
<property name="javax.persistence.jdbc.user" value="admin" />
<property name="javax.persistence.jdbc.password" value="admin" />
</properties>
</persistence-unit>
</persistence>
我用的objectdb数据库,以内嵌的方式运行。
3、注意persistence-unit在配置中的一致性,比如我用的是"myFirstJpaUnit",那么在sitebricks的配置中是这样的(见上面的java代码),并且这一句必须在其他语句之前,尤其是页面显示和与数据库操作的绑定语句之前 :
install(new JpaPersistModule("myFirstJpaUnit"));
在persistence.xml是这样配置的:
<persistence-unit name="myFirstJpaUnit"
transaction-type="RESOURCE_LOCAL">
4、开始应用。
建立一个Entity,比如:User.java:
@Entity
public class User implements Serializable{
/**
*
*/
private static final long serialVersionUID = 7158126725092237523L;
@Id
@GeneratedValue
private Long id;
private String name;
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
5、建立一个接口Finder, 比如:UserFinder。这里有一个地方需要注意:必须通过returnAs指定取得结果集的类型,如果结果是唯一的则可以不用指定,其默认返回类型是Collection。
UserFinder.java:
public interface UserFinder {
@Finder(query="select u from User u", returnAs=ArrayList.class)
ArrayList<User> listAll();
}
注意要在sitebricks配置中注册,即addFinder(见上面完整的代码):
install(new JpaPersistModule("myFirstJpaUnit").addFinder(UserDao.class));
当然,需要创建数据库,那么来个简单的:
UserDao.java:
public class UserDao {
@Inject
private EntityManager em;
@Transactional
public void createNewUser() {
User user = new User();
user.setName("test");
user.setPassword("123456");
em.persist(user);
}
}
页面类Home.java:
@Decorated
public class Home extends Decorator {
private List<User> users;
@Inject
private UserDao dao;
@Inject
private UserFinder finder;
@Get
public void get() {
// Load the list of cards in this HTTP get handler...
dao.createNewUser();
users = finder.listAll();
}
public List<User> getUsers(){
return users;
}
@Override
public String getPageTitle() {
return "Home Page";
}
}
页面模板Home.html:
<!DOCTYPE HTML>
<html>
<head>
<title>bricks - Home page</title>
<meta charset="UTF-8" />
</head>
<body>
<div>
<ol>
@Repeat(items=users, var="user")
<li>${user.name}</li>
</ol>
</div>
</body>
</html>
分享到:
相关推荐
赠送jar包:guice-assistedinject-3.0.jar; 赠送原API文档:guice-assistedinject-3.0-javadoc.jar; 赠送源代码:guice-assistedinject-3.0-sources.jar; 赠送Maven依赖信息文件:guice-assistedinject-3.0.pom;...
赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)版...
sisu-guice-2.1.7-noaop.jar
赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...
赠送jar包:guice-assistedinject-3.0.jar; 赠送原API文档:guice-assistedinject-3.0-javadoc.jar; 赠送源代码:guice-assistedinject-3.0-sources.jar; 赠送Maven依赖信息文件:guice-assistedinject-3.0.pom;...
赠送jar包:guice-4.0.jar; 赠送原API文档:guice-4.0-javadoc.jar; 赠送源代码:guice-4.0-sources.jar; 赠送Maven依赖信息文件:guice-4.0.pom; 包含翻译后的API文档:guice-4.0-javadoc-API文档-中文(简体)-...
Guice-3.0是Guice的一个版本,包含了核心库guice-3.0.jar,以及与Spring和Struts2集成的扩展库guice-spring-3.0.jar和guice-struts2-plugin-3.0.jar。 1. **Guice核心概念**: - **依赖注入**:Guice的核心机制,...
guice-spring-1.0.jar
赠送jar包:guice-multibindings-3.0.jar; 赠送原API文档:guice-multibindings-3.0-javadoc.jar; 赠送源代码:guice-multibindings-3.0-sources.jar; 赠送Maven依赖信息文件:guice-multibindings-3.0.pom; ...
sisu-guice-3.1.3-no_aop.jar
赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)版...
guice-1.0.jar guice-1.0.jar
赠送jar包:guice-3.0.jar; 赠送原API文档:guice-3.0-javadoc.jar; 赠送源代码:guice-3.0-sources.jar; 赠送Maven依赖信息文件:guice-3.0.pom; 包含翻译后的API文档:guice-3.0-javadoc-API文档-中文(简体)-...
guice-servlet-1.0.jar
赠送jar包:guice-servlet-4.0.jar; 赠送原API文档:guice-servlet-4.0-javadoc.jar; 赠送源代码:guice-servlet-4.0-sources.jar; 赠送Maven依赖信息文件:guice-servlet-4.0.pom; 包含翻译后的API文档:guice-...
guice-servlet-3.0.jar
赠送jar包:guice-servlet-4.0.jar; 赠送原API文档:guice-servlet-4.0-javadoc.jar; 赠送源代码:guice-servlet-4.0-sources.jar; 赠送Maven依赖信息文件:guice-servlet-4.0.pom; 包含翻译后的API文档:guice-...
赠送jar包:shiro-guice-1.4.0.jar; 赠送原API文档:shiro-guice-1.4.0-javadoc.jar; 赠送源代码:shiro-guice-1.4.0-sources.jar; 赠送Maven依赖信息文件:shiro-guice-1.4.0.pom; 包含翻译后的API文档:shiro-...
赠送jar包:jersey-guice-1.19.jar; 赠送原API文档:jersey-guice-1.19-javadoc.jar; 赠送源代码:jersey-guice-1.19-sources.jar; 赠送Maven依赖信息文件:jersey-guice-1.19.pom; 包含翻译后的API文档:jersey...
guice-3.0.jar,用于reportNG的一个工具包。配合reportng-1.1.4.jar使用