学习内容:
1.实现页面的渲染(从数据库拿数据去渲染页面,动态的可修改)
2.实现一个简单的登录功能
3.实现对新闻详情的评论
4.评论的时候可以上传照片
5.本次使用的是mysql+mvc+控制层+过滤器+js文件下的配置文件+webapp下的jsp文件+测试类
6.开发工具IDEA
- domian层防止了用户+新闻类型+新闻详情+评论 对应MySQL中的四张表
- dao下进行了数据交互
- service进行了服务控制
- controller是控制层
- filter是过滤器
domain下的四个类:
package com.csi.news.domain;
import java.io.Serializable;
public class UserInfo implements Serializable {
private Integer userId ;
private String username ;
private String password ;
private String sex ;
public Integer getUserId() {
return userId;
}
public void setUserId(Integer userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
package com.csi.news.domain;
import java.io.Serializable;
public class NewsType implements Serializable {
private Integer typeid ;
private String name ;
private String illustrate ;
private String img ;
public Integer getTypeid() {
return typeid;
}
public void setTypeid(Integer typeid) {
this.typeid = typeid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getIllustrate() {
return illustrate;
}
public void setIllustrate(String illustrate) {
this.illustrate = illustrate;
}
public String getImg() {
return img;
}
public void setImg(String img) {
this.img = img;
}
}
package com.csi.news.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.Set;
public class NewsInfo implements Serializable {
private Integer newsId;
private String title;
private Date time;
private String source;
private String context;
private String edit;
private NewsType newsType ;
private Set<NewsComments> newsComments ;
public Set<NewsComments> getNewsComments() {
return newsComments;
}
public void setNewsComments(Set<NewsComments> newsComments) {
this.newsComments = newsComments;
}
public NewsType getNewsType() {
return newsType;
}
public void setNewsType(NewsType newsType) {
this.newsType = newsType;
}
public Integer getNewsId() {
return newsId;
}
public void setNewsId(Integer newsId) {
this.newsId = newsId;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getEdit() {
return edit;
}
public void setEdit(String edit) {
this.edit = edit;
}
}
dao层下的接口和类
package com.csi.news.dao.impl;
import com.csi.news.dao.UserInfoDao;
import com.csi.news.domain.UserInfo;
import com.csi.news.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserInfoDaoImpl extends JDBCUtils implements UserInfoDao {
@Override
public UserInfo login(String username, String password) throws SQLException {
final String SQL = "SELECT * FROM user WHERE username = ? AND password = ?" ;
//2. 创建JDBC连接
Connection connection = this.getConnection();
//3. 创建PreparedStatement对象
PreparedStatement ps = connection.prepareStatement(SQL);
//3.1 设置占位符
ps.setString(1,username);
ps.setString(2,password);
//4. 发起查询,返回ResultSet
ResultSet rs = ps.executeQuery() ;
UserInfo userInfo = null ;
//5. 判断结果集是否存在数据
if(rs.next()) {
//6. 如果存在数据,封装对象,返回对象
userInfo = new UserInfo() ;
userInfo.setUserId(rs.getInt("UserId"));
userInfo.setUsername(rs.getString("username"));
userInfo.setSex(rs.getString("sex"));
}
//7. 关闭资源
this.release(rs,ps,connection);
return userInfo;
}
@Override
public UserInfo findById(int userId) throws SQLException {
final String SQL = "select * from user where userId = ?" ;
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(SQL);
ps.setInt(1,userId);
ResultSet rs = ps.executeQuery();
UserInfo userInfo = null ;
if (rs.next()){
userInfo = new UserInfo() ;
userInfo.setUserId(rs.getInt("UserId"));
userInfo.setUsername(rs.getString("Username"));
userInfo.setSex(rs.getString("Sex"));
userInfo.setPassword(rs.getString("Password"));
}
this.release(rs,ps,connection);
return userInfo;
}
}
package com.csi.news.dao.impl;
import com.csi.news.dao.NewsDao;
import com.csi.news.domain.NewsType;
import com.csi.news.utils.JDBCUtils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class NewsDaoImpl extends JDBCUtils implements NewsDao {
@Override
public List<NewsType> findShow() throws SQLException {
final String SQL = "SELECT * from `news_type`";
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(SQL);
ResultSet rs = ps.executeQuery();
List<NewsType> news = new ArrayList<>();
NewsType news1 = null;
while (rs.next()) {
news1 = new NewsType();
news1.setTypeid(rs.getInt("Type_id"));
news1.setName(rs.getString("name"));
news1.setIllustrate(rs.getString("illustrate"));
news1.setImg(rs.getString("img"));
news.add(news1);
}
release(rs, ps, connection);
return news;
}
@Override
public NewsType select(int type_id) throws SQLException {
final String SQL = "SELECT * from `news_type` WHERE type_id= ?";
Connection connection = this.getConnection();
PreparedStatement ps = connection.prepareStatement(SQL);
ps.setInt(1, type_id);
ResultSet rs = ps.executeQuery();
NewsType news = null;
if (rs.next()) {
news = new NewsType();
news.setTypeid(rs.getInt("type_id"));
news.setName(rs.getString("name"));
news.setIllustrate(rs.getString("illustrate"));
news.setImg(rs.getString("img"));
}
release(rs, ps, connection);
return news;
}
}
package com.csi.news.dao.impl;
import co