代码拉取完成,页面将自动刷新
FastBuilder是一个快速开发的ORM框架,支持Mysql和Oracle数据库, 在DAO层开发效率节约50%以上
FastBuilder目前支持的功能如下:
后续开发计划:
代码生成工具下载:http://git.oschina.net/wangwei123/FastBuilderCodeGen
快速开始:
第一步:在项目中加入FastBuilder源码:
第二步:在Spring配置文件中配置数据源:
<!-- 数据库配置 -->
<bean id="dataSource" class="com.jolbox.bonecp.BoneCPDataSource"
destroy-method="close">
<property name="driverClass" value="${db.driver}" />
<property name="jdbcUrl" value="${db.master.url}" />
<property name="username" value="${db.master.username}" />
<property name="password" value="${db.master.password}" />
</bean>
<!-- 事务管理 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
模型类创建:
//=====================================================================================
//=======================Model Layer===================================================
//=====================================================================================
package com.ch.sys.model;
import com.ch.fastbuilder.model.Model;
public class Employee extends Model {
public static String TABLE = "employee";
public static String ID = "id";
public static String ACCOUNT = "account";
public static String PASSWORD = "password";
public static String NICKNAME = "nickname";
public static String GENDER = "gender";
public static String HEAD_URL = "head_url";
public static String TYPE = "type";
public static String CREATED = "created";
public Employee() {
super.setPrimaryKey(ID);
super.setTableName(TABLE);
//super.setGenerationType(GenerationType.UUID);
super.setGenerationType(GenerationType.IDENTITY);
super.setColumns(ID,ACCOUNT,PASSWORD,NICKNAME,GENDER,HEAD_URL,TYPE,CREATED);
}
public static Employee getByAccountPwd(String account, String password) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("account", account);
params.put("password", SHA1.signature(password));
String sql = "account=:account and password=:password";
Employee employee = Model.Where(sql, params).get(Employee.class);
return employee;
}
}
//=====================================================================================
//=======================Service Layer=================================================
//=====================================================================================
@Service
public class EmployeeService {
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Employee getByAccountPwd(String account, String password) {
Employee employee = Employee.getByAccountPwd(account, password);
return employee;
}
}
//=====================================================================================
//=======================Controller Layer with Spring MVC==============================
//=====================================================================================
@Controller
@RequestMapping(Constants.REST_WEB_URL)
public class EmployeeController extends BaseController {
@Autowired
EmployeeService employeeService;
@Autowired
RoleService roleService;
/**
* 员工登陆
* @return
*/
@ResponseBody
@RequestMapping(value="/employee/login",method=RequestMethod.POST)
public Response login(@RequestBody Employee model, HttpServletRequest request) {
Response response = Response.newResponse();
String account = model.getString(Employee.ACCOUNT);
String password = model.getString(Employee.PASSWORD);
Employee employee = employeeService.getByAccountPwd(account, password);
if(employee == null) {
return response.ACCOUNT_PASS_ERROR();
}
Role role = roleService.getByEmployeeId(employee.getLong(Employee.ID));
employee.set("role", role);
SessionUtils.set(request, employee);
return response.ok(employee);
}
}
关联查询:
public class Role extends Model {
/**
*
*/
private static final long serialVersionUID = 1L;
public static String TABLE = "role";
public static String ID = "id";
public static String NAME = "name";
public static String CREATED = "created";
public Role() {
super.setPrimaryKey(ID);
super.setTableName(TABLE);
//super.setGenerationType(GenerationType.UUID);
super.setGenerationType(GenerationType.IDENTITY);
super.setColumns(ID, NAME, CREATED);
}
public static PageResult findByPage(ParamMap params) {
BuilderModel builder = Model.InitParams(params);
PageResult pageResult = builder.findPage(Role.class);
return pageResult;
}
public static Role getByEmployeeId(Long employeeId) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("employee_id", employeeId);
BuilderModel builder = Model.InitParams(params);
builder.select("r.id, r.name, r.created");
builder.from("employee_role emr");
builder.innerJoin("role r on emr.role_id = r.id");
builder.where("emr.employee_id = :employee_id");
Role role = builder.get(Role.class);
return role;
}
}
新增一条数据:
//新增操作
public void add() {
Employee employee = new Employee();
employee.set(Employee.ACCOUNT, "wangwei");
employee.set(Employee.PASSWORD, "111111");
employee.create();
//或者这样执行create方法
Model.Create(employee);
}
修改一条数据:
//修改操作
public void update() {
Employee employee = new Employee();
employee.set(Employee.ID, 1);
employee.set(Employee.ACCOUNT, "LiMing");
employee.set(Employee.PASSWORD, "123456");
employee.update();
//或者这样执行update方法
Model.Update(employee);
}
删除一条数据:
//删除操作
public void delete() {
Employee employee = new Employee();
employee.set(Employee.ID, 1);
employee.delete();
//或者这样执行delete方法
Model.Delete(employee);
//或者这样执行delete方法
//Model.Delete(id, Employee.class);
Model.Delete(1, Employee.class);
}
事务管理:
@Service("roleService")
public class RoleService {
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Response findPage(ParamMap param) {
Response response = Response.newResponse();
PageResult pageResult = Role.findByPage(param);
return response.setPageResults(pageResult);
}
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public Role getByEmployeeId(Long employeeId) {
Role role = Role.getByEmployeeId(employeeId);
return role;
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public Response add(Role role) {
Response response = Response.newResponse();
Timestamp time = new Timestamp(System.currentTimeMillis());
role.set(Role.CREATED, time);
role.create();
return response.OK();
}
@Transactional(readOnly = false, propagation = Propagation.REQUIRED)
public Response update(Role role) {
Response response = Response.newResponse();
role.update();
return response.OK();
}
}
单条数据查询:
package com.ch.sys.service;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Service;
import com.ch.fastbuilder.model.Model;
import com.ch.sys.model.Employee;
import com.ch.sys.utils.SHA1;
@Service
public class EmployeeService {
public Employee getByAccountPwd(String account, String password) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("account", account);
params.put("password", SHA1.signature(password));
String sql = "account=:account and password=:password";
Employee employee = Model.Where(sql, params).get(Employee.class);
return employee;
}
}
分页查询:
public static PageResult findByPage(ParamMap params) {
BuilderModel builder = Model.InitParams(params);
PageResult pageResult = builder.findPage(Role.class);
return pageResult;
}
* 或者这样分页查询
List<Role> roles = Model.Limit(0, 10).list(Role.class);
* 或者这样分页查询
Map<String,Object> params = new HashMap<String,Object>();
params.put("id", 5);
params.put("date", new Date());
List<Role> roles = Model.Where("role_id > :id and created < :date", params).limit(1, 10).list(Role.class);
* 这么写太长了么?可以这么写
BuilderModel builder = Model.Where("role_id > :id and created < :date", params);
List<Role> roles = builder.limit(1, 10).list(Role.class);
* 可以自定义查询字段
List<Role> roles = Model.Select("id, name, created").limit(1, 10).list(Role.class);
执行SQL语句:
Map<String,Object> params = new HashMap<String,Object>();
params.put("name", "修改名称");
* 修改操作
Model.SQL("update role set name = :name").withParams(params).excute();
* 查询操作
Model.SQL("select * from Role where id > :id").withParams(params).list(Role.class);
* 查询数量,更多操作这里不列出来了,具体看demo
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。
1. 开源生态
2. 协作、人、软件
3. 评估模型