我在快速启动应用程序中使用了spring-data-mongodb 1.2.1-RELEASE。它工作正常, 我可以连接到mongo db我现在可以创建,更新和删除集合我想要将wicket-framework与此应用程序集成。
域类
@Document
public class Student
{
@Id
private String id;
private String firstName;
private int age;
public String getFirstName()
{
return firstName;
}
public void setFirstName(String firstName)
{
this.firstName = firstName;
}
public int getAge()
{
return age;
}
public void setAge(int age)
{
this.age = age;
}
}
学生存储库类
@Repository
public class StudentService
{
@Autowired
MongoTemplate mongoTemplate;
@Override
public void create(Student student)
{
mongoTemplate.insert(student);
}
@Override
public void update(Query query, Update update)
{
mongoTemplate.updateFirst(query, update, Student.class);
}
@Override
public List<Student> findAll()
{
List<Student> students = mongoTemplate.findAll(Student.class);
logger.debug("Student: {}", students);
return students;
}
@Override
public void delete(Student student)
{
mongoTemplate.remove(student);
}
@Override
public void deleteAll()
{
Query searchUserQuery = new Query(Criteria.where("age").gt(0));
mongoTemplate.remove(searchUserQuery, Student.class);
}
}
Mongo配置类
@Configuration
@EnableMongoRepositories
@ComponentScan(basePackageClasses = {MongoDBApp.class})
@PropertySource("classpath:application.properties")
public class MongoConfiguration extends AbstractMongoConfiguration
{
@Override
protected String getDatabaseName()
{
return "demo";
}
@Override
public Mongo mongo() throws Exception
{
return new Mongo("localhost", 27017);
}
@Override
protected String getMappingBasePackage()
{
return "mypackage";
}
}
Mongo Db主类
public class MongoDBApp
{
static final Logger logger = LoggerFactory.getLogger(MongoDBApp.class);
public static void main(String[] args)
{
logger.info("Demo application");
ApplicationContext context = new AnnotationConfigApplicationContext(MongoConfiguration.class);
StudentService studentService = context.getBean(StudentService.class);
Student student = new Student();
student.setFirstName("foo");
student.setAge(24);
studentService.create(student);
List<Student> students = studentService.findAll();
logger.info("No. of students: {}", students.size());
studentService.delete(student);
logger.info("Deleted student: {}", student);
}
}
pom.xml中的依赖项
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-mongodb</artifactId>
<version>1.2.1.RELEASE</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
对于这段代码,我想与wicket框架集成。
请帮我怎么做?
答案 0 :(得分:0)
如果你的Spring / MongoDb运行良好,你只需要将它与模块wicket-spring集成到Wicket中。您可以在此处找到此集成的示例:
http://wicketguide.comsysto.com/guide/chapter18.html#chapter18_2
源代码可在此处获取:
https://github.com/bitstorm/Wicket-tutorial-examples/tree/master/SpringInjectionExample
答案 1 :(得分:0)
谢谢Andrea del bence, 我在MyWicketApplication中做到了这一点
@Override
public void init()
{
super.init();
getComponentInstantiationListeners().add(new SpringComponentInjector(this, getSpringContext()));
// add your configuration here
}
public ApplicationContext getSpringContext()
{
return WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
}