java redis缓存工具类_util: 对日常工作中常见的工具类进行打包: 1、数据库:DBUtil工具类 2、Redis缓存:JedisUtil工具类...

/**

* 功能:使用原始SQL查询单条记录

* 返回每条记录封装到Map

*/

public static void demo1_1() {

DBUtil db = new DBUtil();

try {

String sql = "select * from account where nickName = ? and userName = ?";

Object[] params = new Object[]{"wuren","562117904@qq.com"};

Map map = db.queryOne(sql, params);

System.out.print("【queryOne-原始SQL】");

System.out.println(JSONObject.toJSONString(map));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL查询单条记录

* 返回记录映射到Bean对象

*/

public static void demo1_2() {

DBUtil db = new DBUtil();

try {

String sql = "select * from account where nickName = ? and userName = ?";

Object[] params = new Object[]{"wuren","562117904@qq.com"};

Account account = db.queryOne(sql, params, Account.class);

System.out.print("【queryOne-原始SQL】");

System.out.println(JSONObject.toJSONString(account));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板查询单条记录

* 返回每条记录封装到Map

*/

public static void demo1_3() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("nickName", "wuren");

params.put("userName", "562117904@qq.com");

Map map = db.queryOne("account", "demo1_2", params);

System.out.print("【queryOne-SQL模板】");

System.out.println(JSONObject.toJSONString(map));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板查询单条记录

* 返回记录映射到Bean对象

*/

public static void demo1_4() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("nickName", "wuren");

params.put("userName", "562117904@qq.com");

Account account = db.queryOne("account", "demo1_2", params, Account.class);

System.out.print("【queryOne-SQL模板】");

System.out.println(JSONObject.toJSONString(account));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL查询多条记录

* 返回每条记录封装到Map,再将Map集合存入List

*/

public static void demo2_1() {

DBUtil db = new DBUtil();

try {

String sql = "select * from account where status = ? order by nickName";

Object[] params = new Object[]{1};

List> list = db.query(sql, params);

System.out.print("【query-原始SQL】");

System.out.println(JSONObject.toJSONString(list));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL查询多条记录

* 返回记录映射到Bean对象,再将Bean存入List集合中

*/

public static void demo2_2() {

DBUtil db = new DBUtil();

try {

String sql = "select * from account where status = ? order by nickName";

Object[] params = new Object[]{1};

List list = db.query(sql, params, Account.class);

System.out.print("【query-原始SQL】");

System.out.println(JSONObject.toJSONString(list));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板查询多条记录

* 返回每条记录封装到Map,再将Map集合存入List

*/

public static void demo2_3() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("status", 1);

List> list = db.query("account", "demo2_2", params);

System.out.print("【query-SQL模板】");

System.out.println(JSONObject.toJSONString(list));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板查询多条记录

* 返回记录映射到Bean对象,再将Bean存入List集合中

*/

public static void demo2_4() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("status", 1);

List list = db.query("account", "demo2_2", params, Account.class);

System.out.print("【query-SQL模板】");

System.out.println(JSONObject.toJSONString(list));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL单值查询,返回 Long

*

*/

public static void demo3() {

DBUtil db = new DBUtil();

try {

String sql = "select count(1) from account";

Long result = db.querySingleLong(sql, null);

System.out.print("【querySingleLong】");

System.out.println(JSONObject.toJSONString(result));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL单值查询,返回 Double

*/

public static void demo4() {

DBUtil db = new DBUtil();

try {

String sql = "select count(1)+1.5 from account";

Double result = db.querySingleDouble(sql, null);

System.out.print("【querySingleDouble】");

System.out.println(JSONObject.toJSONString(result));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL单值查询,返回 Float

*/

public static void demo5() {

DBUtil db = new DBUtil();

try {

String sql = "select count(1)+1.5 from account";

Float result = db.querySingleFloat(sql, null);

System.out.print("【querySingleFloat】");

System.out.println(JSONObject.toJSONString(result));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL单值查询,返回 String

*/

public static void demo6() {

DBUtil db = new DBUtil();

try {

String sql = "select nickName from account";

String result = db.querySingleString(sql, null);

System.out.print("【querySingleString】");

System.out.println(JSONObject.toJSONString(result));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL插入一条记录

*/

public static void demo7_1() {

DBUtil db = new DBUtil();

try {

String sql = "insert into account(nickName,userName,password,salt,avatar) values(?,?,?,?,?)";

Object[] params = new Object[]{"wuren_test", "wuren_test@qq.com", "1111", "2222", "0/test.jpg"};

int rows = db.insert(sql, params);

System.out.print("【insert-原始SQL】");

System.out.println(JSONObject.toJSONString(rows));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板插入一条记录

*/

public static void demo7_2() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("nickName", "beetl");

params.put("userName", "wuren_test@qq.com");

params.put("password", "1111");

params.put("salt", "2222");

params.put("avatar", "0/test.jpg");

int rows = db.insert("account", "demo7_2", params);

System.out.print("【insert-SQL模板】");

System.out.println(JSONObject.toJSONString(rows));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL更新记录

*/

public static void demo8_1() {

DBUtil db = new DBUtil();

try {

String sql = "update account set status=1 where nickName=?";

Object[] params = new Object[]{"beetl"};

int rows = db.update(sql, params);

System.out.print("【update-原始SQL】");

System.out.println(JSONObject.toJSONString(rows));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板更新记录

*/

public static void demo8_2() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("nickName", "beetl");

int rows = db.update("account", "demo8_2", params);

System.out.print("【update-SQL模板】");

System.out.println(JSONObject.toJSONString(rows));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用原始SQL删除记录

*/

public static void demo9_1() {

DBUtil db = new DBUtil();

try {

String sql = "delete from account where nickname = ?";

Object[] params = new Object[]{"wuren_test"};

int rows = db.delete(sql, params);

System.out.print("【delete-原始SQL】");

System.out.println(JSONObject.toJSONString(rows));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板删除记录

*/

public static void demo9_2() {

DBUtil db = new DBUtil();

try {

Map params = new HashMap();

params.put("nickName", "beetl");

int rows = db.delete("account", "demo9_2", params);

System.out.print("【delete-SQL模板】");

System.out.println(JSONObject.toJSONString(rows));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 功能:使用SQL模板,条件控制语句

*/

public static void demo10() {

DBUtil db = new DBUtil();

try {

Account acc = new Account();

acc.setNickName("test");

Map params = new HashMap();

params.put("account", acc);

List list = db.query("account", "demo10", params, Account.class);

System.out.print("【SQL模板条件控制语句】");

System.out.println(JSONObject.toJSONString(list));

} catch(Exception e) {

e.printStackTrace();

} finally {

db.closeConnection();

}

}

/**

* 数据库事务

*/

public static void demo11() {

DBUtil db = new DBUtil();

try {

// 开启事务

db.beginTransaction();

String sql = "insert into account(nickName,userName,password,salt,avatar) values(?,?,?,?,?)";

Object[] params = new Object[]{"should_rollback", "wuren_test@qq.com", "1111", "2222", "0/test.jpg"};

int insertRows = db.insert(sql, params);

// 异常SQL

sql = "update account set field_not_exist = 0";

int updateRows = db.update(sql, null);

System.out.println("insertRows = "+insertRows+" updateRows = "+updateRows );

db.commitTransaction();

}catch(Exception e) {

try {

System.out.println("demo12 执行异常 回滚");

db.rollbackTransaction();

} catch (SQLException exp) {

exp.printStackTrace();

}

e.printStackTrace();

} finally {

db.closeConnection();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值