JAVAEE第六次作业-SpringMVC+Spring+mybatis 项目实践
注:由于对SSM框架很陌生,配置文件部分自己弄了很久也不清楚,配置文件部分内容参考了罗付强同学的博客https://www.cnblogs.com/lfq4588/p/13094221.html
一、任务
使用ssm重新开发计科院新闻网站
二、创建SSM框架环境
2.1 新建Maven项目
方便对需要用的包进行管理
Maven导入需要用到的包
junit
junit
4.12
mysql
mysql-connector-java
5.1.47
com.mchange
c3p0
0.9.5.2
javax.servlet
servlet-api
2.5
javax.servlet.jsp
jsp-api
2.2
javax.servlet
jstl
1.2
org.mybatis
mybatis
3.5.2
org.mybatis
mybatis-spring
2.0.2
org.springframework
spring-webmvc
5.1.9.RELEASE
org.springframework
spring-jdbc
5.1.9.RELEASE
2.2 添加Web环境支持
2.3 创建SSM项目文件结构
- Controller:用于将service的结果展示在前端页面
- mapper:mabatis映射接口,编写SQL语句
- pojo:实体类
- service:业务层,和Controller进行交互
2.4 编写配置文件代码
- applicationContext.xml
- springmvc.xml
三、完成SSM项目代码
编写代码之前,可以将MVC Model2中的Web资源拷贝到该项目,代码部分也可以简单修改转换为ssm的形式
3.1 pojo实体类编写
此处用到了lombok工具,实体类就不用编写复杂的get set方法了
//用户实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class UserInfo {
String uName=""; //用户名
String uPwd=""; //用户密码
}
//新闻实体类
@Data
@AllArgsConstructor
@NoArgsConstructor
public class News {
private String newsId;
private String title;
private String newsContent;
private String author;
private String newsDate;
}
3.2 编写mapper数据库映射
- UserMapper
@Repository
public interface UserMapper {
//判断当前账号是否正确
@Select("select * from users_info where UID = #{UID} AND UPWD = #{UPWD}")
UserInfo isUserExist(UserInfo userInfo);
}
- NewsMapper
@Repository
public interface NewsMapper {
//增加新闻
@Insert("insert into users.news (NewID,Title, NewsContent, Author, NewsDate)\n" +
" values (#{NewID},#{Title},#{NewsContent},#{Author},#{NewsDate});")
int addNews(News news);
//删除新闻
@Delete(" delete from users.news where NewID = #{NewID};")
int deleteNewsById(@Param("NewID") String newId);
//修改新闻
@Update("update users.news\n" +
"set Title=#{Title},NewsContent=#{NewsContent},Author=#{Author},NewsDate=#{NewsDate}\n" +
"where NewID = #{NewID};")
int updateNewsById(News news);
//查询一条新闻
@Select("select * from users.news where NewID = #{NewID};")
News queryNewsById(@Param("NewID") String newId);
//查询所有新闻
@Select("select * from users.news;")
List queryAllNews();
}
3.3 编写service的实现
- NewsServiceImpl
@Service("NewsService")
public class NewsServiceImpl implements NewsService {
//service调dao层 组合mapper
@Autowired
private NewsMapper newsMapper;
public void setNewsMapper(NewsMapper newsMapper) {
//可增加其他代码
this.newsMapper = newsMapper;
}
public boolean addNews(News news) {
int rows = newsMapper.addNews(news);
if (rows > 0) {
return true;
} else {
return false;
}
}
public boolean deleteNewsById(String newId) {
int rows = newsMapper.deleteNewsById(newId);
if (rows > 0) {
return true;
} else {
return false;
}
}
public boolean updateNewsById(News news) {
int rows = newsMapper.updateNewsById(news);
if (rows > 0) {
return true;
} else {
return false;
}
}
public News queryNewsById(String newId) {
return newsMapper.queryNewsById(newId);
}
public List queryAllNews() {
return newsMapper.queryAllNews();
}
}
- UserServiceImpl
@Service("UserService")
public class UserServiceImpl implements UserService {
//service调dao层 组合mapper
@Autowired
private UserMapper userMapper;
public void setUserMapper(UserMapper userMapper) {
this.userMapper = userMapper;
}
//账号结果是否正确
public boolean isUserExist(UserInfo userInfo) {
UserInfo quertRes = userMapper.isUserExist(userInfo);
if (quertRes == null) {
return false;
}
if (quertRes.getUID() != null && quertRes.getUPWD() != null) {
return true;
} else {
return false;
}
}
}
3.4 编写controller
- UserController
@Controller
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/login")
public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
String username = request.getParameter("username");
String password = request.getParameter("password");
UserInfo user = new UserInfo();
user.setUID(username);
user.setUPWD(password);
boolean b = userService.isUserExist(user);
if (b) {
response.sendRedirect("page/index.html");
} else {
response.sendRedirect("index.jsp");
}
}
}
- NewsController(由于内容太多,只展示部分)
@RequestMapping("/viewNew")
public void viewNew(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String newsID = request.getParameter("newid");
System.out.println(newsID);
News news = null;
news = newsService.queryNewsById(newsID);
System.out.println(news.getNewID());
System.out.println(news.getNewsContent());
System.out.println(news.getNewsDate());
System.out.println(news.getAuthor());
request.setAttribute("news", news);
//转发到content.jsp
request.getRequestDispatcher("NewsContent.jsp").forward(request, response);
}
四、效果实现
效果与MVC Model2作业类似,但是SSM实现起来更方便了
- 删除编号为7的新闻
成功实现删除功能
- 添加新闻,点击左上方按钮即可
- 跳转到新闻管理表单
- 点写完内容点击更新
- 成功添加新闻
- 点击编辑按钮开始修改新闻内容
- 修改部分内容
成功完成修改
- 点击查看新闻即可跳转到新闻详细界面中
- 官网效果实现
跳转后页面
五、码云地址
码云地址: https://gitee.com/Jason98/JavaEE-Course