Dao 全称为(Data Access Object)数据访问接口,数据访问:顾名思义就是与数据库打交道。又称JavaBean 又称为domain 话不多说,直接上图
在写JavaBean的时候总是会遇到有相同的操作的地方,因此提出Basic 根据上图写出项目结构给出两个表
根据上图首先对actor表和goods表进行映射
package com.cclStu.domain;
import java.util.Date;
public class Actor {
private Integer id ;
private String name;
private String sex;
private Date borndate;
private String phone;
public Actor() { //底层可能用到反射,反射需要无参构造器
}
public Actor(Integer id, String name, String sex, Date borndate, String phone) {
this.id = id;
this.name = name;
this.sex = sex;
this.borndate = borndate;
this.phone = phone;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBorndate() {
return borndate;
}
public void setBorndate(Date borndate) {
this.borndate = borndate;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public String toString() {
return "Actor{" +
"id=" + id +
", name='" + name + ''' +
", sex='" + sex + ''' +
", borndate=" + borndate +
", phone='" + phone + ''' +
'}';
}
}
package com.cclStu.domain;
public class Goods {
private Integer id;
private String goods_name;
private String price;
//反射可能会用到
public Goods() {
}
public Goods(Integer id, String goods_name, String price) {
this.id = id;
this.goods_name = goods_name;
this.price = price;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getGoods_name() {
return goods_name;
}
public void setGoods_name(String goods_name) {
this.goods_name = goods_name;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", goods_name='" + goods_name + ''' +
", price='" + price + ''' +
'}';
}
}
下面进行BasicDao的编写
底层无非就是之前学过的Druid+ApDBUtils连接和封装技术
上代码:
package com.cclStu.dao; import com.cclStu.utils.JdbcUtilsByDruid; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import java.sql.Connection; import java.sql.SQLException; import java.util.List; public class BasicDao{//泛型指定具体的类型 //新建queryrunnner对象 private QueryRunner queryRunner = new QueryRunner(); //通用的dml方法,针对任意的表 public int update(String sql , Object... parameters) { //获取连接 Connection connection = null; try { connection = JdbcUtilsByDruid.getConnection(); int update = queryRunner.update(connection, sql, parameters); return update; } catch (SQLException e) { throw new RuntimeException(e);//将编译异常转为运行异常抛出,方便调用者的使用 }finally { JdbcUtilsByDruid.close(connection); } } //返回多个对象(查询结果是多行的),针对任意表 public List queryMulti(String sql , Class clazz,Object...parameters){ Connection connection = null ; try { connection = JdbcUtilsByDruid.getConnection(); //BeanListHandler 封装到list中 List query = queryRunner.query(connection, sql, new BeanListHandler (clazz), parameters); return query; } catch (SQLException e) { throw new RuntimeException(e); }finally { JdbcUtilsByDruid.close(connection); } } //查询单行结果通用方法 public T querySingle(String sql , Class clazz , Object...parameters){ Connection connection = null; try { connection = JdbcUtilsByDruid.getConnection(); T query = queryRunner.query(connection, sql, new BeanHandler (clazz), parameters); return query; } catch (SQLException e) { throw new RuntimeException(e); }finally { JdbcUtilsByDruid.close(connection); } } //查询单行单列,既返回单值的方法 public Object queryScalar(String sql ,Object...parameters){ Connection connection = null ; try { connection = JdbcUtilsByDruid.getConnection(); Object query = queryRunner.query(connection, sql, new ScalarHandler(), parameters); return query; } catch (SQLException e) { throw new RuntimeException(e); } finally { JdbcUtilsByDruid.close(connection); } } }
使ActorDao和GoodsDao两个表继承BasicDao目的是得到了父类的所有方法,也可以在ActorDao和GoodsDao两个java类中根据相应需求编写特有的方法
package com.cclStu.dao; import com.cclStu.domain.Actor; public class ActorDao extends BasicDao{ //1.有BasicDao所有方法 //2.根据业务需求可以编写特有的方法 }
package com.cclStu.dao; import com.cclStu.domain.Goods; public class GoodsDao extends BasicDao{ //得到父类所有方法 }
下面就是枯燥且无聊的Test
package com.cclStu.test;
import com.cclStu.dao.ActorDao;
import com.cclStu.domain.Actor;
import org.junit.Test;
import java.util.List;
public class TestDao {
@Test
public void testActorDao(){
ActorDao actorDao = new ActorDao();
//1.查询
String sql = "select * from actor where id >= ?";
List actors = actorDao.queryMulti(sql, Actor.class, 1);
System.out.println("====查询结果====");
for (Actor actor : actors) {
System.out.println(actor);
}
}
//查询单行记录
@Test
public void testScaler(){
ActorDao actorDao = new ActorDao();
String sql = "select * from actor where id = ?";
Actor actor = actorDao.querySingle(sql, Actor.class, 1);
System.out.println("查询结果");
System.out.println(actor);
}
//查询单行单列
@Test
public void testSingle(){
ActorDao actorDao = new ActorDao();
String sql = "select name from actor where id = ?";
Object o = actorDao.queryScalar(sql, 2);
System.out.println("====查询结果====");
System.out.println(o);
}
//dml操作
@Test
public void testupdate(){
ActorDao actorDao = new ActorDao();
String sql = "insert into actor values(null,?,?,?,?)";
int update = actorDao.update(sql, "刘德华", "男", "2021-09-27", "14623548875");
System.out.println(update > 0 ? "执行成功!" : "没有影响数据库");
}
}
package com.cclStu.test;
import com.cclStu.dao.GoodsDao;
import com.cclStu.domain.Goods;
import org.junit.Test;
import java.util.List;
public class TestGoodsDao {
@Test
public void testInsert(){ //测试插入语句
//创建GoodsDao对象
GoodsDao goodsDao = new GoodsDao();
//sql语句
String sql = "insert into goods values(null,?,?)";
//update 影响的行数 执行sql
int update = goodsDao.update(sql, "红米k40游戏增强版", "1999");
System.out.println(update > 0 ? "成功添加一条数据!" : "对数据库未产生影响");
}
@Test
public void testUpdate(){//测试update语句
//创建GoodsDao对象
GoodsDao goodsDao = new GoodsDao();
String sql = "update goods set goods_name=? where id=?";
int update = goodsDao.update(sql, "ReadmiK40游戏增强", 8);
System.out.println(update > 0 ? "成功修改一条数据!" : "对数据库无影响!");
}
@Test
public void testDelete(){
GoodsDao goodsDao = new GoodsDao();
String sql = "delete from goods where id = ?";
int update = goodsDao.update(sql, 9);
System.out.println(update > 0 ? "成功删除一条数据!" : "对数据库无影响!");
}
@Test
public void testMany(){
//创建GoodsDao对象
GoodsDao goodsDao = new GoodsDao();
String sql = "select * from goods where id >= ?";
//使用 queryRunner.query(conn,sql , new BeanListHandler , paramaters)
//底层List query = queryRunner.query(connection, sql, new BeanListHandler(clazz), parameters);
List goods = goodsDao.queryMulti(sql, Goods.class, 1);
System.out.println("====查询结果====");
for (Goods good : goods) {
System.out.println(good);
}
}
@Test
public void testOneRow(){
//创建dao对象
GoodsDao goodsDao = new GoodsDao();
//编写sql语句
String sql = "select * from goods where id = ?";
//BasicDao执行sql语句
//底层 T query = queryRunner.query(String sql , Class , Object...paramater)
// queryRunner.query(connection , sql , new BeanHandler,paramaters)
Goods goods = goodsDao.querySingle(sql, Goods.class, 4);
System.out.println("====查询结果====");
System.out.println(goods);
}
@Test
public void testOne(){
//创建GoodsDao对象
GoodsDao goodsDao = new GoodsDao();
//编写sql语句
String sql = "select goods_name from goods where id = ?";
Object o = goodsDao.queryScalar(sql, 4);
System.out.println("====查询结果====");
System.out.println(o);
}
}



