示例代碼看這里 https://gitee.com/anyline/anyline-simple
Anyline的核心在于數(shù)據(jù)庫操作,只需在要項目中注入AnylneService即可快速實現(xiàn)數(shù)據(jù)庫操作。
@Autowired
@Qualifier("anyline.service")
protected AnylineService service;
如果熟悉maven和Spring MVC(或Struts2),那非常簡單.如果什么基礎(chǔ)也沒有請參考:從0開始搶建環(huán)境(spring mvc) 或 從0開始搶建環(huán)境(struts2)
如果是基于SpringBoot請參考:SpringBoot環(huán)境 或者 SpringBoot環(huán)境(前后端分離)
以下springmvc,maven環(huán)境為例:
<!-- Spring MVC -->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-mvc</artifactId>
<version>${anyline.version}</version>
</dependency>
<!-- MySQL 根據(jù)實際情況 -->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-jdbc-mysql</artifactId>
<version>${anyline.version}</version>
</dependency>
<!--掃描org.anyline包-->
<context:component-scan base-package="org.anyline"></context:component-scan>
<!--注冊一個springjdbc模板 根據(jù)實際情況注入數(shù)據(jù)源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds" />
</bean>
接下來主要有掌握兩點:如何操作數(shù)據(jù)庫及如何接收返回結(jié)果
AnylineService(配合AnylineDao)提供了常用的數(shù)據(jù)庫操作接口,其中insert,update,delete,execute比較簡單也容易理解而select操作相對靈活的多,靈活性主要體現(xiàn)在其參數(shù)ConfigStore的構(gòu)造方式上,通過ConfigStore可以實現(xiàn)非常復(fù)雜的查詢操作1public DataSet selects(String src, ConfigStore configs, String ... conditions);在實際開發(fā)過程中,通常是用BaseController繼承tAnylineControllerAnylineController中已經(jīng)注入AnylineService serive,并重載了大量config函數(shù)用來自動構(gòu)造ConfigStore
更詳細的操作參考:AnylineService 與condition()
insert,update,delete,execute返回結(jié)果只有成功失敗或影響行數(shù)select返回DataRow表示一行,selects返回DataSet<DataRow>表示多行DataRow/DataSet上附加了排序,求和,截取,清除空值,按列去重,最大最小值,交集合集差集,分組,行列轉(zhuǎn)換,類SQL篩選(like,eq,in,less,between...),JSON,XML格式轉(zhuǎn)換等常用計算函數(shù)詳細參考:數(shù)據(jù)結(jié)構(gòu):DataRow、數(shù)據(jù)結(jié)構(gòu):DataSet
<!-- Spring MVC -->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-mvc</artifactId>
<version>${anyline.version}</version>
</dependency>
<!-- MySQL 根據(jù)實際情況 -->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-jdbc-mysql</artifactId>
<version>${anyline.version}</version>
</dependency>
<!--掃描org.anyline包-->
<context:component-scan base-package="org.anyline"></context:component-scan>
<!--注冊一個springjdbc模板 根據(jù)實際情況注入數(shù)據(jù)源-->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds" />
</bean>
<!-- Spring MVC -->
<dependency>
<groupId>org.anyboot</groupId>
<artifactId>anyboot-mvc</artifactId>
<version>${anyboot.version}</version>
</dependency>
<!--如果是前后端分離的項目可以把org.anyboot.anyboot-mvc換成org.anyline.anyline-mvc這樣會少一個依賴-->
<!-- MySQL 根據(jù)實際情況 -->
<dependency>
<groupId>org.anyboot</groupId>
<artifactId>anyboot-jdbc-mysql</artifactId>
<version>${anyboot.version}</version>
</dependency>
默認數(shù)據(jù)源配置
spring.datasource.driver=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/al_api?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root
如果需要操作多數(shù)據(jù)源,添加以下配置
spring.datasource.list=sso,cms #先定義數(shù)據(jù)源列表
#依次配置列表中指定的數(shù)據(jù)源
spring.datasource.sso.driver=com.mysql.cj.jdbc.Driver
spring.datasource.sso.url=jdbc:mysql://127.0.0.1:3306/al_sso?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC
spring.datasource.sso.username=root
spring.datasource.sso.password=root
spring.datasource.cms.driver=com.mysql.cj.jdbc.Driver
spring.datasource.cms.url=jdbc:mysql://127.0.0.1:3306/al_cms?useUnicode=true&characterEncoding=UTF8&useSSL=false&serverTimezone=UTC
spring.datasource.cms.username=root
spring.datasource.cms.password=root
配置類中掃描org.anylie與org.anyboot包
import org.anyboot.config.db.ds.DynamicDataSourceRegister;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Import;
@SpringBootApplication
@ComponentScan(basePackages = { "org.anyline", "org.anyboot" })
@Import(DynamicDataSourceRegister.class)
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
}
}
其余操作于Srping MVC環(huán)境完全一致
前后分離的項目不需要依賴anyboot(anyboot中的包主要用來支持JSP)
<!-- Spring MVC -->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-mvc</artifactId>
<version>${anyline.version}</version>
</dependency>
<!-- MySQL 根據(jù)實際情況 -->
<dependency>
<groupId>org.anyline</groupId>
<artifactId>anyline-jdbc-mysql</artifactId>
<version>${anyline.version}</version>
</dependency>
@Autowired
private org.anyline.service.AnylineService service
或者繼承
org.anyline.controller.impl.AnylineController
其余操作于Srping MVC環(huán)境完全一致
更多建議: