版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_16855077/article/details/84957747
1 pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloudtech</groupId>
<artifactId>01-springboot-hello</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath />
<!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- mybatis启动器 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</version>
</dependency>
<!-- mysql数据库驱动 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!-- druid数据库连接池 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.0.9</version>
</dependency>
<!-- 增加thymeleaf坐标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- 热部署 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<!--通用Mapper -->
<dependency>
<groupId>tk.mybatis</groupId>
<artifactId>mapper-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
<!--pageHelper分页 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.5</version>
</dependency>
</dependencies>
</project>
项目的结构:
2.application.yml
server:
port: 8082
spring:
datasource:
# 数据源基本配置
url: jdbc:mysql://XXXX:3306/test?useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true
username: XXX
password: XXXX
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
#mybatis相关配置
mybatis:
#当mybatis的xml文件和mapper接口不在相同包下时
#需要用mapperLocations属性指定xml文件的路径。
#*是个通配符,代表所有的文件,**代表所有目录下
mapper-locations: classpath*:/com/cloudtech/mapper/*.xml
#指定bean所在包
#在mapper.xml中可以使用别名而不使用类的全路径名
type-aliases-package: com.cloudtech.entity
#通用mapper配置
mapper:
identity: MYSQL # 取主键的方式
before: false # 主键递增
not-empty: true # 按主键插入或更新时,是否判断字符串 != ''
style: camelhumpandlowercase # 实体类与表中字段的映射方式:驼峰转带下划线的小写格式
wrap-keyword: '{0}' # 自动配置关键字,配置后不需要使用 @Column 指定别名
safe-delete: true # 删除时必须设置查询条件
safe-update: true # 更新时必须设置查询条件
use-java-type: true # 是否映射Java基本数据类型
mappers: tk.mybatis.mapper.common.Mapper
#pagehelper分页插件
pagehelper:
helperDialect: mysql
reasonable: true
supportMethodsArguments: true
params: count=countSql
记得把数据库的信息修改成自己的
3 新增权限信息
3.1 controller代码
RoleController.java
package com.cloudtech.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cloudtech.dao.RoleMapper;
import com.cloudtech.entity.Role;
/**
* 角色控制器
* @ClassName: RoleController
* @Description:
* @author wude
* @date 2018年12月11日
*
*/
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleMapper roleMapper;
/**
* 方便通过url地址直接访问网站
* @param page
* @return
*/
@RequestMapping("/{page}")
public String page(@PathVariable String page){
return page;
}
/**
* 增加角色信息
* @param role
* @return
*/
@RequestMapping("/addRole")
public String roleIndex(Role role){
int result = roleMapper.insertSelective(role);
if(result > 0){
return "ok";
}else{
return "error";
}
}
}
DemoController.java 控制跳转页面的
package com.cloudtech.controller;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cloudtech.entity.User;
@Controller
public class DemoController {
/**
* 方便通过url地址直接访问网站
* @param page
* @return
*/
@RequestMapping("/{page}")
public String page(@PathVariable String page){
return page;
}
}
3.2 dao类
package com.cloudtech.dao;
import com.cloudtech.entity.Role;
import tk.mybatis.mapper.common.Mapper;
public interface RoleMapper extends Mapper<Role>{
}
3.3 实体类
package com.cloudtech.entity;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
/**
*
* @ClassName: Role
* @Description: 角色
* @author wude
* @date 2018年6月25日
*
*/
public class Role implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@Column
@GeneratedValue(strategy = GenerationType.IDENTITY)
/**
* @author wilson
*主键id
*/
private Integer id;
/**
* @author wilson
*角色名称
*/
private String name;
/**
* @author wilson
*角色描述
*/
private String description;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description == null ? null : description.trim();
}
@Override
public String toString() {
return "Role [id=" + id + ", name=" + name + ", description=" + description + "]";
}
}
记住一定要序列化,使用通用mapper
补一个启动类的代码,启动类一定要这样配置
package com.cloudtech;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;
import tk.mybatis.spring.annotation.MapperScan;
/**
*
* @ClassName: App2
* @Description:
* @author wude
* @date 2018年12月11日
*
*/
@SpringBootApplication
@ServletComponentScan
@MapperScan(basePackages = "com.cloudtech.dao")
public class App2 {
public static void main(String[] args) {
SpringApplication.run(App2.class, args);
}
}
3.4 数据库表结构
/*
Navicat MySQL Data Transfer
Source Server : 外网阿里云环境
Source Server Version : 50723
Source Host : 120.78.150.135:3306
Source Database : test
Target Server Type : MYSQL
Target Server Version : 50723
File Encoding : 65001
Date: 2018-12-11 17:33:35
*/
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for `role`
-- ----------------------------
DROP TABLE IF EXISTS `role`;
CREATE TABLE `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(255) NOT NULL COMMENT '角色名称',
`description` varchar(255) NOT NULL COMMENT '角色描述',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of role
-- ----------------------------
INSERT INTO `role` VALUES ('1', '超级管理员', '超级管理员');
INSERT INTO `role` VALUES ('2', '气象站服务人员', 'test');
INSERT INTO `role` VALUES ('18', '游客', '游客');
INSERT INTO `role` VALUES ('22', '1', '2');
3.5 前端代码
addrole.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加权限</title>
</head>
<body>
<form th:action="@{/role/addRole}" method="post">
name:<input type="text" name="name"><br/>
description:<input type="text" name="description"><br/>
<input type="submit" name="确定">
</form>
</body>
</html>
ok.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>操作提示页面</title>
</head>
<body>
操作成功!!!
</body>
</html>
3.6测试
点击提交
查看数据库的数据
就这样,添加权限的功能就OK咯。
4 权限列表信息
4.1 controller类
package com.cloudtech.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cloudtech.dao.RoleMapper;
import com.cloudtech.entity.Role;
/**
* 角色控制器
* @ClassName: RoleController
* @Description:
* @author wude
* @date 2018年12月11日
*
*/
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleMapper roleMapper;
/**
* 方便通过url地址直接访问网站
* @param page
* @return
*/
@RequestMapping("/{page}")
public String page(@PathVariable String page){
return page;
}
/**
* 增加角色信息
* @param role
* @return
*/
@RequestMapping("/addRole")
public String addRole(Role role){
int result = roleMapper.insertSelective(role);
if(result > 0){
return "ok";
}else{
return "error";
}
}
/**
* 角色列表
* @param model
* @return
*/
@RequestMapping("/index")
public String roleIndex(Model model){
List<Role> roles = roleMapper.select(null);
model.addAttribute("roles", roles);
return "role";
}
}
4.2前端页面
role.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1" style="width: 300px;">
<tr>
<td>id</td>
<td>名称</td>
<td>描述</td>
</tr>
<tr th:each="r:${roles}">
<td th:text="${r.id}"></td>
<td th:text="${r.name}"></td>
<td th:text="${r.description}"></td>
</tr>
</table>
</body>
</html>
4.3测试
5.删除和修改权限信息
5.1controller类
package com.cloudtech.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.cloudtech.dao.RoleMapper;
import com.cloudtech.entity.Role;
/**
* 角色控制器
* @ClassName: RoleController
* @Description:
* @author wude
* @date 2018年12月11日
*
*/
@Controller
@RequestMapping("/role")
public class RoleController {
@Autowired
private RoleMapper roleMapper;
/**
* 方便通过url地址直接访问网站
* @param page
* @return
*/
@RequestMapping("/{page}")
public String page(@PathVariable String page){
return page;
}
/**
* 增加角色信息
* @param role
* @return
*/
@RequestMapping("/addRole")
public String addRole(Role role){
int result = roleMapper.insertSelective(role);
if(result > 0){
return "ok";
}else{
return "error";
}
}
/**
* 角色列表
* @param model
* @return
*/
@RequestMapping("/index")
public String roleIndex(Model model){
List<Role> roles = roleMapper.select(null);
model.addAttribute("roles", roles);
return "role";
}
/**
* 根据权限id查询权限信息
* @param id
* @param model
* @return
*/
@RequestMapping("/findRoleById")
public String findRoleById(Integer id,Model model){
Role role = roleMapper.selectByPrimaryKey(id);
model.addAttribute("role", role);
return "updaterole";
}
/**
* 修改角色信息
* @param role
* @return
*/
@RequestMapping("/updateRole")
public String updateRole(Role role){
int result = roleMapper.updateByPrimaryKey(role);
if(result > 0){
return "ok";
}else{
return "error";
}
}
/**
* 删除角色信息
* @param role
* @return
*/
@RequestMapping("/deleteRole")
public String deleteRole(Integer id){
int result = roleMapper.deleteByPrimaryKey(id);
if(result > 0){
return "redirect:/role/index";
}else{
return "error";
}
}
}
5.2前端页面
role.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<a th:href="@{/addrole}">新增角色</a><br/>
<table border="1" style="width: 900px;">
<tr>
<td>id</td>
<td>名称</td>
<td>描述</td>
<td>操作</td>
</tr>
<tr th:each="r:${roles}">
<td th:text="${r.id}"></td>
<td th:text="${r.name}"></td>
<td th:text="${r.description}"></td>
<td><a th:href="@{/role/findRoleById(id=${r.id})}">修改角色</a>
<a th:href="@{/role/deleteRole(id=${r.id})}">删除角色</a>
</td>
</tr>
</table>
</body>
</html>
addrole.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>增加权限</title>
</head>
<body>
<form th:action="@{/role/addRole}" method="post">
name:<input type="text" name="name"><br/>
description:<input type="text" name="description"><br/>
<input type="submit" name="确定">
</form>
</body>
</html>
updaterole.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>修改权限</title>
</head>
<body>
<form th:action="@{/role/updateRole}" method="post">
<input type="hidden" name="id" th:value="${role.id}">
name:<input type="text" name="name" th:value="${role.name}"><br/>
description:<input type="text" name="description" th:value="${role.description}"><br/>
<input type="submit" name="确定">
</form>
</body>
</html>