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 + '\'' +
                '}';
    }
}

 

你可能感兴趣的:(Java使用超级工具包Hutool操作数据库)