java web 手写 Mybatis 底层框架
四步走:创建三个准备文件,一个测试文件,即可大功告成。费话不多说直接上代码,代码中有详细注释。
一、在包com.zhuoyue.db下创建名为DBManager.java的文件
package com.zhuoyue.db;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.List;
import java.util.Properties;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.zhuoyue.mapper.IMapper;
public class DBManager {
//声明一个数据库连接池对象
private static ComboPooledDataSource ds=new ComboPooledDataSource();
//四大参数
private static String driver="";
private static String url="";
private static String username="";
private static String password="";
static{
try {
InputStream in=DBManager.class.getClassLoader().getResourceAsStream("db.properties");
Properties pro=new Properties();
pro.load(in);
//通过pro的key可以 获取数据了
driver=pro.getProperty("driver");
url=pro.getProperty("url");
username=pro.getProperty("username");
password=pro.getProperty("password");
//初始化数据库连接池
ds.setDriverClass(driver);
ds.setJdbcUrl(url);
ds.setUser(username);
ds.setPassword(password);
//初始化连接数需要在最大和最小连接数之间
ds.setInitialPoolSize(10);
//最大连接数在满足应用需要的情况下,参考默认值15
ds.setMaxPoolSize(100);
} catch (Exception e) {
e.printStackTrace();
}
}
private Connection getConn() throws Exception {
Class.forName(driver);
return ds.getConnection();
}
/*
* 我用来做增删改
*/
public void exectueUpdate(String sql, Object[] params) throws Exception {
Connection conn = null;
PreparedStatement stm = null;
try {
conn = this.getConn();
//写入sql语句
stm = conn.prepareStatement(sql);
int len = params.length;
//循环遍历输入参数
for (int i = 0; i < len; i++) {
stm.setObject(i + 1, params[i]);
}
// 执行sql
stm.executeUpdate();
} catch (Exception ex) {
throw ex;
} finally {
stm.close();
conn.close();
}
}
/*
* 此处执行查询语句(第一个参数为sql语句,第二个参数为客户端传输来的参数,第三个参数为返回值类型,多态思想,继承IMapper接口)
*/
public List executeQuery(String sql,Object []params,IMapper mapper)
throws Exception{
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs =null;
try {
conn = this.getConn();
stm = conn.prepareStatement(sql);
int len = params.length;
for (int i = 0; i < len; i++) {
stm.setObject(i + 1, params[i]);
}
// 获取结果集
rs= stm.executeQuery();
List list=mapper.mapper(rs);
return list;
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
rs.close();
stm.close();
conn.close();
}
}
/*
* 聚合函数
*/
public int getCount(String sql,Object []params)
throws Exception{
Connection conn = null;
PreparedStatement stm = null;
ResultSet rs =null;
try {
conn=this.getConn();
stm=conn.prepareStatement(sql);
//设置参数
int len = params.length;
for (int i = 0; i < len; i++) {
stm.setObject(i + 1, params[i]);
}
// 获取结果集
rs= stm.executeQuery();
int num=0;
if(rs.next()){
num=rs.getInt(1);
}
return num;
} catch (Exception e) {
e.printStackTrace();
throw e;
}finally{
rs.close();
stm.close();
conn.close();
}
}
}
二、在com.zhuoyue.mapper包下创建IMapper.java接口
package com.zhuoyue.mapper;
import java.sql.ResultSet;
import java.util.List;
public interface IMapper {
public List mapper(ResultSet rs)throws Exception;
}
三、在com.zhuoyue.mapper包下创建DeptMapper.java实现IMapper接口
注:每个实体类都应该有有一个对应的***Mapper.java实现IMapper接口
package com.zhuoyue.mapper;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.zhuoyue.po.Dept;
public class DeptMapper implements IMapper {
//重写父类mapper方法
@Override
public List mapper(ResultSet rs)throws Exception{
List<Dept> list = new ArrayList<>();
while (rs.next()) {// 结果集封装成对象
// 获取内容的时候根据行中的列的下标
int deptno = rs.getInt(1);
String dname = rs.getString(2);
String loc = rs.getString(3);
Dept d = new Dept();
d.setDeptno(deptno);
d.setDname(dname);
d.setLoc(loc);
list.add(d);
}
return list;
}
}
四、dao层访问测试
1、增删改方法测试用例
package com.zhuoyue.dao;
import java.util.List;
import com.zhuoyue.db.DBManager;
import com.zhuoyue.mapper.DeptMapper;
import com.zhuoyue.po.Dept;
import com.zhuoyue.util.PageUtil;
public class DeptDAO {
//创建一个DBManager对象
private DBManager db=new DBManager();
public void save(Dept dept)
throws Exception{
//一、准备好sql语句
String sql=" insert into dept values (?,?,?) ";
//二、准备好需要的参数
Object []params={dept.getDeptno(),dept.getDname(),dept.getLoc()};
//调用DBManager中增删改方法
db.exectueUpdate(sql, params);
}
public void delete(Dept dept)
throws Exception{
String sql=" delete from dept where deptno=? ";
Object []params={dept.getDeptno()};
db.exectueUpdate(sql, params);
}
public void merge(Dept dept)
throws Exception{
String sql=" update dept set dname=?,loc=? where deptno=? ";
Object []params={dept.getDname(),dept.getLoc(),dept.getDeptno()};
db.exectueUpdate(sql, params);
}
}
2、查询测试用例
package com.zhuoyue.dao;
import java.util.List;
import com.zhuoyue.db.DBManager;
import com.zhuoyue.mapper.DeptMapper;
import com.zhuoyue.po.Dept;
import com.zhuoyue.util.PageUtil;
public class DeptDAO {
//创建一个DBManager对象
private DBManager db=new DBManager();
public List<Dept> findAll()
throws Exception{
//一、准备好sql语句
String sql="select *from dept order by deptno";
//二、准备好需要的参数
Object []params={};
//调用DBManager类中查询的方法,注意:第三个参数为步骤三创建的
return db.executeQuery(sql, params,new DeptMapper());
}
public List<Dept> findByPage(int pagenow,int pagesize)
throws Exception{
String sql="select *from dept limit ?,?";
//开始条数
int start=PageUtil.getStart(pagenow, pagesize);
Object []params={start,pagesize};
return db.executeQuery(sql, params,new DeptMapper());
}
public Dept findById(int deptno)
throws Exception{
String sql=" select *from dept where deptno=? ";
Object []params={deptno};
List<Dept> list=db.executeQuery(sql, params,new DeptMapper());//最多查询到一个 1
if(list.size()>0){
return list.get(0);
}
return null;//上层调用的时候记得判定...
}
}
3、聚合函数测试用例
package com.zhuoyue.dao;
import java.util.List;
import com.zhuoyue.db.DBManager;
import com.zhuoyue.mapper.DeptMapper;
import com.zhuoyue.po.Dept;
import com.zhuoyue.util.PageUtil;
public class DeptDAO {
//创建一个DBManager对象
private DBManager db=new DBManager();
public int getCount()
throws Exception{
//一、准备好sql语句
String sql=" select count(*) from dept ";
//二、准备好需要的参数(这里不需要参数)
Object []params={};
return db.getCount(sql, params);
}
}
以上就是对java web dao层进行的一定的封装,欢迎添加我的微信进行技术交流。