• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

slardar: Slardar is a frame of data persistence layer,whitch features are simila ...

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

slardar

开源软件地址:

https://gitee.com/rpgmakervx/slardar

开源软件介绍:

Slardar Sql Mapper Framework for Java

slardar

Slardar is a frame of data persistence layer,whitch features are similar to mybatis , hibernate etc...Slardar use javascript to build dynamic sql,whitch is easier for some one who familiar with javascript.It also provides annotaions to build relational mapping.Simplicity is the biggest advantage of the Slardar data mapper over object relational mapping tools.

Features:

  • use javascript to build dynamic sql。
  • support simple inerface define to map your sql, or simply use native jdbc interface.
  • connection pool is supported by default, or the third pool (now contains DBCP and Druid)

Install:

   <dependency>       <groupId>org.easyarch</groupId>       <artifactId>slardar</artifactId>       <version>1.0-SNAPSHOT</version>   </dependency>

Get Started:

A simple query:

Assume you have a table named: t_user. A query demo like this:

First create a class: java User

package org.easyarch.test.pojo;import org.easyarch.slardar.annotation.entity.Column;import org.easyarch.slardar.annotation.entity.Table;@Table(tableName = "user")public class User {    @Column(name = "client_id")    private String clientId;    @Column(name = "username")    private String userName;    @Column(name = "password")    private String password;    @Column(name = "phone")    private String phone;    public String getClientId() {        return clientId;    }    public void setClientId(String clientId) {        this.clientId = clientId == null ? null : clientId.trim();    }    public String getUserName() {        return userName;    }    public void setUserName(String userName) {        this.userName = userName;    }    public String getPassword() {        return password;    }    public void setPassword(String password) {        this.password = password == null ? null : password.trim();    }    public String getPhone() {        return phone;    }    public void setPhone(String phone) {        this.phone = phone == null ? null : phone.trim();    }}

Then you finish object-relational mapping,next to create an mapper:

package org.easyarch.test.dao;import org.easyarch.slardar.annotation.sql.SqlParam;import org.easyarch.test.pojo.User;import java.util.List;public interface UserMapper {   public User findById(@SqlParam(name = "id") String id);   public List<User> findByUser(User user);}

Use slardar's api: java DBSession,create a class:

public class UserService {   private UserMapper mapper;   private DBSession session;   public UserService(){       try {           DBSessionFactory sessionFactory = new DBSessionFactoryBuilder().build(                   ResourcesUtil.getResourceAsStream("/config.xml"));           session = sessionFactory.newDelegateSession();           mapper = session.getMapper(UserMapper.class);       } catch (Exception e) {           e.printStackTrace();       }   }   public User getUser(String id){       return mapper.findById(id);   }   public List<User> getUsers(User user){       return mapper.findByUser(user);   }}

Finally make some configurations:First make some configuration to init db,create resources/db.properties:

username = *****password = *****url = jdbc:mysql://localhost:3306/testdb?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=falsedriverClassName = com.mysql.jdbc.DrivermaxActive =200minIdle = 10initialSize = 50maxWait = 6000

and resources/config.xml

<configuration>   <interface package="org.easyarch.test.dao" />   <mapper location="classpath:mapper/" />   <datasource location="classpath:db.properties" class=""/></configuration>

write your sql in resources/mapper/usermapper.java

ctx.namespace = "org.easyarch.test.dao.UserMapper";function findById(params){  return "select * from t_user where client_id = $id$";}function findByUser(params) {  var sql = "select * from user" + ctx.where;  if (params.username != undefined){      sql += " and username = $username";  }  if (params.phone != undefined){      sql += " and phone = $phone$";  }  if (params.client_id != undefined){      sql += " and client_id = $client_id";  }  return sql;}

Write a main function to test it:

public class Main{  public static void main(String[] args){      UserService service = new UserService();      System.out.println(service.getUser("123456"));            User user = new User();      user.setUserName("code4j");      user.setPhone("13600000000");      System.out.println(service.getUsers(user));  }}

more infomation in Chinese go to:


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap