开发者

Basic Console App with Spring Roo

I understand that it is not really how Roo was intended to be used, but I was to knock up a quick demo with Roo to run within a console app.

I have created a basic app with the following Roo Script:

project --topLevelPackage com.xetius.maths
persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
entity --class ~.domain.Equation --testAutomatically
field number --fieldName firstNum --type java.lang.Integer --notNull
field number --fieldName secondNum --type java.lang.Integer --notNull
field string --fieldName operator --notNull
field number --fieldName answer --type java.lang.Integer

Next I want to add a basic console by adding the following class

package com.xetius.maths;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MathMain {
    public static void main(String[] args) {
        ApplicationContext appContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        System.out.println("Here");
    }
}

My plan was to pass in firstNum, operator and secondNum, add these to the DB, then calculate the answer, add that to the DB, then display the response. If the answer couldn't be calculate 开发者_如何学运维(with divide by 0 for example) then roll the transaction back.

This should be quite simple, and Im guessing that it is, however, I can't work out how to access the sessionFactory. Is this implicit in something else, or am I just doing something wrong?

Am I just not able to do this, or is there another way to do this. This is all for a demo to my bosses to show the advantages of Roo, but can't seem to get my head around this bit


After context is loaded, it is very simply

Equation eq = new Equation();
eq.setFirstNum(2);
eq.setSecondNum(2);
// and so on
eq.persist();

If you need to delete wrong entry you need to use

eq.remove();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜