spring boot+mysql+druid+redis+jpa+ajax实现数据的增删改查

2 篇文章 0 订阅
2 篇文章 0 订阅

说明:基于spring boot 2.1.5+原生druid 1.1.16(未使用spring boot推荐的spring-boot-starter-thymeleaf作为前端模板)

开发工具:eclipse 4.7.2

1、 eclipse中安装STS 插件(Spring Tool Suite)

Eclipse中运用STS插件(Spring Tool Suite)可快速搭建spring boot框架,STS安装步骤:Help->Eclipse Marketplace…,搜索sts,点击install,这个过程比较费时,需耐心等待(我本地已经安装了,所以显示installed

 

2、新建spring boot项目

STS插件安装完毕后就可进行spring boot项目创建,步骤:File->New->Spring Starter Project

填写项目信息

 

下一步选择既定的jar包:

 

至此,框架基本上搭建完毕了

3、配置项目目录结构

 

4、jpa反向工程

首先,得将项目转成JPA Project...步骤:右键选中项目->configure->Convert to JPA Project…

 

 

在新弹出的提示框中增加数据库的连接信息,完成即可

 

项目转成JPA工程后,右键项目可看见JPA Tools选项,可生成数据库表的实体类,步骤:JPA Tools->Generate Entities from tables… 接着下一步就行(最后有选择生成的实体放哪个包下面,注意就行)

 

 

5、多环境配置

首先,根据已知的环境整理出对应环境的配置文件,以统一规则命名,如application-***.properties

 

接着在Pom.xml文件中增加profiles标签指定预定义环境信息

<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<!-- 环境标识,需要与配置文件的名称相对应 -->
				<profileActive>dev</profileActive>
			</properties>
			<activation>
				<!-- 默认环境 -->
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>dat</id>
			<properties>
				<profileActive>dat</profileActive>
			</properties>
		</profile>
		<profile>
			<id>uat</id>
			<properties>
				<profileActive>uat</profileActive>
			</properties>
		</profile>
		<profile>
			<id>vir</id>
			<properties>
				<profileActive>vir</profileActive>
			</properties>
		</profile>
		<profile>
			<id>pro</id>
			<properties>
				<profileActive>pro</profileActive>
			</properties>
		</profile>
</profiles>

接着在Pom.xml文件中<build>标签下增加子标签<resources>控制将指定环境的配置文件打到包中

<build>
		<finalName>${project.artifactId}</finalName>
		<resources>
			<resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <excludes>
                    <exclude>application-dev.properties</exclude>
                    <exclude>application-dat.properties</exclude>
                    <exclude>application-uat.properties</exclude>
                    <exclude>application-vir.properties</exclude>
                    <exclude>application-pro.properties</exclude>
                    <exclude>application.properties</exclude>
                </excludes>
            </resource>
            <!--需要动态添加的资源-->
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <!--读取打包命令中指定的环境-->
                    <include>application-${profileActive}.properties</include>
                    <include>application.properties</include>
                </includes>
            </resource>
		</resources>
</build>

在application.properties文件增加环境指向

spring.profiles.active=@profileActive@
#其中profileActive与前面pom.xml定义的<profileActive>dev</profileActive>保持一致

6、打包时指定环境

Eclipse中打包相关简单,步骤:右键选中项目->运行方式(R->Maven build…,在弹出框中指定位置输入环境信息即可

 

其中,Goalsmaven操作命令,Profiles为对应环境

7、运行时指定环境

spring boot自带tomcat容器,可直接运行,步骤:右键项目(项目中任意文件右键点击)->运行方式(R->运行 配置(N),自动定位到Spring Boot App选项下的启动文件,在右侧边框中可选定对应环境。

注:打包环境与运行环境需要保持一致,否则会报错,如以dev打包,但以dat运行,就会报错;(我本地测试时是这样的,个人简介)

 

 

 

 

 

以上,就是我搭建项目的大致步骤,具体示例见资源路径(绝对真实能跑起来):https://download.csdn.net/download/freedom_fire/11225502

 过程也出现了一些出错:

问题1:com.mysql.cj.exceptions.InvalidConnectionAttributeException: The server time zone value '?й???????' is unrecognized or represents more than one time zone. You must configure either the server or JDBC driver (via the serverTimezone configuration property) to use a more specifc time zone value if you want to utilize time zone support.
问题原因:为了使MySQL JDBC驱动程序的5.1.33版本与UTC时区配合使用,必须在连接字符串中明确指定serverTimezone。
处理办法:jdbc:mysql://localhost:3306/test?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC

问题2:jQuery前端请求时,传json格式数据,后台不用HashMap<String, String> map接受就报下面的错
org.springframework.web.bind.MissingServletRequestParameterException: Required int parameter 'id' is not present

问题3:一个html直接跳转另一个html页面时传值带中文报错
Error parsing HTTP request header
java.lang.IllegalArgumentException: Invalid character found in the request target. The valid characters are defined in RFC 7230 and RFC 3986
处理办法:请求地址用encodeURI方法处理,如url = encodeURI(url);  接受页面用var url=decodeURI(window.location.search);处理

 

 

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值