Springboot min -Solon 详解系列文章:
Springboot mini - Solon详解(一)- 快速入门
Springboot mini - Solon详解(二)- Solon的核心
Springboot mini - Solon详解(三)- Solon的web开发html
//资源路径说明(不用配置;也不能配置) resources/application.properties(或 application.yml) 为应用配置文件 resources/static/ 为静态文件根目标 resources/WEB-INF/view/ 为视图模板文件根目标(支持多视图共存) //调试模式: 启动参数添加:-debug=1
Solon 的默认静态资源的路径为:(这个没得改,也不让改;为了简化套路)java
resources/static/
在默放的处理规则下,全部请求,都会先执行静态文件代理。静态文件代理会检测是否存在静态文件,有则输出,没有则跳过处理。输出的静态文件会作304控制。mysql
Solon里全部的处理,都属于Handler。能够用handler 的模式写,也能够用controller的模式写(Action 也是 Handler)git
// handler模式(前置处理) // Solon.global().before("/hello/", ctx->{ if(ctx.param("name") == null){ ctx.setHandled(true); //若是没有name, 则终止处理 } }); // controller模式 // @Controller public class HelloInterceptor { //(申明前置处理) @Mapping(value = "/hello/" , before = true) public void handle(Context ctx, String name) { if(name == null){ ctx.setHandled(true); //若是没有name, 则终止处理 } } }
@Configuration public class Config{ @Inject("${classpath:user.yml}") private UserModel user; }
HiKariCP是数据库链接池的一个后起之秀,号称性能最好,能够完美地PK掉其余链接池。做者特别喜欢它。github
<dependency> <groupId>com.zaxxer</groupId> <artifactId>HikariCP</artifactId> <version>3.3.1</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.18</version> </dependency>
test.db1: schema: "rock" jdbcUrl: "jdbc:mysql://localdb:3306/rock?useUnicode=true&characterEncoding=utf8&autoReconnect=true&rewriteBatchedStatements=true" driverClassName: "com.mysql.cj.jdbc.Driver" username: "demo" password: "UL0hHlg0Ybq60xyb" maxLifetime: 1000000
建议这种操做,都安排在 @Configuration 配置类里执行。web
//注解模式 // @Configuration public class Config{ // 同时支持 name 和 类型 两种方式注入(注入时没有name,即为按类型注入) // @Bean(value = "db1", typed = true) pubblic DataSource dataSource(@Inject("${test.db1}") HikariDataSource ds){ return ds; } } //静态类模式 // //public class Config{ // pubblic static HikariDataSource dataSource = Solon.cfg().getBean("test.db1", HikariDataSource.class); //}
以后就能够经过@Inject注解获得这个数据源了。通常会改用增强注解对数据源进行自动转换;全部与solon对接的ORM框架皆采用这种方案。正则表达式
Wee3是和Solon同样轻巧的一个框架,配置起来天然是简单的。sql
在pom.xml中引用weed3扩展组件数据库
<dependency> <groupId>org.noear</groupId> <artifactId>weed3-solon-plugin</artifactId> </dependency>
刚才的Config配置类便可复用。先以单数据源场景演示:apache
//使用示例 @Controller public class DemoController{ //@Db 按类型注入 //或 @Db("db1") 按名字注入 //@Db是weed3在Solon里的扩展注解 //能够注入 Mapper, BaseMapper, DbContext // @Db BaseMapper<UserModel> userDao; @Mapping("/user/") pubblic UserModel geUser(long puid){ return userDao.selectById(puid); } }
在pom.xml中引用mybatis扩展组件
<dependency> <groupId>org.noear</groupId> <artifactId>mybatis-solon-plugin</artifactId> </dependency>
添加mybatis mappers及相关的属性配置
mybatis.db1: #db1 要与数据源的bean name 对上 typeAliases: #支持包名 或 类名(.class 结尾) - "webapp.model" mappers: #支持包名 或 类名(.class 结尾)或 xml(.xml结尾);配置的mappers 会 mapperScan并交由Ioc容器托管 - "webapp.dso.mapper.UserMapper.class"
刚才的Config配置类即也可复用
//使用示例 @Controller public class DemoController{ //@Db 是 mybatis-solon-plugin 里的扩展注解,可注入 SqlSessionFactory,SqlSession,Mapper // @Db UserMapper userDao; //UserMapper 已被 db1 自动 mapperScan 并已托管,也可用 @Inject 注入 @Mapping("/user/") pubblic UserModel geUser(long puid){ return userDao.geUser(puid); } }
Solon中推荐使用@Tran注解来申明和管理事务。
@Tran 支持多数据源事务,且使用方便
//使用示例 @Controller public class DemoController{ @Db //@Db("db1") 为多数据源模式 BaseMapper<UserModel> userDao; @Tran @Mapping("/user/add") pubblic Long addUser(UserModel user){ return userDao.insert(user, true); } }
@Controller public class DemoController{ @Db UserMapper userDao; //UserMapper 已被 db1 mapperScan并已托管,也可用 @Inject 注入 @Tran @Mapping("/user/add") pubblic Long addUser(UserModel user){ return userDao.addUser(user); } }
@Service public class UserService{ @Db("db1") //数据库1 UserMapper userDao; @Tran public void addUser(UserModel user){ userDao.insert(user); } } @Service public class AccountService{ @Db("db2") //数据库2 AccountMapper accountDao; @Tran public void addAccount(UserModel user){ accountDao.insert(user); } } @Controller public class DemoController{ @Inject AccountService accountService; @Inject UserService userService; @Tran @Mapping("/user/add") pubblic Long geUser(UserModel user){ Long puid = userService.addUser(user); //会执行db1事务 accountService.addAccount(user); //会执行db2事务 return puid; } }
solon 的jsp支持,是基于视图模板的定位去处理的。根据启动器组件的不一样,配置略有不一样:
<!-- 添加 solon web 开发包 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon-web</artifactId> <type>pom</type> <exclusions> <!-- 排除默认的 jlhttp 启动器 --> <exclusion> <groupId>org.noear</groupId> <artifactId>solon.boot.jlhttp</artifactId> </exclusion> </exclusions> </dependency> <!-- 添加 jetty 或 undertow 启动器 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon.boot.jetty</artifactId> </dependency> <!-- 添加 jetty 或 undertow jsp 扩展支持包 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon.extend.jetty.jsp</artifactId> <type>pom</type> </dependency> <!-- 添加 jsp 视图引擎 --> <dependency> <groupId>org.noear</groupId> <artifactId>solon.view.jsp</artifactId> </dependency>
控制器,只有一个注解。会自动经过不一样的返回值作不一样的处理
@Controller public class DemoController{ @Mapping("/test1/") public void test1(){ //没返回 } @Mapping("/test2/") public String test2(){ return "返回字符串并输出"; } @Mapping("/test3/") public UseModel test3(){ return new UseModel(2, "noear"); //返回个模型,默认会渲染为json格式输出 } @Mapping("/test4/") public ModelAndView test4(){ return new ModelAndView("view path", map); //返回模型与视图,会被视图引擎渲染后再输出,默认是html格式 } }
默认只须要设定value值便可,method默认为MethodType.HTTP,即接收全部的http方法请求。
@Mapping("/user/")
freemaerker 视图
<!doctype html> <html> <head> <meta charset="UTF-8"> <title>${title}</title> </head> <body> <div> ${message} </div> </body> </html>
控制器
@Controller public class HelloworldController { @Mapping("/helloworld") public Object helloworld(){ ModelAndView vm = new ModelAndView("helloworld.ftl"); vm.put("title","demo"); vm.put("message","hello world!"); return vm; } }
//调试模式: 启动参数添加:-deubg=1 或 --deubg=1
Solon校验的是Context上的参数(即http传入的参数),是在Action参数注入以前的预处理。这与Spring验证框架区别是很大的。
@Valid //为控制器开启校验能力;也能够作用在一个基类上 @Controller public class ValidationController { @NoRepeatSubmit @NotNull({"name", "icon", "mobile"}) @Mapping("/valid") public void test(String name, String icon, @Pattern("13\\d{9}") String mobile) { } }
下面是更多的校验注解,能够研究一下:
注解 | 做用范围 | 说明 |
---|---|---|
Date | 参数 | 校验注解的参数值为日期格式 |
DecimalMax(value) | 参数 | 校验注解的参数值小于等于@ DecimalMax指定的value值 |
DecimalMin(value) | 参数 | 校验注解的参数值大于等于@ DecimalMin指定的value值 |
参数 | 校验注解的参数值为电子邮箱格式 | |
Length(min, max) | 参数 | 校验注解的参数值长度在min和max区间内 |
Max(value) | 参数 | 校验注解的参数值小于等于@Max指定的value值 |
Min(value) | 参数 | 校验注解的参数值大于等于@Min指定的value值 |
NoRepeatSubmit | 控制器 或 动做 | 校验本次请求没有重复 |
NotBlank | 动做 或 参数 | 校验注解的参数值不是空白 |
NotEmpty | 动做 或 参数 | 校验注解的参数值不是空 |
NotNull | 动做 或 参数 | 校验注解的参数值不是null |
NotZero | 动做 或 参数 | 校验注解的参数值不是0 |
Null | 动做 或 参数 | 校验注解的参数值是null |
Numeric | 动做 或 参数 | 校验注解的参数值为数字格式 |
Pattern(value) | 参数 | 校验注解的参数值与指定的正则表达式匹配 |
Whitelist | 控制器 或 动做 | 校验本次请求在白名单范围内 |
Solon.start(source, args) .onError(err->err.printStackTrace()); //或者记录到日志系统
Solon 的项目必须开启编译参数:-parameters
<build> <finalName>${project.name}</finalName> <plugins> <!-- 配置编译插件 --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <compilerArgument>-parameters</compilerArgument> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <!-- 配置打包插件(设置主类,并打包成胖包) --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <version>3.3.0</version> <configuration> <finalName>${project.name}</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptorRefs> <descriptorRef>jar-with-dependencies</descriptorRef> </descriptorRefs> <archive> <manifest> <mainClass>webapp.DemoApp</mainClass> </manifest> </archive> </configuration> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
java -jar DemoApp.jar
便可启动