自定义MVC增删查改(控制台)
首先,我们先去把mvc打成jar包
(选中项目,点击右键,选中Export,然后看见里面有两个选项,然后选中Export,进去选中java文件,可以看见三个选项,点击JAR file,然后取名改存放路径就行了)
接下来把需要用到的jar 包全部导如项目里,就可以写了
先把前面写好的工具类全部放入util包里,写实体类,(这里我写了三个实体类,以Student为例)
public class Students {
private int sid;
private String sname;
private String tname;
private String sclass;
private String shobby;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getTname() {
return tname;
}
public void setTname(String tname) {
this.tname = tname;
}
public String getsclass() {
return sclass;
}
public void setsclass(String sclass) {
this.sclass = sclass;
}
public String getShobby() {
return shobby;
}
public void setShobby(String shobby) {
this.shobby = shobby;
}
public Students() {
super();
}
public Students(int sid, String sname, String tname, String sclass, String shobby) {
super();
this.sid = sid;
this.sname = sname;
this.tname = tname;
this.sclass = sclass;
this.shobby = shobby;
}
public Students(String sname, String tname, String sclass, String shobby) {
super();
this.sname = sname;
this.tname = tname;
this.sclass = sclass;
this.shobby = shobby;
}
}
然后写一个通用的增删改方法,写一个通用的查询方法
public class BaseDao<T> {
/**
*
* @param sql:决定查询哪张表的数据
* @param clz:查询出来的数据封装到哪个实体类中
* @param pagebean:决定是否分页
* @return
* @throws SQLException
* @throws IllegalAccessException
* @throws IllegalArgumentException
* @throws InstantiationException
*/
public List<T> executeQuery(String sql,Class clz,PageBean pageBean) throws SQLException, IllegalArgumentException, IllegalAccessException, InstantiationException{
List<T> list = new ArrayList<>();
Connection con = DBAccess.getConnection();
PreparedStatement pst = null;
ResultSet rs = null;
try {
if (pageBean != null && pageBean.isPagination()) {
//该分页了
String countSql = getcountSql(sql);//拼接sql语句用来查询一页的所有行
pst = con.prepareStatement(countSql);
rs = pst.executeQuery();
if(rs.next()) {
pageBean.setTotal(rs.getLong(1)+"");
}
//拼接sql语句 用来查询第几页的结果集
String pageSql = getPageSql(sql,pageBean);
pst = con.prepareStatement(pageSql);
rs = pst.executeQuery();
} else {//不分页
pst = con.prepareStatement(sql);
rs = pst.executeQuery();
}
while (rs.next()) {
// list.add(new Book(rs.getInt("bid"),
// rs.getString("bname"),
// rs.getFloat("price")));
/*
* 1、创建了一个Book对象
* 2、从ResultSet结果集中获取值放入Book对象属性中
* 2.1 获取到Book的属性对象
* 2.2 给属性对象赋值
* 3、将已经有值的book对象放入list集合中
*
*/
//利用反射拿到值
T t = (T) clz.newInstance();
Field[] fields = clz.getDeclaredFields();
for (Field field : fields) {
field.setAccessible(true);
field.set(t, rs.getObject(field.getName()));
}
//加入集合中
list.add(t);
}
} finally {
//shift+alt+z
//关闭连接,执行对象以及结果集
DBAccess.close(con, pst, rs);
}
return list;
}
/**
* 用原生sql拼接出符合条件的某一页的数据查询sql
* @param sql
* @param pageBean
* @return
*/
private String getPageSql(String sql, PageBean pageBean) {
return sql + " limit "+ pageBean.getStartIndex()+","+pageBean.getRows();
}
/**
* 用原生sql拼接出查询符合条件的记录数
* @param sql
* @return
*/
private String getcountSql(String sql) {
return "select count(1) from ("+sql+") t";
}
/**
* 通用的增删改方法
* @param t : 实体类的实例
* @param sql :增删改的sql语句
* @param attrs ?所代表的实体类的属性
* @return
* @throws SQLException
* @throws NoSuchFieldException
* @throws SecurityException
* @throws IllegalArgumentException
* @throws IllegalAccessException
*/
public int executeUpdate(T t,String sql,String[] attrs) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
Connection con = DBAccess.getConnection();
PreparedStatement pst = con.prepareStatement(sql);
for (int i = 0; i < attrs.length; i++) {
Field field = t.getClass().getDeclaredField(attrs[i]);
field.setAccessible(true);
pst.setObject(i+1, field.get(t));
}
return pst.executeUpdate();
}
}
再去写dao方法就行了
public class StuDao extends BaseDao<Students> {
public List<Students> lst(Students stu,PageBean pageBean) throws IllegalArgumentException, IllegalAccessException, InstantiationException, SQLException{
String sql = "select * from tb_student where 1=1";
if(StringUtils.isNotBlank(stu.getSname())) {
sql += " and sname like '%"+stu.getSname()+"%'";
}
return super.executeQuery(sql, Students.class, pageBean);
}
public int addStu(Students stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql = "insert into tb_student(sname,tname,sclass,shobby) values(?,?,?,?)";
return super.executeUpdate(stu, sql,
new String[] {"sname","tname","sclass","shobby"});
}
public int delStu(Students stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql = "delete from tb_student where sid=?";
return super.executeUpdate(stu, sql,
new String[] {"sid"});
}
public int updateStu(Students stu) throws SQLException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
String sql = "update tb_student set sname=?,tname=?,sclass=?,shobby=? where sid=?";
return super.executeUpdate(stu, sql,
new String[] {"sname","tname","sclass","shobby","sid"});
}
}
然后去junit测试就行了
(ctrl+n 输入junit找到测试就行了)
class StuDaoTest {
private StuDao stuDao = new StuDao();
private Students stu = null;
@BeforeEach
void setUp() throws Exception {
stu = new Students();
}
@AfterEach
void tearDown() throws Exception {
}
@Test
void testLst() throws IllegalArgumentException, IllegalAccessException, InstantiationException, SQLException {
List<Students> lst = this.stuDao.lst(stu, null);
System.out.println(lst.size());
}
@Test
void testAddStu() {
stu.setSname("JJ");
stu.setTname("aa");
stu.setsclass("1524");
stu.setShobby("唱歌,跳舞");
try {
this.stuDao.addStu(stu);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
e.printStackTrace();
}
}
@Test
void testDelStu() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, SQLException {
stu.setSid(3);
this.stu.setSid(3);
this.stuDao.delStu(stu);
}
@Test
void testUpdateStu() {
stu.setSid(3);
stu.setSname("ZJJ");
stu.setTname("bb");
stu.setsclass("1524");
stu.setShobby("唱歌,跳舞");
try {
this.stuDao.updateStu(stu);
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException
| SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
本人已经测试了,都可以加入数据库去,谢谢~