jade-all
Jade,是Java应用程序访问Mysql数据库的一个小框架.
数据库访问流程图
,---------------. ,---. ,----. ,----------.
|JavaApplication| |DAO| |Jade| |DataServer|
`-------+-------' `-+-' `-+--' `----+-----'
| | | |
|<------------------>| | |
| | | |
| | 1.Implement jdade specification | |
| |-----------------------------------------------> |
| | | |
| | | 2.Commit sql request|
| | | -------------------->
| | | |
| | | 3.Return result |
| | | <- - - - - - - - - -
| | | |
| |4.To convert a database object to a Java object| |
| | according to the DAO specification | |
| |<- - - - - - - - - - - - - - - - - - - - - - - - |
,-------+-------. ,-+-. ,-+--. ,----+-----.
|JavaApplication| |DAO| |Jade| |DataServer|
`---------------' `---' `----' `----------'
工程依赖
- JDK1.7或更高版本
- Maven3.x
- MySql数据库服务器
简单调用示例
- 添加如下依赖:
<dependency> <groupId>com.github.leixuan6</groupId> <artifactId>jade-all</artifactId> <version>1.0.3</version> </dependency>
- 设置src/main/resource/db.properties文件
driverClassName=com.mysql.jdbc.Driver url=jdbc:mysql://127.0.0.1/jade?characterEncoding=UTF-8 username=root password=123456 maxTotal=1 maxIdle=1 maxWait=-1
- 应用层实现DAO规范
- 调用JadeBootstrap.start方法,传入的路径参数。Jade扫描这个路径下的DAO,Jade会注册这些DAO服务
- 具体参考org.jade.example目录下的例子
实现Jade的DAO规范
@DAO
public interface AccountDAO {
@SQL(val = "Select * from Account ", type = SQLType.SELECT)
Account query();
@SQL(val = "Select * from Account ", type = SQLType.SELECT)
List queryList();
@SQL(val="Select * from Account Where id = ?",type = SQLType.SELECT)
Account queryById(int id);
@SQL(val="Select * from Account Where id =? and name =?",type = SQLType.SELECT)
Account queryById2(int id, String name);
@SQL(val="Delete From Account where id = ?",type=SQLType.INSERT)
int deleteById(int id);
@SQL(val="Update Account set name = ? where id = ?",type = SQLType.UPDATE)
int updateById(int id,String name);
@SQL(val="Insert into Account()Values(?,?)",type = SQLType.INSERT)
int insert(int id, String name);
}
Java对象,实现getset方法
public class Account {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Account [id=" + id + ", name=" + name + "]";
}
}
测试增删改查
public class JadeTest {
public static void main(String[] args) {
JadeBootstrap.start("org.jade");
AccountDAO dao = JadeDAOService.getDao(AccountDAO.class);
dao.deleteById(1);
dao.insert(1, "test");
dao.query();
dao.queryById(1);
dao.queryById2(1, "test");
dao.updateById(1, "test2");
}
}