#=================================================================== # 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> # # <!-- https://mvnrepository.com/artifact/com.microsoft.sqlserver/mssql-jdbc --> # <dependency> # <groupId>com.microsoft.sqlserver</groupId> # <artifactId>mssql-jdbc</artifactId> # <version>7.4.0.jre12</version> # <scope>test</scope> # </dependency> # 數據庫配置文件樣例 # \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<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()); 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 + '\'' + '}'; } }