1.新建一个项目(Sql开发的是JPA的使用)
2.在pom.xml中导入依赖(只有部分常用的剩下的需要自己添加)
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 基础工具包导入 -->
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.16.10</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.47</version>
</dependency>
<!-- 数据库的使用 和 JPA的导入 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-jpa</artifactId>
</dependency>
<dependency>
<groupId>com.querydsl</groupId>
<artifactId>querydsl-apt</artifactId>
</dependency>
<!--springboot 整合的数据连接-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- 加入映射 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.1.1</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>
<!--thymeleaf结束-->
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<!-- maven的处理和sql -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>com.mysema.maven</groupId>
<artifactId>apt-maven-plugin</artifactId>
<version>1.1.3</version>
<executions>
<execution>
<goals>
<goal>process</goal>
</goals>
<configuration>
<outputDirectory>target/generated-sources/java</outputDirectory>
<processor>com.querydsl.apt.jpa.JPAAnnotationProcessor</processor>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
3.在application.yml中进行配置
server:
port: 1100
#服务器的使用
obsCdn:
host: http://81.70.220.54
spring:
thymeleaf:
prefix:
classpath: /templates/
suffix: .html
mode: HTML
encoding: UTF-8
servlet:
content-type: text/html
#数据库
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://81.70.220.54:3306/student?characterEncoding=UTF-8&serverTimezone=GMT%2B8
username: root
password: password
servlet:
multipart:
max-request-size: 1000MB
max-file-size: 100MB
jpa:
#配置数据库类型
database: MYSQL
#配置是否打印sql
show-sql: true
#Hibernate相关配置
hibernate:
#配置级联等级
ddl-auto: update
naming:
#命名策略 遇到大写字母 加”_”的命名。
strategy: org.hibernate.cfg.ImprovedNamingStrategy
properties:
hibernate:
dialect: org.hibernate.dialect.MySQL5Dialect
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
#mybatis配置文件
mybatis:
mapperLocations: classpath:Mapper/*.xml
type-aliases-package: com.test.demoApp.Domain
#图片存放地
userPhoto:
urlPrefix: /user/getPhoto
store:
path: /deploy/student/Phone/user
obsPath: /user/getPhoto
cbs:
videosPath: file:D:/testFile/video/
imagesPath: file:D:/testFile/image/
4.在resources资源文件下新建mybatis-generator.xml配置数据及mybatis
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--这个是你springboot的配置文件,它会加载spring的配置-->
<!-- <properties resource="application.yml"/> -->
<!-- 必须要有这个 mysql驱动的位置 这个是MySQL连接的jar包,你需要指定你自己计算机上的jar包的位置,我用的自己本机的maven仓库的,注意别只复制这个jar存在的路径,在最后边要加上jar的名字哦。细节细节-->
<classPathEntry location="C:\Users\zhilong.zhang\.m2\repository\mysql\mysql-connector-java\8.0.16\mysql-connector-java-8.0.16.jar"/>
<context id="Tables" targetRuntime="MyBatis3">
<!-- 生成 JavaBean 对象重写 toString方法 -->
<plugin type="org.mybatis.generator.plugins.ToStringPlugin"/>
<!-- 取消生成注释 -->
<commentGenerator>
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--这是mysql连接的配置信息,DriverClass的路径是否存在 cj 看自己mysql连接的版本 5.xx没有cj。6.xx的有cj-->
<jdbcConnection driverClass="com.mysql.jdbc.Driver"
connectionURL="jdbc:mysql://localhost:3306/test?serverTimezone=GMT"
userId="root"
password="password">
<!--这个很重要,没有的话,会发生一些奇怪的问题,稍后细讲!-->
<property name="nullCatalogMeansCurrent" value="true" />
</jdbcConnection>
<!-- 默认false,把JDBC DECIMAL 和 NUMERIC 类型解析为 Integer,为 true时把JDBC DECIMAL 和NUMERIC 类型解析为java.math.BigDecimal 涉及金额就要注意了哟-->
<javaTypeResolver>
<property name="forceBigDecimals" value="false" />
</javaTypeResolver>
<!--指定javaBean生成的位置 targetProject="./src/main/java" 注意src前面有个zf-->
<!--targetPackage="com.chenggongyuan.xunjian.pojo",这里是自己项目的名字,下面的都得替换成自己的项目名称-->
<javaModelGenerator targetPackage="com.test.demoApp.Domain"
targetProject="demoApp/src/main/java">
<!--这个为true 逆向工程可能会生成子包-->
<property name="enableSubPackages" value="true"/>
<property name="trimStrings" value="true"/>
</javaModelGenerator>
<!-- 指定sql映射文件生成位置 -->
<sqlMapGenerator targetPackage="Mapper" targetProject="demoApp/src/main/resources">
<property name="enableSubPackages" value="false"/>
</sqlMapGenerator>
<!-- 指定dao接口生成的位置,mapper接口 -->
<javaClientGenerator type="XMLMAPPER" targetPackage="com.test.demoApp.Dao"
targetProject="demoApp/src/main/java">
<property name="enableSubPackages" value="true"/>
</javaClientGenerator>
<!-- 制定每个表的生成策略 这里可以写多个表,表名和需要生成的pojo名写正确 -->
<table tableName="post" domainObjectName="post"></table>
</context>
</generatorConfiguration>
5.数据库的具体使用看idea项目 --- 2.数据库的使用(简单方便)
网友评论