我的JDBC通用DAO(解说)

前两篇的连接:

1,我的JDBC通用DAO  

2,我的JDBC通用DAO(续)

为了方便大家阅读和调试,现在把demo发出来

先介绍一下新增的类

首先是主角登场,业务模型类

@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>{

}

再是接口实现

@Repository("modelDao")
public class ModelDaoImpl extends BaseDaoMysqlImpl<OneModel, Integer> implements ModelDao {
	public ModelDaoImpl() {
		super(OneModel.class);
	}
}

好了,写完了。你没看错,基本都是空的

咱写个main方法测一下

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打包

我的JDBC通用DAO(demo)

osc博文没找到文件上传,就请各位移步代码分享模块下载吧

你可能感兴趣的:(DAO,jdbc,通用)