mysql java orm_ObjectiveSQL(Java ORM) 之 MySQL 实战

第一步,引用Maven依赖:

com.github.braisdom

objective-sql

1.3.4

复制代码

第二步,使用Annotation 定义一个DomainModel :

import com.github.braisdom.objsql.annotations.Column;

import com.github.braisdom.objsql.annotations.DomainModel;

import com.github.braisdom.objsql.annotations.Queryable;

import com.github.braisdom.objsql.annotations.Relation;

import com.github.braisdom.objsql.relation.RelationType;

import java.util.List;

@DomainModel

public class Member {

@Queryable

@Column(updatable = false)

private String no;

@Queryable

private String name;

private Integer gender;

private String mobile;

private String otherInfo;

@Relation(relationType = RelationType.HAS_MANY)

private List orders;

}

复制代码

第三步:定义ConnectionFactory 并注入ObjectiveSQL:

class MySQLConnectionFactory implements ConnectionFactory {

@Override

public Connection getConnection(String dataSourceName) throws SQLException {

String url = "jdbc:mysql://localhost:4406/objective_sql?serverTimezone=Asia/Shanghai";

String user = "root";

String password = "123456";

Connection connection;

try {

Class.forName("com.mysql.cj.jdbc.Driver").newInstance();

connection = DriverManager.getConnection(url, user, password);

} catch (InstantiationException | IllegalAccessException | ClassNotFoundException ex) {

throw new IllegalStateException(ex.getMessage(), ex);

}

return connection;

}

}

复制代码

第四步:执行数据库操作

创建会员

Member newMember = new Member();

newMember.setNo("100000")

.setName("Pamela")

.setGender(1)

.setRegisteredAtWithJoda(DateTime.now())

.setUpdatedAt(Timestamp.valueOf("2020-10-05 00:00:00"))

.setMobile("15011112222");

Member member = Member.create(newMember, true);

INSERT INTO members (no,name,gender,mobile,extended_attributes,registered_at,updated_at)

VALUES (?,?,?,?,?,?,?), with: [100000,Pamela,1,15011112222,null,2020-10-08 11:01:57.368,2020-10-05 00:00:00.0]

统计会员数量

long count = Member.count("id > ?", 10);

SELECT COUNT(*) AS count_rows FROM members

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值