# Spring_test
**Repository Path**: wangkaij/spring_test
## Basic Information
- **Project Name**: Spring_test
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2021-09-27
- **Last Updated**: 2021-09-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 一、Spring练习环境搭建
## 1. Spring环境搭建步骤
- 创建工程
- 导入静态页面
- 导入需要坐标
- 创建包结构(controller、service、dao、domain、urils)
- 导入数据库脚本(test.sql)
- 创建POJO类(User.java和Role.java)
- 创建配置文件(applicationContext.xml、spring-mvc.xml、jdbc.properties、log4j.properties)
**applicationContext.xml基础配置:**
```xml
```
**spring-mvc.xml基础配置:**
```xml
```
**web.xml基础配置:**
```xml
contextConfigLocation
classpath:applicationContext.xml
org.springframework.web.context.ContextLoaderListener
dispatcherServlet
org.springframework.web.servlet.DispatcherServlet
contextConfigLocation
classpath:spring-mvc.xml
1
dispatcherServlet
/
```
# 二、角色列表的展示和添加操作
## 2.1 角色列表的展示步骤分析
- 点击角色管理菜单发送请求到服务器端(修改角色管理菜单的url地址)
- 创建RoleController和showList()方法
- 创建RoleService和showList()方法
- 创建RoleDao和findAll()方法
- 使用JdbcTemplate完成查询操作
- 将查询数据存到Mode中
- 转发到role-list.jsp页面进行展示
**RoleController.java**
```java
@RequestMapping("/role")
@Controller
public class RoleController {
//注入service层
@Autowired
private RoleService roleService;
@RequestMapping("/list")
public ModelAndView list(){
ModelAndView modelAndView = new ModelAndView();
List roleList = roleService.list();
//设置模型对象
modelAndView.addObject("roleList",roleList);
//设置视图
modelAndView.setViewName("role-list");
return modelAndView;
}
}
```
**Service层**
```java
public interface RoleService {
List list();
}
```
```java
public class RoleServiceImpl implements RoleService {
//注入Dao层
private RoleDao roleDao;
public void setRoleDao(RoleDao roleDao){
this.roleDao = roleDao;
}
@Override
public List list() {
List roleList = roleDao.findAll();
System.out.println("执行service--list");
return roleList;
}
}
```
**Dao层**
```java
public interface RoleDao {
List findAll();
}
```
```java
public class RoleDaoImpl implements RoleDao {
//注入JdbcTemplate
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template){
this.template = template;
}
@Override
public List findAll() {
System.out.println("执行dao--findAll");
String sql = "select * from sys_role";
List roleList = template.query(sql, new BeanPropertyRowMapper(Role.class));
System.out.println("执行dao ---> "+roleList);
return roleList;
}
}
```
## 2.2 角色添加的步骤分析
- 点击列表页面新建按钮跳转到角色添加页面
- 输入角色信息,点击保存按钮,表单数据提交服务器
- 编写RoleController的save()方法
- 编写RoleService的save()方法
- 编写RoleDao的save()方法
- 使用JdbcTemplate保存Role数据到sys_role
- 跳转回角色列表页面
**RoleController.java**
```java
@RequestMapping("/save")
public String save(Role role){
roleService.save(role);
return "redirect:/role/list";
}
```
**Service层**
```java
void save(Role role);
```
```java
@Override
public void save(Role role) {
roleDao.save(role);
}
```
**Dao层**
```java
void save(Role role);
```
```java
@Override
public void save(Role role) {
String sql = "insert into sys_role values(?,?,?)";
template.update(sql,null,role.getRoleName(),role.getRoleDesc());
}
```
# 三、用户列表的展示
**UserController.java**
```java
@Controller
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@RequestMapping("/list")
public ModelAndView list(){
List userList = userService.list();
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("userList",userList);
modelAndView.setViewName("user-list");
return modelAndView;
}
}
```
**Service层**
```java
public interface UserService {
List list();
}
```
```java
public class UserServiceImpl implements UserService {
private UserDao userDao;
public void setUserDao(UserDao userDao){
this.userDao = userDao;
}
private RoleDao roleDao;
public void setRoleDao(RoleDao roleDao){
this.roleDao = roleDao;
}
@Override
public List list() {
List userList = userDao.findAll();
//封装userList中的每一个User的roles数据
for (User user :
userList) {
//获得user的id
Long id = user.getId();
//将id作为参数,查询当前userId对应的Role集合数据
List roles = roleDao.findRoleByUserId(id);
user.setRoles(roles);
}
return userList;
}
}
```
**Dao层--UserDao**
```java
public interface UserDao {
List findAll();
}
```
```java
public class UserDaoImpl implements UserDao {
private JdbcTemplate template;
public void setTemplate(JdbcTemplate template){
this.template = template;
}
@Override
public List findAll() {
String sql = "select * from sys_user";
List userList = template.query(sql, new BeanPropertyRowMapper(User.class));
return userList;
}
}
```
**Dao层--RoleDao**
```java
List findRoleByUserId(Long id);
```
```java
@Override
public List findRoleByUserId(Long id) {
String sql = "select * from sys_user_role ur, sys_role r where ur.roleId=r.id and ur.userId=?";
List roles = template.query(sql, new BeanPropertyRowMapper(Role.class), id);
return roles;
}
```
# 四、删除用户操作