jdao 初始化: DaoFactory.setDefaultDataSource(getDataSource()); jdao初始化 设置数据源,一步完成。
getDataSource()获取数据源方法: 如:ActionTest1_1_2.java 中: public static DataSource getDataSource() throws Exception { Properties p = new Properties(); p.load(ActionTest1_1_2.class.getClassLoader().getResourceAsStream(“druid.properties”)); return DruidDataSourceFactory.createDataSource(p);}
例如:对 数据库表名为 hstest的操作 CREATE TABLE hstest ( id int(10) DEFAULT NULL, value varchar(50) DEFAULT ‘’, rowname varchar(50) DEFAULT ‘’ )
hstest
id
value
rowname
一.生成dao对象,生成Hstest.java public void createDao() throws Exception { Class.forName(“com.mysql.jdbc.Driver”); String driverUrl = “jdbc:mysql://127.0.0.1:3306/test”; String path = System.getProperty(“user.dir”) + “/test/com/jdao/action”; CreateDaoUtil.createFile(“com.jdao.action”, “hstest”,path,DriverManager.getConnection(driverUrl, “root”, “123456”), “utf-8”); //com.jdao.action 为 Hstest的包名 //hstest为表名 }
二.对Hstest对象的操作 查询 SQL: select value,rowname from hstest where id between 2 and 10; jdao对象操作如下: Hstest t = new Hstest(); t.where(Hstest.ID.BETWEEN(2, 10)); t.query(Hstest.VALUE, Hstest.ROWNAME);
插入 SQL: insert into hstest (id,rowname,value) values(1,”donnie”,”wuxiaodong”) jdao对象操作如下: Hstest t = new Hstest(); t.setId(1); t.setRowname(“donnie”); t.setValue(“wuxiaodong”); t.save();
批量插入 SQL: insert into hstest (id,rowname,value) values(1,”donnie1”,”wuxiaodong1”),(2,”donnie2”,”wuxiaodong2”),(3,”donnie3”,”wuxiaodong3”) jdao对象操作如下: Hstest t = new Hstest(); t.setId(1); t.setRowname(“donnie1”); t.setValue(“wuxiaodong1”); t.addBatch(); t.setId(2); t.setRowname(“donnie2”); t.setValue(“wuxiaodong2”); t.addBatch(); t.setId(3); t.setRowname(“donnie3”); t.setValue(“wuxiaodong3”); t.addBatch(); t.batchForSave();
更新 SQL: update hstest set rowname=”wuxiaodong”,value=”wuxiaodong” where id=10 jdao对象操作如下: Hstest t = new Hstest(); t.setRowname(“wuxiaodong”); t.setValue(“wuxiaodong”); t.where(Hstest.ID.EQ(10)); t.update(); 删除 SQL: delete from hstest where id=2 jdao对象操作如下: Hstest t = new Hstest(); t.where(Hstest.ID.EQ(2)); t.delete(); 三.支持SQL操作 DBUtils DBUtils> db=new DBUtils(); db.select(“select * from hstest where id=? limit 1”,1); System.out.println(db.getString(“value”)); int i = db.execute(“insert into hstest(rowname,value)values(?,?)”,1,2); 四.自定义类继承 DBUtils 任何子类继承自DBUtils 都可以设置与其对应的数据源,同时支持sql编写,支持翻页 如:class RsTest extends DBUtils {} //翻页 public static void testSelectListPage() throws Exception { RsTest rt = new RsTest(); // 分页查询方法 rt.selectListPage(0, 20, “select * from hstest”); System.out.println(rt.rsList().size()); // selectListPage 会返回 totalcount List list = rt.rsList(); for (RsTest r : list) { System.out.println(r.getString(“value”)); } }
//单行返回 public static void testSelect() throws Exception { RsTest rt = new RsTest(); rt.select(“select * from hstest where id=?”, 1); System.out.println(rt.getString(“value”)); rt.select(“select * from hstest where id=?”, 2); System.out.println(rt.getString(“value”)); }
//插入 public static void testInsert() throws Exception { RsTest rt = new RsTest(); System.out.println(rt.execute(“insert into hstest(value,rowname)values(?,?),(?,?) “, “wu1”, “11”, “wu2”, “22”)); }
//生成dao 的翻页测试类 public static void testPageTurn() throws Exception { Hstest ht = new Hstest(); ht.setPageTurn(true); //翻页 ht.where(Hstest.ID.GE(0)); List list = ht.query(); System.out.println(“totalcount:” + list.get(0).getTotalcount()); for (Hstest h : list) { System.out.println(h.getRowname() + ” ” + h.getValue()); } }
//PageDao类测试 public static void testPageDao() throws Exception { Hstest ht = new Hstest(); ht.where(Hstest.ID.GE(1)); PageDao pd = ht.selectListPage(); System.out.println(“totalcount:” + pd.getTotalcount()); List list = pd.getList(); for (Hstest h : list) { System.out.println(h.getRowname() + ” ” + h.getValue()); } } 五.事务 Transaction t = new Transaction(getDataSource()); Hstest hstest = new Hstest(); hstest.setTransaction(t); hstest.setRowname(“wu”); hstest.setValue(“dong”); hstest.save(); Hstest hstest2 = new Hstest(); hstest2.setTransaction(t); hstest2.setRowname(“wu2”); hstest2.setValue(“dong2”); hstest2.save(); DBUtils rt = new DBUtils(); rt.setTransaction(t); rt.execute(“insert into hstest(rowname,value)values(?,?)”, 1, 2); t.rollBackAndClose();