(7) 小项目
给个项目的文件排版图片(你可以直接模仿我的结构)
我认真讲每个包的作用,并且给你完整的代码(如果按照我的排版,你不需要更改代码)
Member.java:实体类
这个是对应数据库的列,实际上可以说是,通过操作实体类,操作数据库的数据
代码:
1 package edu.uc.bean; 2 3 public class Member implements java.io.Serializable { 4 5 /** 6 * 7 */ 8 private static final long serialVersionUID = 1L; 9 10 private long userId; 11 private String userName; 12 private String userPass; 13 private String nickName; 14 private String email; 15 private String mobile; 16 private String myId; 17 private String myIdkey; 18 private String regIp; 19 private java.util.Date regDate; 20 private String lastLoginIp; 21 private java.util.Date lastLoginTime; 22 private String salt; 23 private String secques; 24 private String status; 25 private String remark; 26 private Long sortNum; 27 28 29 30 public String getRemark() { 31 return remark; 32 } 33 34 public void setRemark(String remark) { 35 this.remark = remark; 36 } 37 38 public Long getSortNum() { 39 return sortNum; 40 } 41 public void setSortNum(Long sortNum) { 42 this.sortNum = sortNum; 43 } 44 45 private Long isDeleted; 46 private Long createBy; 47 private Long updateBy; 48 private java.util.Date createOn; 49 private java.util.Date updateOn; 50 public long getUserId() { 51 return userId; 52 } 53 public void setUserId(long userId) { 54 this.userId = userId; 55 } 56 public String getUserName() { 57 return userName; 58 } 59 public void setUserName(String userName) { 60 this.userName = userName; 61 } 62 public String getUserPass() { 63 return userPass; 64 } 65 public void setUserPass(String userPass) { 66 this.userPass = userPass; 67 } 68 public String getNickName() { 69 return nickName; 70 } 71 public void setNickName(String nickName) { 72 this.nickName = nickName; 73 } 74 public String getEmail() { 75 return email; 76 } 77 public void setEmail(String email) { 78 this.email = email; 79 } 80 public String getMobile() { 81 return mobile; 82 } 83 public void setMobile(String mobile) { 84 this.mobile = mobile; 85 } 86 public String getMyId() { 87 return myId; 88 } 89 public void setMyId(String myId) { 90 this.myId = myId; 91 } 92 public String getMyIdkey() { 93 return myIdkey; 94 } 95 public void setMyIdkey(String myIdkey) { 96 this.myIdkey = myIdkey; 97 } 98 public String getRegIp() { 99 return regIp; 100 } 101 public void setRegIp(String regIp) { 102 this.regIp = regIp; 103 } 104 public java.util.Date getRegDate() { 105 return regDate; 106 } 107 public void setRegDate(java.util.Date regDate) { 108 this.regDate = regDate; 109 } 110 public String getLastLoginIp() { 111 return lastLoginIp; 112 } 113 public void setLastLoginIp(String lastLoginIp) { 114 this.lastLoginIp = lastLoginIp; 115 } 116 public java.util.Date getLastLoginTime() { 117 return lastLoginTime; 118 } 119 public void setLastLoginTime(java.util.Date lastLoginTime) { 120 this.lastLoginTime = lastLoginTime; 121 } 122 public String getSalt() { 123 return salt; 124 } 125 public void setSalt(String salt) { 126 this.salt = salt; 127 } 128 public String getSecques() { 129 return secques; 130 } 131 public void setSecques(String secques) { 132 this.secques = secques; 133 } 134 public String getStatus() { 135 return status; 136 } 137 public void setStatus(String status) { 138 this.status = status; 139 } 140 public Long getIsDeleted() { 141 return isDeleted; 142 } 143 public void setIsDeleted(Long isDeleted) { 144 this.isDeleted = isDeleted; 145 } 146 public Long getCreateBy() { 147 return createBy; 148 } 149 public void setCreateBy(Long createBy) { 150 this.createBy = createBy; 151 } 152 public Long getUpdateBy() { 153 return updateBy; 154 } 155 public void setUpdateBy(Long updateBy) { 156 this.updateBy = updateBy; 157 } 158 public java.util.Date getCreateOn() { 159 return createOn; 160 } 161 public void setCreateOn(java.util.Date createOn) { 162 this.createOn = createOn; 163 } 164 public java.util.Date getUpdateOn() { 165 return updateOn; 166 } 167 public void setUpdateOn(java.util.Date updateOn) { 168 this.updateOn = updateOn; 169 } 170 171 172 173 174 }
MemberDao.java: dao层(接口)
主要是各各操作的定义,比如你需要一个更新操作,可以update()方法
代码:
1 package edu.uc.dao; 2 3 import java.util.*; 4 5 import edu.uc.bean.Member; 6 7 public interface MemberDao { 8 9 Long delete(Long id);//删除操作,返回是Long类型 10 11 Member load(Long id);//根据id查询操作 12 13 Long update(Member bean);//更新操作,返回Long类型 14 15 Long insert(Member bean);//添加操作 16 17 18 Member loadByName(String name);//通过名字查询 19 20 21 List<Member> list();//输出数据 22 23 List<Member> listByName(String name);//通过名字查询,输出数据 24 25 }
MemberDaoImpl.java:就是实现上面接口里面的方法,具体的实现操作和逻辑
里面包括连接数据库的操作和sql语言的查询数据库等
代码:
1 package edu.uc.dao.impl; 2 import java.sql.*; 3 import java.util.*; 4 import com.liuvei.common.DbFun; 5 import edu.uc.bean.*; 6 import edu.uc.dao.*; 7 import edu.uc.util.*; 8 public class MemberDaoImpl implements MemberDao { 9 10 public Member load(Long id) { 11 Member bean = null; 12 // 0.定义变量 13 StringBuffer sbSQL = new StringBuffer(); 14 15 List<Object> paramsList = new ArrayList<Object>(); 16 17 // 1.组合SQL 18 sbSQL.append(" select * from Member"); 19 sbSQL.append(" where userId = ?"); 20 sbSQL.append(" order by userId asc"); 21 22 // 2.添加参数 23 // paramsList.add(id); 24 paramsList.add(id); 25 26 // 3.转换类型 27 String sql = sbSQL.toString(); 28 Object[] params = paramsList.toArray(); 29 30 Connection conn = null; 31 ResultSet rs = null; 32 try { 33 // 4.取得连接对象 34 conn = DbUtil.getConn(); 35 36 // 5.执行sql 37 rs = DbFun.query(conn, sql, params); 38 39 // 6) 多行转为对象列表 40 if (rs.next()) { 41 bean = toBean(rs); 42 } 43 } catch (SQLException e) { 44 // TODO: handle exception 45 e.printStackTrace(); 46 throw new RuntimeException(e); 47 } finally { 48 49 50 DbUtil.close(conn); 51 } 52 return bean; 53 } 54 55 56 @Override 57 public Member loadByName(String name) { 58 Member bean = null; 59 // 0.定义变量 60 StringBuffer sbSQL = new StringBuffer(); 61 List<Object> paramsList = new ArrayList<Object>(); 62 63 // 1.组合SQL 64 sbSQL.append(" select * from Member"); 65 sbSQL.append(" where userName = ?"); 66 sbSQL.append(" order by userId asc"); 67 // 2.添加参数 68 // paramsList.add(id); 69 paramsList.add(name); 70 71 // 3.转换类型 72 String sql = sbSQL.toString(); 73 Object[] params = paramsList.toArray(); 74 75 Connection conn = null; 76 ResultSet rs = null; 77 try { 78 // 4.取得连接对象 79 conn = DbUtil.getConn(); 80 81 // 5.执行sql 82 rs = DbFun.query(conn, sql, params); 83 84 // 6. 多行转为对象列表 85 if (rs.next()) { 86 bean = toBean(rs); 87 } 88 } catch (SQLException e) { 89 // TODO: handle exception 90 e.printStackTrace(); 91 throw new RuntimeException(e); 92 } finally { 93 DbUtil.close(conn); 94 } 95 return bean; 96 } 97 98 99 @Override 100 public List<Member> list() { 101 // TODO Auto-generated method stub 102 List<Member> list = new ArrayList<>(); 103 104 // 0.定义变量 105 StringBuffer sbSQL = new StringBuffer(); 106 List<Object> paramsList = new ArrayList<Object>(); 107 108 // 1.组合SQL 109 sbSQL.append(" select * from Member"); 110 sbSQL.append(" order by userId asc"); 111 // 2.添加参数 112 // paramsList.add(id); 113 114 115 // 3.转换类型 116 String sql = sbSQL.toString(); 117 Object[] params = paramsList.toArray(); 118 119 Connection conn = null; 120 ResultSet rs = null; 121 try { 122 // 4.取得连接对象 123 conn = DbUtil.getConn(); 124 125 // 5.执行sql 126 rs = DbFun.query(conn, sql, params); 127 128 // 6) 多行转为对象列表 129 Member bean = null; 130 while (rs.next()) { 131 bean = toBean(rs); 132 133 list.add(bean); 134 } 135 } catch (SQLException e) { 136 // TODO: handle exception 137 e.printStackTrace(); 138 throw new RuntimeException(e); 139 } finally { 140 DbUtil.close(conn); 141 } 142 return list; 143 } 144 145 @Override 146 public List<Member> listByName(String name) { 147 // TODO Auto-generated method stub 148 List<Member> list = new ArrayList<>(); 149 150 // 0.定义变量 151 StringBuffer sbSQL = new StringBuffer(); 152 List<Object> paramsList = new ArrayList<Object>(); 153 154 // 1.组合SQL 155 sbSQL.append(" select * from Member"); 156 sbSQL.append(" where userName like ?"); 157 sbSQL.append(" order by userId asc"); 158 // 2.添加参数 159 paramsList.add("%"+name+"%"); 160 161 162 // 3.转换类型 163 String sql = sbSQL.toString(); 164 Object[] params = paramsList.toArray(); 165 166 Connection conn = null; 167 ResultSet rs = null; 168 try { 169 // 4.取得连接对象 170 conn = DbUtil.getConn(); 171 172 // 5.执行sql 173 rs = DbFun.query(conn, sql, params); 174 175 // 6) 多行转为对象列表 176 Member bean = null; 177 while (rs.next()) { 178 bean = toBean(rs); 179 180 list.add(bean); 181 } 182 } catch (SQLException e) { 183 // TODO: handle exception 184 e.printStackTrace(); 185 throw new RuntimeException(e); 186 } finally { 187 DbUtil.close(conn); 188 } 189 return list; 190 } 191 192 193 194 private Member toBean(ResultSet rs) { 195 Member bean = new Member(); 196 try { 197 bean.setUserId(rs.getLong("userId")); 198 bean.setUserName(rs.getString("userName")); 199 200 bean.setUserPass(rs.getString("userPass")); 201 bean.setNickName(rs.getString("nickName")); 202 bean.setEmail(rs.getString("email")); 203 bean.setMobile(rs.getString("Mobile")); 204 205 bean.setMyId(rs.getString("myId")); 206 bean.setMyIdkey(rs.getString("myIdKey")); 207 bean.setRegIp(rs.getString("regIp")); 208 bean.setRegDate(rs.getDate("regDate")); 209 210 bean.setLastLoginIp(rs.getString("lastLoginIp")); 211 bean.setLastLoginTime(rs.getDate("lastLoginTime")); 212 bean.setSalt(rs.getString("salt")); 213 bean.setSecques(rs.getString("secques")); 214 215 bean.setRemark(rs.getString("remark")); 216 bean.setSortNum(rs.getLong("sortNum")); 217 bean.setStatus(rs.getString("status")); 218 bean.setIsDeleted(rs.getLong("isDeleted")); 219 220 bean.setCreateBy(rs.getLong("createBy")); 221 bean.setUpdateBy(rs.getLong("updateBy")); 222 bean.setCreateOn(rs.getDate("createOn")); 223 bean.setUpdateOn(rs.getDate("updateOn")); 224 225 } catch (SQLException e) { 226 // TODO Auto-generated catch block 227 e.printStackTrace(); 228 229 throw new RuntimeException(e); 230 } 231 return bean; 232 } 233 234 235 @Override 236 public Long insert(Member bean) { 237 // TODO Auto-generated method stub 238 Long result = 0L; 239 // 0.定义变量 240 StringBuffer sbSQL = new StringBuffer(); 241 List<Object> paramsList = new ArrayList<Object>(); 242 243 // 1.组合SQL 244 sbSQL.append(" Insert Into Member"); 245 sbSQL.append(" ("); 246 sbSQL.append(" userName,userPass,nickName,email"); 247 sbSQL.append(" ,myId,myIdKey,regIp,regDate"); 248 sbSQL.append(" ,lastLoginIp,lastLoginTime,salt,secques"); 249 sbSQL.append(" ,remark,sortNum,status,isDeleted"); 250 sbSQL.append(" ,createBy,updateBy,createOn,updateOn"); 251 sbSQL.append(" )"); 252 sbSQL.append(" values(?,?,?,? ,?,?,?,? ,?,?,?,? ,?,?,?,? ,?,?,?,?)"); 253 // 2.添加参数 254 paramsList.add(bean.getUserName()); 255 paramsList.add(bean.getUserPass()); 256 paramsList.add(bean.getNickName()); 257 paramsList.add(bean.getEmail()); 258 259 paramsList.add(bean.getMyId()); 260 paramsList.add(bean.getMyIdkey()); 261 paramsList.add(bean.getRegIp()); 262 paramsList.add(bean.getRegDate()); 263 264 paramsList.add(bean.getLastLoginIp()); 265 paramsList.add(bean.getLastLoginTime()); 266 paramsList.add(bean.getSalt()); 267 paramsList.add(bean.getSecques()); 268 269 paramsList.add(bean.getRemark()); 270 paramsList.add(bean.getSortNum()); 271 paramsList.add(bean.getStatus()); 272 paramsList.add(bean.getIsDeleted()); 273 274 paramsList.add(bean.getCreateBy()); 275 paramsList.add(bean.getUpdateBy()); 276 paramsList.add(bean.getCreateOn()); 277 paramsList.add(bean.getUpdateOn()); 278 // 3.转换类型 279 String sql = sbSQL.toString(); 280 Object[] params = paramsList.toArray(); 281 282 Connection conn = null; 283 284 try { 285 // 4.取得连接对象 286 conn = DbUtil.getConn(); 287 288 // 5.执行sql 289 result = DbFun.update(conn, sql, params); 290 291 292 } catch (Exception e) { 293 // TODO: handle exception 294 e.printStackTrace(); 295 throw new RuntimeException(e); 296 } finally { 297 298 299 DbUtil.close(conn); 300 } 301 return result; 302 303 } 304 305 306 @Override 307 public Long update(Member bean) { 308 // TODO Auto-generated method stub 309 Long result = 0L; 310 // 0.定义变量 311 StringBuffer sbSQL = new StringBuffer(); 312 List<Object> paramsList = new ArrayList<Object>(); 313 314 // 1.组合SQL 315 sbSQL.append(" Update Member "); 316 sbSQL.append(" set"); 317 sbSQL.append(" userName=?,userPass=?,nickName=?,email=?"); 318 sbSQL.append(" ,myId=?,myIdKey=?,regIp=?,regDate=?"); 319 sbSQL.append(" ,lastLoginIp=?,lastLoginTime=?,salt=?,secques=?"); 320 sbSQL.append(" ,remark=?,sortNum=?,status=?,isDeleted=?"); 321 sbSQL.append(" ,createBy=?,updateBy=?,createOn=?,updateOn=?"); 322 sbSQL.append(" where userId=?"); 323 // 2.添加参数 324 paramsList.add(bean.getUserName()); 325 paramsList.add(bean.getUserPass()); 326 paramsList.add(bean.getNickName()); 327 paramsList.add(bean.getEmail()); 328 329 paramsList.add(bean.getMyId()); 330 paramsList.add(bean.getMyIdkey()); 331 paramsList.add(bean.getRegIp()); 332 paramsList.add(bean.getRegDate()); 333 334 paramsList.add(bean.getLastLoginIp()); 335 paramsList.add(bean.getLastLoginTime()); 336 paramsList.add(bean.getSalt()); 337 paramsList.add(bean.getSecques()); 338 339 paramsList.add(bean.getRemark()); 340 paramsList.add(bean.getSortNum()); 341 paramsList.add(bean.getStatus()); 342 paramsList.add(bean.getIsDeleted()); 343 344 paramsList.add(bean.getCreateBy()); 345 paramsList.add(bean.getUpdateBy()); 346 paramsList.add(bean.getCreateOn()); 347 paramsList.add(bean.getUpdateOn()); 348 349 paramsList.add(bean.getUserId()); 350 // 3.转换类型 351 String sql = sbSQL.toString(); 352 Object[] params = paramsList.toArray(); 353 354 Connection conn = null; 355 356 try { 357 // 4.取得连接对象 358 conn = DbUtil.getConn(); 359 360 // 5.执行sql 361 result = DbFun.update(conn, sql, params); 362 363 364 } catch (Exception e) { 365 // TODO: handle exception 366 e.printStackTrace(); 367 throw new RuntimeException(e); 368 } finally { 369 370 371 DbUtil.close(conn); 372 } 373 return result; 374 } 375 376 @Override 377 public Long delete(Long id) { 378 // TODO Auto-generated method stub 379 Long result = 0L; 380 // 0.定义变量 381 StringBuffer sbSQL = new StringBuffer(); 382 List<Object> paramsList = new ArrayList<Object>(); 383 384 // 1.组合SQL 385 sbSQL.append(" Delete From Member "); 386 sbSQL.append(" where userId=?"); 387 // 2.添加参数 388 paramsList.add(id); 389 // 3.转换类型 390 String sql = sbSQL.toString(); 391 Object[] params = paramsList.toArray(); 392 393 Connection conn = null; 394 395 try { 396 // 4.取得连接对象 397 conn = DbUtil.getConn(); 398 399 // 5.执行sql 400 result = DbFun.update(conn, sql, params); 401 402 403 } catch (Exception e) { 404 // TODO: handle exception 405 e.printStackTrace(); 406 throw new RuntimeException(e); 407 } finally { 408 409 410 DbUtil.close(conn); 411 } 412 return result; 413 414 } 415 }
MemberService.java
可以看做高一层的dao层
代码:
1 package edu.uc.service; 2 3 import java.util.List; 4 5 import edu.uc.bean.Member; 6 7 public interface MemberService { 8 9 Long delete(Long id); 10 11 Member load(Long id); 12 13 Long update(Member bean); 14 15 Long insert(Member bean); 16 17 Member loadByName(String name); 18 19 List<Member> list(); 20 21 List<Member> listByName(String name); 22 23 }
MemberServiceImpl
通过调用dao层实现操作的组合
代码:
1 package edu.uc.service.impl; 2 3 import java.util.List; 4 5 import edu.uc.bean.Member; 6 import edu.uc.dao.MemberDao; 7 import edu.uc.dao.impl.MemberDaoImpl; 8 import edu.uc.service.MemberService; 9 10 public class MemberServiceImpl implements MemberService{ 11 12 13 private MemberDao memberDao = new MemberDaoImpl(); 14 15 @Override 16 public Member loadByName(String name) { 17 // TODO Auto-generated method stub 18 return memberDao.loadByName(name); 19 } 20 21 @Override 22 public List<Member> list() { 23 // TODO Auto-generated method stub 24 return memberDao.list(); 25 } 26 27 @Override 28 public List<Member> listByName(String name) { 29 // TODO Auto-generated method stub 30 return memberDao.listByName(name); 31 } 32 33 @Override 34 public Long insert(Member bean) { 35 // TODO Auto-generated method stub 36 return memberDao.insert(bean); 37 } 38 39 @Override 40 public Member load(Long id) { 41 // TODO Auto-generated method stub 42 return memberDao.load(id); 43 } 44 45 @Override 46 public Long update(Member bean) { 47 // TODO Auto-generated method stub 48 return memberDao.update(bean); 49 } 50 51 @Override 52 public Long delete(Long id) { 53 // TODO Auto-generated method stub 54 return memberDao.delete(id); 55 } 56 57 }
上面的是实现底层的操作基本好了,明天实现的是界面
我现在给你个视频,关于我项目运行的演示:
难搞由于不会,所以对不起了各位,有问题可以说,
会当凌绝顶,一览众山小