如何使用Jorm增删查改数据库

66次阅读
没有评论

共计 6444 个字符,预计需要花费 17 分钟才能阅读完成。

这篇文章主要介绍“如何使用 Jorm 增删查改数据库”,在日常操作中,相信很多人在如何使用 Jorm 增删查改数据库问题上存在疑惑,丸趣 TV 小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何使用 Jorm 增删查改数据库”的疑惑有所帮助!接下来,请跟着丸趣 TV 小编一起来学习吧!

准备
以 MySQL 为例,执行下面的 sql 建立数据表
CREATE TABLE `t_user` ( 
  `id` int(11) NOT NULL, 
  `name` varchar(50) DEFAULT NULL, 
  `sex` char(4) DEFAULT NULL, 
  `age` int(11) DEFAULT NULL, 
  `career` varchar(100) DEFAULT NULL, 
  PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

引入 jar 或 maven 依赖,需要 jar 包
gerald-jorm-1.0.5.jar 最新版本下载:
commons-logging-1.1.1.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.6.jar
javassist-3.11.0.GA.jar 或 cglib-nodep-2.2.2.jar (根据实际情况选择性加入)

配置文件
在你的 java 工程的 classpath 下建立 config.properties 和 jdbc.cfg.xml 文件
config.properties 内容:
# 下面路径可以根据实际情况指定,为相对 classpath 的路径地址
jdbc.config.path=jdbc.cfg.xml

jdbc.cfg.xml 内容:
?xml version= 1.0 encoding= UTF-8 ?
jdbc-configuration

  constant name= show_sql value= true /
  constant name= jdbc.batch_size value= 600 /
  constant name= bytecode.provider value= cglib /
 
  connections default= simple
 
  connection name= simple
  property name= connection.implementation org.javaclub.jorm.jdbc.connection.impl.SimpleConnection /property
  property name= connection.dialect MySQLDialect /property
  property name= connection.driver com.mysql.jdbc.Driver /property
  property name= connection.jdbcurl jdbc:mysql://127.0.0.1:3306/test?useUnicode=true amp;characterEncoding=UTF-8 /property
  property name= connection.database test /property
  property name= connection.username root /property
  property name= connection.password root /property
  /connection

  connection name= c3p0
  property name= connection.implementation org.javaclub.jorm.jdbc.connection.impl.PooledConnection /property
  property name= connection.dialect MySQLDialect /property
  property name= connection.driver com.mysql.jdbc.Driver /property
  property name= connection.jdbcurl jdbc:mysql://127.0.0.1:3306/test?useUnicode=true amp;characterEncoding=UTF-8 /property
  property name= connection.database test /property
  property name= connection.username root /property
  property name= connection.password root /property
  property name= connection.pool.min 1 /property
  property name= connection.pool.max 8 /property
  property name= connection.test.sql select 1 /property
  /connection
 
  /connections

/jdbc-configuration

实体类 User.java
@PK(value = id)
@Entity(table= t_user)
public class User {
 
  @Id
  private int id;

  private String name;

  private String sex;

  private Integer age;

  private String career;
 
  @NoColumn
  private int kvalue;
 
  public User() {
  super();
  }

  public User(String name, String sex, Integer age, String career) {
  super();
  this.name = name;
  this.sex = sex;
  this.age = age;
  this.career = career;
  }

  public User(Integer id, String name, String sex, Integer age, String career) {
  super();
  this.id = id;
  this.name = name;
  this.sex = sex;
  this.age = age;
  this.career = career;
  }

  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;
  }

  public String getSex() {
  return sex;
  }

  public void setSex(String sex) {
  this.sex = sex;
  }

  public Integer getAge() {
  return age;
  }

  public void setAge(Integer age) {
  this.age = age;
  }

  public String getCareer() {
  return career;
  }

  public void setCareer(String career) {
  this.career = career;
  }

  public int getKvalue() {
  return kvalue;
  }

  public void setKvalue(int kvalue) {
  this.kvalue = kvalue;
  }

  public String toString() {
  StringBuffer sb = new StringBuffer();
  sb.append([ + id + , + name + , + sex + , + age + , + career +]
  return sb.toString();
  }

}

这里字段和 java 实体类 User 的属性在命名上是一致的,如果不一致,比如如果表创建 sql 为:
CREATE TABLE `t_user` ( 
  `user_id` int(11) NOT NULL, 
  `user_name` varchar(50) DEFAULT NULL, 
  `sex` char(4) DEFAULT NULL, 
  `col_age` int(11) DEFAULT NULL, 
  `career_job` varchar(100) DEFAULT NULL, 
  PRIMARY KEY (`id`) 
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

那么对应的实体 User 应该写成:
@PK(value = id)
@Entity(table= t_user)
public class User {
 
  @Id
  @Column(user_id)
  private int id;

  @Column(user_name)
  private String name;
 
  // 与数据库字段命名一致,可以不指定 @Column
  private String sex;

  @Column(col_age)
  private Integer age;

  @Column(career_job)
  private String career;
 
  @NoColumn
  private int kvalue;
 
  public User() {
  super();
  }

  public User(String name, String sex, Integer age, String career) {
  super();
  this.name = name;
  this.sex = sex;
  this.age = age;
  this.career = career;
  }

  public User(Integer id, String name, String sex, Integer age, String career) {
  super();
  this.id = id;
  this.name = name;
  this.sex = sex;
  this.age = age;
  this.career = career;
  }

  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;
  }

  public String getSex() {
  return sex;
  }

  public void setSex(String sex) {
  this.sex = sex;
  }

  public Integer getAge() {
  return age;
  }

  public void setAge(Integer age) {
  this.age = age;
  }

  public String getCareer() {
  return career;
  }

  public void setCareer(String career) {
  this.career = career;
  }

  public int getKvalue() {
  return kvalue;
  }

  public void setKvalue(int kvalue) {
  this.kvalue = kvalue;
  }

  public String toString() {
  StringBuffer sb = new StringBuffer();
  sb.append([ + id + , + name + , + sex + , + age + , + career +]
  return sb.toString();
  }

}

对 User 的增删查改,UserCrudTest.java,记得引入 junit-4.8.2.jar
public class UserCrudTest {

  static Session session;
 
  @BeforeClass
  public static void before() {
  session = Jorm.getSession();
  }
 
  @AfterClass
  public static void after() {
  Jorm.free();
  }
 
  @Test
  public void save_user() {
  session.clean(User.class);
  User user = null;
  for (int i = 0; i 600; i++) {
  String sex = (i % 2 == 0 ? 男 : 女
  user = new User(Strings.fixed(5), sex, Numbers.random(98), Strings.random(8));
  session.save(user);
  }
  }
 
  @Test // 批量保存
  public void batch_save_user() {
  session.clean(User.class);
  JdbcBatcher batcher = session.createBatcher();
  User user = null;
  for (int i = 0; i 600; i++) {
  String sex = (i % 2 == 0 ? 男 : 女
  user = new User(Strings.fixed(5), sex, Numbers.random(98), Strings.random(8));
  batcher.save(user);
  }
  batcher.execute();
  }
 
  @Test
  public void loadUser() {
  User user = session.read(User.class, 1);
  // 这里 user 是一个代理对象,因为 @Entity(table= t_user , lazy = true)
  System.out.println(user.getCareer());// 发出查询 sql
  }
 
  @Test
  public void deletUser() {
  User user = session.read(User.class, 1);
  if(null != user) {
  session.delete(user);
  }
  user = session.read(User.class, 1);
  System.out.println(user);
  }
 
  @Test
  public void test_update_proxy() {
 
  User u;
  u = session.read(User.class, 2);
  Assert.assertNotNull(u);
  Assert.assertTrue(u instanceof JormProxy);
 
  u.setName(Gerald.Chen
  session.update(u);
  System.out.println(u.getName());
  u = session.read(User.class, 2);
  Assert.assertTrue(Gerald.Chen .equals(u.getName()));
  }
 
  @Test
  public void queryUser() {
  SqlParams User params = new SqlParams User
  params.setObjectClass(User.class);
  params.setFirstResult(8);
  params.setMaxResults(20);
  List User users = session.list(params);
  System.out.println(users.size());
  System.out.println(users);
  }
 
}

到此,关于“如何使用 Jorm 增删查改数据库”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注丸趣 TV 网站,丸趣 TV 小编会继续努力为大家带来更多实用的文章!

正文完
 
丸趣
版权声明:本站原创文章,由 丸趣 2023-07-28发表,共计6444字。
转载说明:除特殊说明外本站除技术相关以外文章皆由网络搜集发布,转载请注明出处。
评论(没有评论)