投票管理系统的设计与实现(项目搭建和数据设计)

一、项目搭建:

1.由于使用的是springboot,降低了开发和环境搭建的时间,使用了maven来管理项目的依赖和jar包,减轻了项目搭建的难度,主要jar包如下。

		<dependencies>
		<!--redis依赖-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-data-redis</artifactId>
		</dependency>
		<!--themleaf模板引擎,视图显示-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-thymeleaf</artifactId>
		</dependency>
		<!--spring boot核心-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-amqp</artifactId>
		</dependency>
		<!--shiro核心组件-->
		<dependency>
			<groupId>org.apache.shiro</groupId>
			<artifactId>shiro-spring-boot-web-starter</artifactId>
			<version>1.7.0</version>
		</dependency>
		<!--视图和shiro的界面代码-->
		<dependency>
			<groupId>com.github.theborakompanioni</groupId>
			<artifactId>thymeleaf-extras-shiro</artifactId>
			<version>2.0.0</version>
		</dependency>
		<dependency>
			<groupId>commons-fileupload</groupId>
			<artifactId>commons-fileupload</artifactId>
			<version>1.4</version>
		</dependency>
		<!--热部署,快速开发-->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<!--MySQL数据库驱动,连接数据库-->
		<dependency>
			<groupId>mysql</groupId>
			<artifactId>mysql-connector-java</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</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>
		<dependency>
			<groupId>com.alibaba</groupId>
			<artifactId>druid</artifactId>
			<version>1.1.8</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-generator</artifactId>
			<version>3.4.1</version>
		</dependency>
		<dependency>
			<groupId>org.apache.velocity</groupId>
			<artifactId>velocity-engine-core</artifactId>
			<version>2.2</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
		<dependency>
			<groupId>log4j</groupId>
			<artifactId>log4j</artifactId>
			<version>1.2.9</version>
		</dependency>
		<dependency>
			<groupId>org.apache.logging.log4j</groupId>
			<artifactId>log4j-core</artifactId>
			<version>2.12.1</version>
		</dependency>
		<!--简化entity-->
		<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
			<optional>true</optional>
		</dependency>
	</dependencies>

二、数据库分析

数据库这一块,我设计了6张表,没有使用RBAC模式对数据库进行设计,所以可能设计的不是特别好,对于还未入门的在校小萌新来说已经绞尽脑汁啦,数据库结构还是挺简单的,首先是用户表,用来存储用户信息,一张投票信息表,用来存储投票信息,还有投票信息数量表,投票信息选项表,投票信息类型表,最后一张就是关系表,用户和投票信息之间的关系表。

在这里插入图片描述

​ 三、逆向工程

在这里我使用了MP(Mybatis-Plus)对数据库进行操作,MP和Mybatis之间的关系就像魂斗罗双兄弟一样,只是在其基础上进行了增强,我这里使用了MP的代码生成策略,简化实体类开发,还有接口那些繁琐的过程,彻底解放双手:

public class DataSourceCode {
	public static void main(String[] args) {
		AutoGenerator mpg = new AutoGenerator();
		GlobalConfig gc = new GlobalConfig();
		String projectPath = System.getProperty("user.dir");
		gc.setOutputDir(projectPath + "/src/main/java");
		gc.setAuthor("zhanziqin");
		gc.setOpen(false);
		gc.setFileOverride(false);
		gc.setServiceName("%sService");
		gc.setIdType(IdType.ID_WORKER);
		gc.setDateType(DateType.ONLY_DATE);
		gc.setSwagger2(true);
		mpg.setGlobalConfig(gc);
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setUrl(
				"jdbc:mysql://root@localhost:3306/votedemo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true");
		dsc.setDriverName("com.mysql.cj.jdbc.Driver");
		dsc.setUsername("root");
		dsc.setPassword("123456");
		dsc.setDbType(DbType.MYSQL);
		mpg.setDataSource(dsc);
		PackageConfig pc = new PackageConfig();
//        pc.setModuleName("blog");
		pc.setParent("com");
		pc.setEntity("entity");
		pc.setMapper("mapper");
		pc.setService("service");
		pc.setController("controller");
		mpg.setPackageInfo(pc);

		StrategyConfig strategy = new StrategyConfig();
		strategy.setInclude("v_info");
		strategy.setNaming(NamingStrategy.underline_to_camel);
		strategy.setColumnNaming(NamingStrategy.underline_to_camel);
		System.out.println("生成成功");
		mpg.setStrategy(strategy);
		mpg.execute();
	}

}

四、全局配置

接下来是对项目的全局进行配置,设置数据库参数,数据库连接池,国际化配置,还有缓存配置,日志打印等。

spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.url=jdbc:mysql://root@localhost:3306/votedemo?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong&rewriteBatchedStatements=true&allowPublicKeyRetrieval=true
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
# 国际化
spring.messages.basename=i18n.login 
spring.messages.encoding=utf-8
#禁用缓存 + ctrl+f9:重新编译
spring.thymeleaf.cache=false
server.tomcat.uri-encoding=UTF-8
#sql日志打印
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
mybatis-plus.mapper-locations=classpath:xml/*.xml
#mybatis-plus.mapper-locations=classpath:/com/mapper/xml/*.xml
spring.mvc.hiddenmethod.filter.enabled=true
spring.redis.host=127.0.0.1
spring.redis.port=6379
#debug=true
#设置开启热部署
spring.devtools.restart.enabled=true
ons=classpath:/com/mapper/xml/*.xml
spring.mvc.hiddenmethod.filter.enabled=true
spring.redis.host=127.0.0.1
spring.redis.port=6379
#debug=true
#设置开启热部署
spring.devtools.restart.enabled=true

更多推荐