首页 > 数据库技术 > 详细

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

时间:2019-09-24 10:30:02      阅读:402      评论:0      收藏:0      [点我收藏+]
#===================================================================
# pom.xml中添加引用
#       <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
#       <dependency>
#           <groupId>mysql</groupId>
#           <artifactId>mysql-connector-java</artifactId>
#           <version>8.0.17</version>
#       </dependency>
# 数据库配置文件样例
# \src\main\resources目录下创建config/db.setting
# DsFactory默认读取的配置文件是config/db.setting
# db.setting的配置包括两部分:基本连接信息和连接池配置信息。
# 基本连接信息所有连接池都支持,连接池配置信息根据不同的连接池,连接池配置是根据连接池相应的配置项移植而来
#===================================================================

## db.setting文件
url = jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC&useSSL=false
user = root
pass = smile6111
driver =com.mysql.cj.jdbc.Driver
## 可选配置
# 是否在日志中显示执行的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<Entity> 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<Entity> list = Db.use().page(entity, 2, 10);
        for (Entity entity1 : list) {
            System.out.println(entity1.toString());
        }

    }

}
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操作数据库

原文:https://www.cnblogs.com/smartsmile/p/11576584.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!