FastQuery 基于Java语言.他的使命是:简化Java操作数据层.做为一个开发者, 仅仅只需要设计DAO接口即可,其内部采用ASM动态生成实现,执行快. 因此,代码简洁而优雅.从而,大幅度提升开发效率.
FastQuery 主要特性如下:
- 设计优雅,配置简单,极易上手.
- 采用ASM动态生成字节码,因此支持编译前预处理,可最大限度减少运行期的错误.显著提升程序的强壮性.
- 支持安全查询,防止SQL注入.
- 支持与主流数据库连接池框架集成,如集成c3p0,dbcp等等
- 支持 @Query 查询,使用 @Condition,可实现动态 where 条件查询.
- 支持查询结果集以JSON类型返回
- 支持AOP,注入拦截器只需标识几个简单的注解,如: @Before , @After
运行环境要求
jdk1.8+
配置文件
jdbc-config.xml
用来配置支持jdbc. 注意:如果采用连接池,该配置文件可以不要.
<?xml version="1.0" encoding="UTF-8"?>
<jdbc-config>
<named-config name="xk_db">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="url">jdbc:mysql://192.168.1.1:3306/xk?user=xk&password=abc123</property>
</named-config>
<named-config name="shtest_db">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="databaseName">dbname</property>
<property name="user">username</property>
<property name="password">userpasswd</property>
<property name="portNumber">3306</property>
<property name="serverName">192.168.1.1</property>
</named-config>
</jdbc-config>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
c3p0-config.xml
支持c3p0配置,详情配置请参照c3p0官网的说明: http://www.mchange.com/projects/c3p0/.
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="xk-c3p0">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://192.168.1.1:3306/xk</property>
<property name="user">xk</property>
<property name="password">abc123</property>
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
</named-config>
</c3p0-config>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
fastquery.json
配置数据源的作用范围
[
{
"config": "c3p0",
"dataSourceName": "xk-c3p0",
"basePackages": [
"org.fastquery.example.StudentDBService"
]
},
{
"config" : "jdbc",
"dataSourceName": "shtest_db",
"basePackages": [
"org.fastquery.example.DataAcquireDbService"
]
}
]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
一个完整的入门例子
public class Student
{
private String no;
private String name;
private String sex;
private Integer age;
private String dept;
}
public interface StudentDBService extends QueryRepository {
@Query("select * from student")
JSONArray findAll();
@Query("select * from student")
Student[] find();
}
StudentDBService studentDBService = FQuery.getRepository(StudentDBService.class);
JSONArray jsonArray = studentDBService.findAll();
Student[] students = studentDBService.find();
带条件查询
@Query("select no as no,name,sex,age,dept from student s where s.sex=?2 and s.age > ?1")
Student[] find(Integer age,String sex);
@Query("select * from student s where s.sex=?1 and s.age > ?2")
JSONArray find(String sex,Integer age);
@Query("select * from student s where s.sex=?1 and s.age > ?2")
List<Map<String, Object>> findBy(String sex,Integer age);
动态条件查询
@Query("select * from Student #{#where} order by age desc")
@Condition(l="no",o=Operator.LIKE,r="?1")
@Condition(c=COperator.AND,l="name",o=Operator.LIKE,r="?2")
@Condition(c=COperator.AND,l="age",o=Operator.GT,r="?3",ignoreNull=false)
@Condition(c=COperator.OR,l="dept",o=Operator.IN,r="(?4,?5,?6)")
@Condition(c=COperator.AND,l="name",o={Operator.NOT,Operator.LIKE},r="?7")
@Condition(c=COperator.OR,l="age",o=Operator.BETWEEN,r="?8 and ?9")
Student[] findAllStudent(... args ...);
count
统计查询行数
@Query("select count(no) from student")
long count();
exists
判断是否存在
@Query("select * from student s where s.no=?1")
boolean exists(String no);
改操作
@Query("update student s set s.age=?3,s.name=?2 where s.no=?1")
@Modifying
int update(String no,String name,int age);
@Modifying
@Query("DELETE FROM `userinfo` WHERE id=?1")
boolean deleteUserinfoById(int id);
@Query("update student s set s.age=?2 where s.no=?1")
@Modifying
int update(String no,int age);
@Query("insert into student (no, name, sex, age, dept) values (?1, ?2, ?3, ?4, ?5)")
@Modifying(table="student",id="no")
Student addStudent(@Id String no,String name,String sex,int age,String dept);
@Modifying(id="id",table="userinfo")
@Query("insert into #{#table} (name,age) values (?1, ?2)")
Map<String, Object> addUserInfo(String name,Integer age);
@Modifying(id="id",table="userinfo")
@Query("insert into #{#table} (name,age) values (?1, ?2)")
JSONObject saveUserInfo2(String name,Integer age);
@Modifying(id="id",table="userinfo")
@Query("insert into #{#table} (name,age) values (?1, ?2)")
Primarykey saveUserInfo(String name,Integer age);
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
@Before拦截器
/**
* @author xixifeng (fastquery@126.com)
*/
public class MyBeforeFilter1 extends BeforeFilter<Repository> {
@Override
public void doFilter(Repository repository, Method method, Object[] args) {
}
}
@Before(MyBeforeFilter1.class)
@Before(MyBeforeFilter2.class)
@Before(MyBeforeFilter3.class)
public interface StudentDBService extends QueryRepository {
}
@After拦截器
/**
* @author xixifeng (fastquery@126.com)
*/
public class MyAfterFilter extends AfterFilter<Repository> {
@Override
public Object doFilter(Repository repository, Method method, Object[] args, Object returnVal) {
return returnVal;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
@After(MyAfterFilter.class)
@After(MyAfterFilter2.class)
public interface StudentDBService extends QueryRepository {
}
开源地址
http://www.oschina.net/p/fastquery
转自:http://blog.youkuaiyun.com/StellaAh/article/details/51382958