@Table(tableName = "user", primaryKey = "id")
public class User extends JbootModel<User> {
}
dao query
public class UserDao {
public static find User DAO = new User();
public User findById(String id){
return DAO.findById(id);
}
public List<User> findByNameAndAge(String name,int age){
return DAO.findListByColums(Columns.create().like("name","%"+name+"%").gt("age",age));
}
}
event example
send event
Jboot.sendEvent(actionStr, dataObj)
event listener
@EventConfig(action = {User.ACTION_ADD,User.ACTION_DELETE})
public class MyEventListener implements JbootEventListener {
public void onMessage(JbootEvent event){
if(event.getAction.equals(User.ACTION_ADD)){
System.out.println("new user add, user:"+event.getData);
}else if(event.getAction.equals(User.ACTION_DELETE)){
System.out.println("user deleted, user:"+event.getData);
}
}
}
jboot 1.0-alphpa1 发布,类似 springboot 的开源框架
jboot
jboot是一个基于jfinal、undertow开发的一个类似springboot的开源框架, 我们已经在正式的商业上线项目中使用。她集成了代码生成,微服务,MQ,RPC,监控等功能, 开发者使用及其简单。
maven dependency
controller example
new a controller
@UrlMapping(url="/") public class MyController extend JbootController{ public void index(){ renderText("hello jboot"); } }start
public class MyStarter{ public static void main(String [] args){ Jboot.run(args); } }visit: http://127.0.0.1:8088
mq example
config jboot.properties
server a sendMqMessage
server b message listener
Jboot.getMq().addMessageListener(new JbootmqMessageListener(){ @Override public void onMessage(String channel, Object obj) { System.out.println(obj); } }, channel);rpc example
config jboot.properties
define interface
public interface HelloService { public String hello(String name); }server a export serviceImpl
@JbootrpcService(export = HelloService.class) public class myHelloServiceImpl extends JbootService implements HelloService { public String hello(String name){ System.out.println("hello" + name); return "hello ok"; } }server b call
HelloService service = Jboot.service(HelloService.class); service.hello("michael");cache example
config jboot.properties
use cache
Jboot.getCache().put("cacheName", "key", "value");database access example
config jboot.properties
define model
@Table(tableName = "user", primaryKey = "id") public class User extends JbootModel<User> { }dao query
public class UserDao { public static find User DAO = new User(); public User findById(String id){ return DAO.findById(id); } public List<User> findByNameAndAge(String name,int age){ return DAO.findListByColums(Columns.create().like("name","%"+name+"%").gt("age",age)); } }event example
send event
event listener
@EventConfig(action = {User.ACTION_ADD,User.ACTION_DELETE}) public class MyEventListener implements JbootEventListener { public void onMessage(JbootEvent event){ if(event.getAction.equals(User.ACTION_ADD)){ System.out.println("new user add, user:"+event.getData); }else if(event.getAction.equals(User.ACTION_DELETE)){ System.out.println("user deleted, user:"+event.getData); } } }read config
config jboot.properties
define config model
@PropertieConfig(prefix = "jboot.myconfig") public class MyConfig { private String name; private String password; // getter and setter }get config model
code generator
public static void main(String[] args) { String modelPackage = "io.jboot.test"; String dbHost = "127.0.0.1"; String dbName = "yourDbName"; String dbUser = "root"; String dbPassword = ""; JbootModelGenerator.run(modelPackage, dbHost, dbName, dbUser, dbPassword); }build
config pom.xml
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.8</source> <target>1.8</target> </configuration> </plugin> <plugin> <groupId>org.codehaus.mojo</groupId> <artifactId>appassembler-maven-plugin</artifactId> <version>1.10</version> <configuration> <assembleDirectory>${project.build.directory}/app</assembleDirectory> <repositoryName>lib</repositoryName> <binFolder>bin</binFolder> <configurationDirectory>webRoot</configurationDirectory> <copyConfigurationDirectory>true</copyConfigurationDirectory> <configurationSourceDirectory>src/main/resources</configurationSourceDirectory> <repositoryLayout>flat</repositoryLayout> <encoding>UTF-8</encoding> <logsDirectory>logs</logsDirectory> <tempDirectory>tmp</tempDirectory> <programs> <program> <mainClass>io.jboot.Jboot</mainClass> <id>jboot</id> <platforms> <platform>windows</platform> <platform>unix</platform> </platforms> <jvmSettings> <extraArguments> <extraArgument>-server</extraArgument> <extraArgument>-Xmx2G</extraArgument> <extraArgument>-Xms2G</extraArgument> </extraArguments> </jvmSettings> </program> </programs> </configuration> </plugin> </plugins> </build>maven build
start app
start app and change config
use your properties replace jboot.properties
use jboot-dev.proerties replace jboot.properties and set jboot.server.port=8080