前言

基于前面的SSM整合配置文件,博主带大家来做一个小案例,来练练手。😁😁😁
任务描述:数据库有一张作者表(Author),这张表包含属性:id、username、password、email、bio,使用SSM整合来完成这张作者表的查询所有作者操作,其实你们也可以衍生删除、修改、增加等操作,都是大同小异~

编写Author实体类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
public class Author {
private int id;
private String username;
private String password;
private String email;
private String bio;

public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

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 getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public String getBio() {
return bio;
}

public void setBio(String bio) {
this.bio = bio;
}

public Author(int id, String username, String password, String email, String bio) {
super();
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.bio = bio;
}

public Author() {
super();
}
}

编写AuthorController类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
@Controller
@RequestMapping(value="/author")
public class AuthorController {
//@Resource:按名称装配,如果找不到与名称匹配的bean,则按类型装配
@Resource
AuthorService authorService;

public AuthorService getAuthorService() {
return authorService;
}

public void setAuthorService(AuthorService authorService) {
this.authorService = authorService;
}

//当访问getAllAuthors.action时,查询到所有的作者,然后将结果存入request,再重定向到result.jsp
@RequestMapping(value="/getAllAuthors.action")
public String getAllAuthors(HttpServletRequest request) {
List<Author> authors = authorService.getAllAuthors();
request.setAttribute("authors", authors);

return "forward:../result.jsp";
}
}

编写service层代码

  • 首先我们在service包下创建Author对应的AuthorService接口类
1
2
3
public interface AuthorService {
public List<Author> getAllAuthors();
}
  • 其次在service.impl实现包下创建对应的实现类AuthorServiceImpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@Service(value="authorService")
public class AuthorServiceImpl implements AuthorService{
@Resource
AuthorDao authorDao;

//如果用的spring配置文件进行dao注入,就得创建该dao的getter、setter方法进行注入
public AuthorDao getAuthorDao() {
return authorDao;
}

public void setAuthorDao(AuthorDao authorDao) {
this.authorDao = authorDao;
}

@Override
public List<Author> getAllAuthors() {
return authorDao.findAll();
}
}

编写dao层代码

  • 首先在dao包下面创建对应的Author的Dao接口AuthorDao
1
2
3
public interface AuthorDao {
public List<Author> findAll();
}
  • 再次在dao包下创建于AuthorDao接口名字一样的xml文件,这就是代理模式的体现
1
2
3
4
5
6
7
8
9
10
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="dao.AuthorDao">
<select id="findAll" resultType="entity.Author">
select * from Author
</select>
</mapper>

最后编写测试代码

我创建了两个jsp文件(一个用于点击访问action,一个用于显示结果)

  • 实现action的jsp
1
2
3
4
5
6
7
8
9
10
11
12
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>index</title>
</head>
<body>
<a href="author/getAllAuthors.action">findAll</a>
</body>
</html>
  • 实现显示结果的jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<%@page import="entity.Author"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>result</title>
</head>
<body>
<table border="1">
<tr>
<td>ID</td>
<td>用户名</td>
<td>密码</td>
<td>邮箱</td>
<td>bio</td>
</tr>
<c:forEach items="${authors }" var="author">
<tr>
<td>${author.id }</td>
<td>${author.username }</td>
<td>${author.password }</td>
<td>${author.email }</td>
<td>${author.bio }</td>
</tr>
</c:forEach>
</table>
</body>
</html>

成功运行结果

ps:因作者能力有限,有错误的地方请见谅

  • 喜欢这篇文章的话可以用快捷键 Ctrl + D 来收藏本页

最后更新: 2018年09月19日 15:54

原始链接: https://blog.hdqyf.club/2018/04/18/20180418-SSM集成应用—SSM整合案例/

× 请我吃糖~
打赏二维码