第八章 项目框架搭建及用户管理初建
连接数据库,使用EA直接生成数据库脚本,创建表。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
/* ---------------------------------------------------- */ /* Generated by Enterprise Architect Version 14.0 */ /* Created On : 09-12月-2018 12:49:55 */ /* DBMS : MySql */ /* ---------------------------------------------------- */ SET FOREIGN_KEY_CHECKS=0 ; /* Drop Tables */ DROP TABLE IF EXISTS `ts_department` CASCADE ; DROP TABLE IF EXISTS `ts_module` CASCADE ; DROP TABLE IF EXISTS `ts_permission` CASCADE ; DROP TABLE IF EXISTS `ts_role` CASCADE ; DROP TABLE IF EXISTS `ts_user` CASCADE ; DROP TABLE IF EXISTS `ts_user_role` CASCADE ; /* Create Tables */ CREATE TABLE `ts_department` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键', `mame` VARCHAR(20) NOT NULL COMMENT '名称', `pid` INT NOT NULL COMMENT '上级', `pids` VARCHAR(1000) NOT NULL COMMENT '结构', `is_valid` TINYINT NOT NULL COMMENT '是否有效(1有效,0无效)', `levels` INT NOT NULL COMMENT '级别', `order_num` INT NOT NULL COMMENT '排序', `create_time` DATETIME NOT NULL COMMENT '创建时间', `edit_time` DATETIME NOT NULL COMMENT '更新时间', `editor` INT NOT NULL COMMENT '更新人', `editor_name` VARCHAR(20) NOT NULL COMMENT '更新人', CONSTRAINT `PK_ts_department` PRIMARY KEY (`id` ASC) ) COMMENT = '组织结构表' ; CREATE TABLE `ts_module` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键', `pid` INT NOT NULL COMMENT '父键', `pids` VARCHAR(200) NOT NULL COMMENT '结构', `code` VARCHAR(50) NOT NULL COMMENT '编号', `name` VARCHAR(20) NOT NULL COMMENT '名称', `icon` VARCHAR(50) NOT NULL COMMENT '图标', `url` VARCHAR(50) NOT NULL COMMENT '地址', `levels` INT NOT NULL COMMENT '层级', `type` TINYINT NOT NULL COMMENT '类型(1菜单,2方法)', `order_num` INT NOT NULL DEFAULT 100 COMMENT '排序', `is_valid` TINYINT NOT NULL COMMENT '状态(1启用;2停用)', `remarks` VARCHAR(200) NOT NULL COMMENT '备注', `create_time` DATETIME NOT NULL COMMENT '创建时间', `edit_time` DATETIME NOT NULL COMMENT '修改时间', `editor` INT NOT NULL COMMENT '修改人', `editor_name` VARCHAR(20) NOT NULL COMMENT '修改人', CONSTRAINT `PK_ts_menu` PRIMARY KEY (`id` ASC) ) COMMENT = '菜单表' ; CREATE TABLE `ts_permission` ( `id` INT NOT NULL AUTO_INCREMENT, `role_id` INT NOT NULL COMMENT '角色主键', `module_id` INT NOT NULL COMMENT '菜单主键', `create_time` DATETIME NOT NULL COMMENT '创建时间', `edit_time` DATETIME NOT NULL COMMENT '修改时间', `editor` INT NOT NULL COMMENT '修改人', `editor_name` VARCHAR(20) NOT NULL COMMENT '修改人', `is_valid` TINYINT NOT NULL COMMENT '是否有效(1有效,0无效)', CONSTRAINT `PK_ts_permission` PRIMARY KEY (`id` ASC) ) COMMENT = '权限表' ; CREATE TABLE `ts_role` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键', `role_name` VARCHAR(20) NOT NULL COMMENT '角色名称', `is_valid` TINYINT NOT NULL COMMENT '是否有效(1有效,0无效)', `remarks` VARCHAR(200) NOT NULL COMMENT '说明', `create_time` DATETIME NOT NULL COMMENT '创建时间', `edit_time` DATETIME NOT NULL COMMENT '修改时间', `editor` INT NOT NULL COMMENT '修改人', `editor_name` VARCHAR(20) NOT NULL, CONSTRAINT `PK_ts_role` PRIMARY KEY (`id` ASC) ) COMMENT = '角色表' ; CREATE TABLE `ts_user` ( `id` INT NOT NULL AUTO_INCREMENT COMMENT '主键', `username` VARCHAR(20) NOT NULL COMMENT '用户名', `password` VARCHAR(64) NOT NULL COMMENT '密码', `salt` VARCHAR(50) NOT NULL COMMENT '密码加盐', `realname` VARCHAR(20) NOT NULL COMMENT '姓名', `sex` VARCHAR(1) NOT NULL COMMENT '性别(1男,2女)', `mail` VARCHAR(50) NOT NULL COMMENT '邮箱地址', `phone` VARCHAR(20) NOT NULL COMMENT '电话', `avatar` VARCHAR(50) NOT NULL, `remarks` VARCHAR(500) NOT NULL COMMENT '备注', `status` CHAR(1) NOT NULL COMMENT '状态(1正常,2停用,3删除)', `create_time` DATETIME NOT NULL COMMENT '创建时间', `edit_time` DATETIME NOT NULL COMMENT '更新时间', `editor` INT NOT NULL COMMENT '更新人', `editor_name` VARCHAR(20) NOT NULL COMMENT '更新人', `last_login_time` DATETIME NULL COMMENT '最后登陆时间', `last_login_ip` VARCHAR(32) NULL COMMENT '最后登陆ip地址', CONSTRAINT `PK_ts_user` PRIMARY KEY (`id` ASC) ) COMMENT = '用户表' ; CREATE TABLE `ts_user_role` ( `id` INT NOT NULL AUTO_INCREMENT, `user_id` INT NOT NULL COMMENT '用户主键', `role_id` INT NOT NULL COMMENT '角色主键', `dept_id` INT NOT NULL, `create_time` DATETIME NOT NULL COMMENT '创建时间', `edit_time` DATETIME NOT NULL COMMENT '修改时间', `is_valid` TINYINT NOT NULL COMMENT '是否有效(1有效,0无效)', CONSTRAINT `PK_ts_user_role` PRIMARY KEY (`id` ASC) ) COMMENT = '用户角色关联表' ; /* Create Primary Keys, Indexes, Uniques, Checks */ ALTER TABLE `ts_module` ADD CONSTRAINT `uni_ts_menu_code` UNIQUE (`code` ASC) ; ALTER TABLE `ts_module` ADD CONSTRAINT `uni_ts_menu_name` UNIQUE (`name` ASC) ; ALTER TABLE `ts_permission` ADD INDEX `IXFK_ts_role_menu_ts_menu` (`module_id` ASC) ; ALTER TABLE `ts_permission` ADD INDEX `IXFK_ts_role_menu_ts_role` (`role_id` ASC) ; ALTER TABLE `ts_role` ADD CONSTRAINT `uni_ts_role_roleName` UNIQUE (`role_name` ASC) ; ALTER TABLE `ts_user` ADD CONSTRAINT `uni_ts_user_username` UNIQUE (`username` ASC) ; ALTER TABLE `ts_user_role` ADD INDEX `IXFK_ts_user_role_ts_department` (`dept_id` ASC) ; ALTER TABLE `ts_user_role` ADD INDEX `IXFK_ts_user_role_ts_role` (`role_id` ASC) ; ALTER TABLE `ts_user_role` ADD INDEX `IXFK_ts_user_role_ts_user` (`user_id` ASC) ; /* Create Foreign Key Constraints */ ALTER TABLE `ts_permission` ADD CONSTRAINT `FK_ts_role_menu_ts_role` FOREIGN KEY (`role_id`) REFERENCES `ts_role` (`id`) ON DELETE Restrict ON UPDATE Restrict ; ALTER TABLE `ts_permission` ADD CONSTRAINT `FK_ts_role_module_ts_menu` FOREIGN KEY (`module_id`) REFERENCES `ts_module` (`id`) ON DELETE Restrict ON UPDATE Restrict ; ALTER TABLE `ts_user_role` ADD CONSTRAINT `FK_ts_user_role_ts_department` FOREIGN KEY (`dept_id`) REFERENCES `ts_department` (`id`) ON DELETE Restrict ON UPDATE Restrict ; ALTER TABLE `ts_user_role` ADD CONSTRAINT `FK_ts_user_role_ts_role` FOREIGN KEY (`role_id`) REFERENCES `ts_role` (`id`) ON DELETE Restrict ON UPDATE Restrict ; ALTER TABLE `ts_user_role` ADD CONSTRAINT `FK_ts_user_role_ts_user` FOREIGN KEY (`user_id`) REFERENCES `ts_user` (`id`) ON DELETE Restrict ON UPDATE Restrict ; SET FOREIGN_KEY_CHECKS=1 ;
创建项目目录。结构如下,每个目录内容就不细说了。最后的结果
后面是代码,先是pom文件,里面包括mybatis, druid, swagger的引用
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> <version>1.3.2</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.1.10</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.13</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.9.2</version> </dependency>
然后是配置文件application.properties
#端口号 server.port=8080 #编码 server.tomcat.uri-encoding=UTF-8 #数据库 spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/tqms?characterEncoding=utf8&useSSL=true spring.datasource.username=root spring.datasource.password=123456 #Mybatis扫描 mybatis.mapper-locations=classpath*:mapper/**/*.xml #输出sql logging.level.zyx.txfan.tqms.mng.mapper=debug
然后针对user表,以下各层代码。
UserController:
@Api(value = "用户管理", description = "用户管理") @RestController @RequestMapping("user") public class UserController { @Autowired private UserService userService; @ApiOperation(value = "用户信息查询", notes = "获取所有用户信息") @PostMapping("query") public List<User> queryUser(QueryUser vo){ return userService.query(vo); } }
UserService:
public interface UserService { List<User> query(QueryUser vo); }
UserServiceImpl:
@Service public class UserServiceImpl implements UserService { @Autowired private UserMapper userMapper; @Override public List<User> query(QueryUser vo) { return userMapper.select(vo); } }
UserMapper:
@Mapper public interface UserMapper { List<User> select(QueryUser vo); }
UserMapper.xml:
<?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="zyx.txfan.tqms.mng.mapper.UserMapper"> <select id="select" resultType="zyx.txfan.tqms.mng.entity.model.User"> select * from ts_user </select> </mapper>
User:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 /** 2 * 用户类 3 */ 4 public class User { 5 private int id; 6 private String username; 7 private String password; 8 private String salt; 9 private String realname; 10 private String sex; 11 private String mail; 12 private String phone; 13 private String avatar; 14 private String remarks; 15 private String status; 16 private Date createTime; 17 private Date editTime; 18 private int editor; 19 private String editorName; 20 private Date lastLoginTime; 21 private String lastLoginIp; 22 23 public int getId() { 24 return id; 25 } 26 27 public void setId(int id) { 28 this.id = id; 29 } 30 31 public String getUsername() { 32 return username; 33 } 34 35 public void setUsername(String username) { 36 this.username = username; 37 } 38 39 public String getPassword() { 40 return password; 41 } 42 43 public void setPassword(String password) { 44 this.password = password; 45 } 46 47 public String getSalt() { 48 return salt; 49 } 50 51 public void setSalt(String salt) { 52 this.salt = salt; 53 } 54 55 public String getRealname() { 56 return realname; 57 } 58 59 public void setRealname(String realname) { 60 this.realname = realname; 61 } 62 63 public String getSex() { 64 return sex; 65 } 66 67 public void setSex(String sex) { 68 this.sex = sex; 69 } 70 71 public String getMail() { 72 return mail; 73 } 74 75 public void setMail(String mail) { 76 this.mail = mail; 77 } 78 79 public String getPhone() { 80 return phone; 81 } 82 83 public void setPhone(String phone) { 84 this.phone = phone; 85 } 86 87 public String getAvatar() { 88 return avatar; 89 } 90 91 public void setAvatar(String avatar) { 92 this.avatar = avatar; 93 } 94 95 public String getRemarks() { 96 return remarks; 97 } 98 99 public void setRemarks(String remarks) { 100 this.remarks = remarks; 101 } 102 103 public String getStatus() { 104 return status; 105 } 106 107 public void setStatus(String status) { 108 this.status = status; 109 } 110 111 public Date getCreateTime() { 112 return createTime; 113 } 114 115 public void setCreateTime(Date createTime) { 116 this.createTime = createTime; 117 } 118 119 public Date getEditTime() { 120 return editTime; 121 } 122 123 public void setEditTime(Date editTime) { 124 this.editTime = editTime; 125 } 126 127 public int getEditor() { 128 return editor; 129 } 130 131 public void setEditor(int editor) { 132 this.editor = editor; 133 } 134 135 public String getEditorName() { 136 return editorName; 137 } 138 139 public void setEditorName(String editorName) { 140 this.editorName = editorName; 141 } 142 143 public Date getLastLoginTime() { 144 return lastLoginTime; 145 } 146 147 public void setLastLoginTime(Date lastLoginTime) { 148 this.lastLoginTime = lastLoginTime; 149 } 150 151 public String getLastLoginIp() { 152 return lastLoginIp; 153 } 154 155 public void setLastLoginIp(String lastLoginIp) { 156 this.lastLoginIp = lastLoginIp; 157 } 158 159 @Override 160 public String toString() { 161 return "User{" + 162 "id=" + id + 163 ", username='" + username + '\'' + 164 ", password='" + password + '\'' + 165 ", salt='" + salt + '\'' + 166 ", realname='" + realname + '\'' + 167 ", sex='" + sex + '\'' + 168 ", mail='" + mail + '\'' + 169 ", phone='" + phone + '\'' + 170 ", avatar='" + avatar + '\'' + 171 ", remarks='" + remarks + '\'' + 172 ", status='" + status + '\'' + 173 ", createTime=" + createTime + 174 ", editTime=" + editTime + 175 ", editor=" + editor + 176 ", editorName='" + editorName + '\'' + 177 ", lastLoginTime=" + lastLoginTime + 178 ", lastLoginIp='" + lastLoginIp + '\'' + 179 '}'; 180 } 181 }
QueryUser:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class QueryUser { 2 }
运行项目,访问127.0.0.1/,看到下面的内容说明我们的项目可以跑起来了
这个是swagger的代码
@Configuration @EnableSwagger2 public class swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("zyx.txfan.tqms.mng.controller")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("欢迎使用tqms") .version("1.0") .build(); } }
访问127.0.0.1/swagger2-ui.html,出现下面内容。我们就可以开心的调试了。
可以访问/user/query,然后try一下。结果如下。在ts_user表里,放一个username为test的记录就可以看到了。
本章小节
在写代码之前真不容易,docker个mysql,然后是更新各种idea,各种ide。开始弄代码时,已经过去了2个小时。
本项目的目录结构有非常大的机率在下次看见时,已经变了。所以,还没想好到底怎么放目录。
本章包括的内容挺多。mybatis,swagger等。大家可以先弄起来。能看到人了。再研究一下具体的。
下章的目标可以在vue里显示出来数据库的内容。估计下章出现后面堆代码就比现在快了。