java databasetool_Java使用超级工具包Hutool操作数据库

#===================================================================

# pom.xml中添加引用

#

#

# mysql

# mysql-connector-java

# 8.0.17

#

#

#

#

# com.microsoft.sqlserver

# mssql-jdbc

# 7.4.0.jre12

# test

#

# 数据库配置文件样例

# \src\main\resources目录下创建config/db.setting

# DsFactory默认读取的配置文件是config/db.setting

# db.setting的配置包括两部分:基本连接信息和连接池配置信息。

# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来

#===================================================================

## db.setting文件

#mysql

#url = jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false

#driver =com.mysql.cj.jdbc.Driver

#sqlserver

url = jdbc:sqlserver://111.230.23.148:1433; DatabaseName=SampleDB

driver =com.microsoft.sqlserver.jdbc.SQLServerDriver

user = sa

pass = Smile2017AbC_168_bCd8690

## 可选配置

# 是否在日志中显示执行的SQL

showSql = true

# 是否格式化显示的SQL

formatSql = false

# 是否显示SQL参数

showParams = true

package com.database.demo;

import cn.hutool.db.Db;

import cn.hutool.db.Entity;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.springframework.boot.test.context.SpringBootTest;

import org.springframework.test.context.junit4.SpringRunner;

import java.sql.SQLException;

import java.util.List;

@RunWith(SpringRunner.class)

@SpringBootTest

public class DemoApplicationTests {

@Test

public void contextLoads() {

}

@Test

public void Test1() throws SQLException {

//product为表名

// List list = Db.use().findAll("product");

// for (Entity entity : list) {

// System.out.println(entity.toString());

// }

// 直接sql语句操作 insert/update/delete

// Integer i = Db.use().execute("insert into product(username,password,realname) values(?,?,?)", "zhangxiao", "1234568", "张晓");

// if (i > 0) {

// System.out.println("数据插入成功");

// } else {

// System.out.println("数据插入失败");

// }

Entity entity = Entity.create("product");

List list = Db.use().page(entity, 2, 10);

for (Entity entity1 : list) {

//System.out.println(entity1.toString());

System.out.printf("用户ID: %s 用户名:%s 真实姓名:%s", entity1.get("id"), entity1.get("username"), entity1.get("realname"));

System.out.println();

}

}

}

package com.database.demo.model;

public class Product {

private int id;

private String username;

private String password;

private String realname;

public Product(int id, String username, String password, String realname) {

this.id = id;

this.username = username;

this.password = password;

this.realname = realname;

}

public int getId() {

return id;

}

public void setId(int id) {

this.id = id;

}

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;

}

public String getRealname() {

return realname;

}

public void setRealname(String realname) {

this.realname = realname;

}

@Override

public String toString() {

return "Product{" +

"id=" + id +

", username='" + username + '\'' +

", password='" + password + '\'' +

", realname='" + realname + '\'' +

'}';

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值