DAO: data access object
数据访问对象层: 主要进行增删改查操作。
什么是ORM
ORM,即Object Relational Mapping,它是对象关系模型的简称。它的作用是在关系型数据库和对象之间作一个映射。使程序能够通过操纵描述对象方式来操纵数据库。
MVC 三层架构
DAO模式的组成:
- DAO接口
- DAO实现类
- 实体类的包名:(一般为):entity、models、pojo、vo
- 数据库封装工具类
针对于任何表
- 通用的增删改方法
- 通用的查询单条记录方法
- 通用的查询多条记录方法
- 通用的查询单个值方法
泛型方法设计
JAVABeans类/实体类(entity)
- 无参构造
- 有参构造
- 属性私有
- get/set方法
- toString()方法
- 实现serializable接口
封装BasicDAO
DAO(举例)
-
Dao接口
package com.miyon.dao; import com.miyon.entity.Region; import java.util.List; //定义一个dao接口 public interface RegionDao { //增加 void add(); //修改 void modify(); //删除 void delete(); //查询 List<Region> findAll(); //用数组查询 void arrayCheck(); }
-
实体类(entity)
package com.miyon.entity; public class Region { private int id; private String name; @Override public String toString() { return "Region{" + "id=" + id + ", name='" + name + '\'' + '}'; } public Region() { } public Region(int id, String name) { this.id = id; this.name = name; } public void setId(int id) { this.id = id; } public void setName(String name) { this.name = name; } public int getId() { return id; } public String getName() { return name; } }
-
Dao实现类
package com.miyon.dao.impl;
import com.miyon.dao.RegionDao;
import com.miyon.entity.Region;
import com.miyon.utils.JDBCUtilsByDruid;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.util.Arrays;
import java.util.List;
public class RegionDaoImpl extends JDBCUtilsByDruid implements RegionDao {
//添加
@Override
public void add() {
try {
getConnection();
QueryRunner qr = new QueryRunner();
String sql = "insert into region values(?,?)";
int i = qr.update(getConnection(), sql, null, "nan极");
// System.out.println("受影响的行数为:"+i);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
JDBCUtilsByDruid.close(null,null,getConnection());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
//修改
@Override
public void modify() {
try {
getConnection();
QueryRunner qr = new QueryRunner();
String sql = "update region set name = ? where id = ?";
int i = qr.update(getConnection(), sql, "北极洲", 3);
// System.out.println("受影响的行数为:"+i);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
JDBCUtilsByDruid.close(null,null,getConnection());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
//删除
@Override
public void delete() {
try {
getConnection();
QueryRunner qr = new QueryRunner();
String sql = "delete from region where name like ?";
qr.update(getConnection(),sql,"%nan%");
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
JDBCUtilsByDruid.close(null,null,getConnection());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
//集合方式查询
@Override
public List<Region> findAll() {
List<Region> list = null;
try {
getConnection();
QueryRunner qr = new QueryRunner();
String sql = "select * from region";
list = qr.query(getConnection(), sql, new BeanListHandler<Region>(Region.class));
} catch (Exception e) {
throw new RuntimeException(e);
// } finally {
// try {
// JDBCUtilsByDruid.close(null,null,getConnection());
// } catch (Exception e) {
// throw new RuntimeException(e);
// }
}
return list;
}
//数组方式查询 : 适用于查询单条记录的情况 new ArrayHandler
public void arrayCheck(){
try {
getConnection();
QueryRunner qr = new QueryRunner();
String sql = "select * from men";
//返回值是数组的情况,最好用在查询结果唯一情况下,因为只能返回一条查询结果的数组。
Object[] arr = qr.query(getConnection(), sql, new ArrayHandler());
//数组-->集合 的方法 : Arrays.asList();
List<Object> list = Arrays.asList(arr);
list.forEach(System.out::println);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
JDBCUtilsByDruid.close(null,null,getConnection());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}
-
数据库封装工具类
package com.miyon.utils; import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.util.Properties; /** * 封装JDBCUtils * 通过Druid数据库连接池获取连接对象 */ public class JDBCUtilsByDruid { static DataSource ds; static { try { Properties properties = new Properties(); properties.load(new FileInputStream("src\\druid.properties")); //1.创建一个指定参数的数据库连接池 ds = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } //创建连接 public static Connection getConnection() throws Exception{ //2.从数据库连接池中获取可用的连接对象 // Connection connection = ds.getConnection(); return ds.getConnection(); } /** * 功能:释放资源 * * @param set * @param statement * @param connection * @throws Exception */ //关闭连接 public static void close(ResultSet set, PreparedStatement statement, Connection connection) throws Exception { if (set != null) { set.close(); } if (statement != null) { statement.close(); } if (connection != null) { connection.close(); } } }
//将String类型转为int类型
int a = Integer.parseInt(str);