前兩篇的鏈接: java
1,個人JDBC通用DAO spring
2,個人JDBC通用DAO(續) sql
爲了方便你們閱讀和調試,如今把demo發出來 數據庫
先介紹一下新增的類 數據結構
首先是主角登場,業務模型類 ide
@Table(name="model") public class OneModel { private Integer id; private String name; private Date date; @Id @GeneratedValue(strategy=GenerationType.IDENTITY) public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } @Column(name = "test_name") public String getName() { return name; } public void setName(String name) { this.name = name; } public Date getDate() { return date; } public void setDate(Date date) { this.date = date; } @Override public String toString() { return "OneModel [id=" + id + ", name=" + name + ", date=" + date + "]"; } }這個類是和數據庫中的表關聯的,因此須要一個名叫test的庫,而後建一張表
CREATE TABLE `model` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(64) DEFAULT NULL, `date` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8而後就能夠寫模型的操做接口和實現了
先是模型操做接口 測試
public interface ModelDao extends BaseDao<OneModel, Integer>{ }
再是接口實現 this
@Repository("modelDao") public class ModelDaoImpl extends BaseDaoMysqlImpl<OneModel, Integer> implements ModelDao { public ModelDaoImpl() { super(OneModel.class); } }
好了,寫完了。你沒看錯,基本都是空的 spa
咱寫個main方法測一下 .net
public class Main { public static void main(String[] args) { new Main().demo(); } public void demo(){ ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml"); ModelDao modelDao=context.getBean("modelDao",ModelDao.class); //增 OneModel model=new OneModel(); model.setDate(new Date()); model.setName("隨便寫"); model=modelDao.save(model); Integer id=model.getId(); //查一下 model=modelDao.get(id); System.out.println(model); //改 model.setName("再試一次"); modelDao.update(model); //再查一下 model=modelDao.get(id); System.out.println(model); //刪了吧 modelDao.del(id); } }
增刪改查全都搞定,是否是很簡單?
什麼?你說的用的不是jdbc?我暈...給你看日誌
[2013-12-08 23:39:55,507] DEBUG main (BaseDaoMysqlImpl.java:269) - sql : insert into model(test_name,date) values(?,?) values:[隨便寫, Sun Dec 08 23:39:55 CST 2013] [2013-12-08 23:39:55,933] INFO main (BaseDaoMysqlImpl.java:129) - sql : select * from model where id=? values:[2] OneModel [id=2, name=隨便寫, date=2013-12-08 23:39:55.0] [2013-12-08 23:39:55,945] DEBUG main (BaseDaoMysqlImpl.java:375) - sql : update model set test_name=?, date=? where id=? values:[再試一次, 2013-12-08 23:39:55.0, 2] [2013-12-08 23:39:55,955] INFO main (BaseDaoMysqlImpl.java:129) - sql : select * from model where id=? values:[2] OneModel [id=2, name=再試一次, date=2013-12-08 23:39:55.0] [2013-12-08 23:39:55,959] DEBUG main (BaseDaoMysqlImpl.java:415) - sql : delete from model where id=? values:[2]
2013-12-16 追加
小夥伴們要求寫個多表查詢的demo,就在這裏了
最近幾天事情比較多,每天加班,今天才有時間弄這個,很抱歉
好吧,如今切入正題
首選建測試表
CREATE TABLE `user` ( `user_id` int(11) NOT NULL AUTO_INCREMENT, `user_name` varchar(32) DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 CREATE TABLE `user_info` ( `user_id` int(11) NOT NULL, `birthday` datetime DEFAULT NULL, `address` varchar(128) DEFAULT NULL, PRIMARY KEY (`user_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8而後是程序中對應的模型類
@Table(name = "user") public class User { private Integer userId; private String userName; @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "user_id") public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } @Column(name = "user_name") public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } @Override public String toString() { return "User [userId=" + userId + ", userName=" + userName + "]"; } } @Table(name = "user_info") public class UserInfo { private Integer userId; private Date birthday; private String address; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "user_id") public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "UserInfo [userId=" + userId + ", birthday=" + birthday + ", address=" + address + "]"; } }相關的DAO層類就不給出了,請你們參照OneModel的相關接口與實現,或者下載打包好的代碼 個人JDBC通用DAO(demo)
下邊是測試方法和多表查詢demo
public class Main1 { public static void main(String[] args) { new Main1().demo(); } public void demo(){ ApplicationContext context=new ClassPathXmlApplicationContext("spring.xml"); UserDao userDao = context.getBean("userDao",UserDao.class); UserInfoDao userInfoDao = context.getBean("userInfoDao",UserInfoDao.class); //首先插入測試數據 User user = new User(); user.setUserName("測試用戶"); user = userDao.save(user); UserInfo info = new UserInfo(); info.setUserId(user.getUserId()); info.setAddress("未知位置"); info.setBirthday(new Date()); info = userInfoDao.save(info); //查一下看看 user = userDao.get(user.getUserId()); System.out.println("保存後的user=" + user); info = userInfoDao.get(user.getUserId()); System.out.println("保存後的userInfo=" + info); //改一下 user.setUserName("改一改"); userDao.update(user); System.out.println("修改後的user=" + user); //多表查詢 String searchSql = "select user.user_id, user_name, address, birthday " + "from user, user_info where user.user_id = user_info.user_id " + "and user.user_id = ?"; List<Object> searchValues = new ArrayList<Object>(); searchValues.add(user.getUserId()); //能夠使用Map做爲多表查詢結果的數據結構 List<Map<String, Object>> totalInfoMap = userDao.searchForMap(searchSql, searchValues); System.out.println("map風格的查詢結果totalInfoMap=" + totalInfoMap); //也能夠自定義一個類做爲多表查詢結果的數據結構 List<TotalUserInfo> totalInfo = userDao.search(searchSql, searchValues, TotalUserInfo.class); System.out.println("對象風格的查詢結果totalInfo=" + totalInfo); //分頁查詢 PageBean<TotalUserInfo> page = new PageBean<TotalUserInfo>(); page.setPageNo(1); page = userDao.search(searchSql, searchValues, page , TotalUserInfo.class); System.out.println("分頁查詢結果 page=" + page); } }
複雜查詢的結果能夠用java原生的map來存儲(比較便捷),也能夠自定義一個盒子(類)來存儲查詢結果,這種方式寫出的代碼風格會比較友好,因此我本人喜歡這種
測試中使用的盒子(類)的聲明
/** * 這個類是一個盒子,僅僅用來裝查詢後的結果,在數據庫中沒有表和 * 它對應,因此不用指定表名與主鍵 */ public class TotalUserInfo { private Integer userId; private String userName; private Date birthday; private String address; public Integer getUserId() { return userId; } public void setUserId(Integer userId) { this.userId = userId; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public Date getBirthday() { return birthday; } public void setBirthday(Date birthday) { this.birthday = birthday; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Override public String toString() { return "TotalUserInfo [userId=" + userId + ", userName=" + userName + ", birthday=" + birthday + ", address=" + address + "]"; } }
最後看一下上邊測試生成的日誌:
[2013-12-16 23:40:17,903] INFO main (BaseDaoMysqlImpl.java:277) - sql : insert into user(user_name) values(?) values:[測試用戶] [2013-12-16 23:40:18,120] INFO main (BaseDaoMysqlImpl.java:266) - sql : insert into user_info(birthday,address,user_id) values(?,?,?) values:[Mon Dec 16 23:40:18 CST 2013, 未知位置, 1] [2013-12-16 23:40:18,135] INFO main (BaseDaoMysqlImpl.java:137) - sql : select * from user where user_id=? values:[1] 保存後的user=User [userId=1, userName=測試用戶] [2013-12-16 23:40:18,140] INFO main (BaseDaoMysqlImpl.java:137) - sql : select * from user_info where user_id=? values:[1] 保存後的userInfo=UserInfo [userId=1, birthday=2013-12-16 23:40:18.0, address=未知位置] [2013-12-16 23:40:18,145] INFO main (BaseDaoMysqlImpl.java:383) - sql : update user set user_name=? where user_id=? values:[改一改, 1] 修改後的user=User [userId=1, userName=改一改] [2013-12-16 23:40:18,151] INFO main (BaseDaoMysqlImpl.java:755) - sql : select user.user_id, user_name, address, birthday from user, user_info where user.user_id = user_info.user_id and user.user_id = ? values:[1] map風格的查詢結果totalInfoMap=[{user_id=1, user_name=改一改, address=未知位置, birthday=2013-12-16 23:40:18.0}] [2013-12-16 23:40:18,154] INFO main (BaseDaoMysqlImpl.java:670) - sql : select user.user_id, user_name, address, birthday from user, user_info where user.user_id = user_info.user_id and user.user_id = ? values:[1] 對象風格的查詢結果totalInfo=[TotalUserInfo [userId=1, userName=改一改, birthday=2013-12-16 23:40:18.0, address=未知位置]] [2013-12-16 23:40:18,158] INFO main (BaseDaoMysqlImpl.java:435) - sql : select count(*) from (select user.user_id, user_name, address, birthday from user, user_info where user.user_id = user_info.user_id and user.user_id = ?) as _tn values:[1] [2013-12-16 23:40:18,163] INFO main (BaseDaoMysqlImpl.java:670) - sql : select user.user_id, user_name, address, birthday from user, user_info where user.user_id = user_info.user_id and user.user_id = ? limit 0,10 values:[1] 分頁查詢結果 page=PageBean [pageNo=1, nextNo=0, priorNo=0, pageCount=1, rowCount=1, pageSize=10, startRow=0, orderBy=null, orderType=null, pageList=null, pageListSize=10, list=[TotalUserInfo [userId=1, userName=改一改, birthday=2013-12-16 23:40:18.0, address=未知位置]], groupby=null]
最後是整個demo打包
osc博文沒找到文件上傳,就請各位移步代碼分享模塊下載吧