一茅坛、安裝IDEA
1则拷、下載 java8曹鸠,配置環(huán)境變量
2、下載 idea坛善,安裝
二邻眷、新建項(xiàng)目
1、File - New - Project
選Spring Initializr
改衩,Service URL
使用Default
2抖拴、填寫包名相關(guān)信息com.yzyfdf.shares
腥椒,Java選8
3、勾選依賴
- web
- Spring Web
- SQL
- JDBC API
- MyBatis Framework
- MySQL Driver
4洒放、選擇項(xiàng)目位置
5滨砍、完成配置等待項(xiàng)目初始化
三、添加數(shù)據(jù)
1惋戏、安裝 MySQL,idea連接數(shù)據(jù)庫响逢,可以參照 PyCharm連接MySQL
2、創(chuàng)建數(shù)據(jù)庫些膨,新建一個表钦铺,比如:
create table sharesdata
(
myId int primary key auto_increment comment '主鍵id',
timestamp varchar(13) comment '收盤時間',
symbol varchar(12) not null comment '股票代碼',
index (symbol),
name varchar(8) not null comment '股票名稱',
current float comment '當(dāng)前股價',
chg float comment '股價變更',
percent float comment '股價變更百分比',
high float comment '最高',
low float comment '最低',
open float comment '今開',
last_close float comment '昨收',
limit_up float comment '漲停',
limit_down float comment '跌停',
volume bigint comment '成交量',
amount float comment '成交額'
);
3、插入一條數(shù)據(jù)
insert into sharesdata (symbol, name, current, chg, percent, high, low, open, last_close, limit_up, limit_down)
values ('SH600000', '浦發(fā)銀行', 10.46, -0.04, -0.38, 10.57, 10.44, 10.57, 10.5, 11.55, 9.45, 38982822, 408921984);
四洼哎、配置mybatis-generator插件
1、pom.xml中plugins
標(biāo)簽下加入
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.5</version>
<configuration>
<!-- 在控制臺打印執(zhí)行日志 -->
<verbose>true</verbose>
<!-- 重復(fù)生成時會覆蓋之前的文件-->
<overwrite>true</overwrite>
<configurationFile>src/main/resources/generatorConfig.xml</configurationFile>
</configuration>
<!-- 數(shù)據(jù)庫連接選擇8.0以上的窑邦,因?yàn)橛玫膍ysql8.0壕探,這里一定要加,雖然上面依賴?yán)镆呀?jīng)有了-->
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.15</version>
</dependency>
</dependencies>
</plugin>
2瞧筛、跟啟動類Application同級导盅,創(chuàng)建4個目錄:controller
,service
乍炉,mapper
滤馍,entity
,在resources下新建mapper
文件夾
3巢株、添加配置
resources下新建generatorConfig.xml
,最下方table
標(biāo)簽注意改表名和實(shí)體類名困檩,內(nèi)容:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
"http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<!--導(dǎo)入配置文件-->
<properties resource="mybatisGeneratorinit.properties"/>
<!-- 一個數(shù)據(jù)庫一個context -->
<context id="default">
<!-- 注釋生成設(shè)置 -->
<commentGenerator>
<!-- 是否生成注釋代時間戳-->
<property name="suppressDate" value="true"/>
<!-- 是否取消注釋 -->
<property name="suppressAllComments" value="true"/>
</commentGenerator>
<!--jdbc的數(shù)據(jù)庫連接-->
<jdbcConnection driverClass="${jdbc_driver}" connectionURL="${jdbc_url}"
userId="${jdbc_user}" password="${jdbc_password}">
<property name="nullCatalogMeansCurrent" value="true"/>
</jdbcConnection>
<!-- 類型轉(zhuǎn)換 -->
<javaTypeResolver>
<!-- 是否使用bigDecimal悼沿, false可自動轉(zhuǎn)化以下類型(Long, Integer, Short, etc.) -->
<property name="forceBigDecimals" value="false"/>
</javaTypeResolver>
<!-- targetPackage:生成的實(shí)體類所在的包 -->
<!-- targetProject:生成的實(shí)體類所在的硬盤位置 -->
<javaModelGenerator targetPackage="${location_entity}" targetProject="src/main/java">
<!-- 是否允許子包 -->
<property name="enableSubPackages" value="false"/>
<!-- 是否對modal添加構(gòu)造函數(shù) -->
<property name="constructorBased" value="true"/>
<!-- 是否清理從數(shù)據(jù)庫中查詢出的字符串左右兩邊的空白字符 -->
<property name="trimStrings" value="true"/>
<!-- 建立modal對象是否不可改變 即生成的modal對象不會有setter方法骚灸,只有構(gòu)造方法 -->
<property name="immutable" value="false"/>
</javaModelGenerator>
<!-- targetPackage 和 targetProject:生成的 mapper xml 文件的包和位置 -->
<sqlMapGenerator targetPackage="mapper" targetProject="src/main/resources">
<!-- 是否在當(dāng)前路徑下新加一層schema,ex:false路徑com.qikegu.demo.model逢唤, com.qikegu.demo.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
<!-- 是否針對string類型的字段在set的時候進(jìn)行trim調(diào)用 -->
<property name="trimStrings" value="true"/>
</sqlMapGenerator>
<!-- targetPackage 和 targetProject:生成的 java interface 文件的包和位置 -->
<javaClientGenerator type="XMLMAPPER"
targetPackage="${location_mapper}"
targetProject="src/main/java">
<!-- 是否在當(dāng)前路徑下新加一層schema,ex:false路徑com.qikegu.demo.model, com.qikegu.demo.model.[schemaName] -->
<property name="enableSubPackages" value="false"/>
</javaClientGenerator>
<!-- 配置表信息 -->
<!-- schema即為數(shù)據(jù)庫名 tableName為對應(yīng)的數(shù)據(jù)庫表 domainObjectName是要生成的實(shí)體類 enable*ByExample, 是否生成 example類 -->
<!-- 不同的表魔慷,修改tableName和domainObjectName就可以 -->
<table tableName="sharesdata" domainObjectName="Share"
enableCountByExample="false" enableUpdateByExample="false"
enableDeleteByExample="false" enableSelectByExample="false"
selectByExampleQueryId="false">
</table>
</context>
</generatorConfiguration>
resources下新建mybatisGeneratorinit.properties
著恩,內(nèi)容:
#DataSource
#數(shù)據(jù)庫驅(qū)動
jdbc_driver=com.mysql.jdbc.Driver
#數(shù)據(jù)庫鏈接
jdbc_url=jdbc:mysql://localhost:3306/shares?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
#數(shù)據(jù)庫用戶名
jdbc_user=root
#數(shù)據(jù)庫密碼
jdbc_password=123456
#生成類位置
location_entity=com.yzyfdf.shares.entity
location_mapper=com.yzyfdf.shares.mapper
application.yml
中添加:
spring:
datasource:
username: root
password: 123456
url: jdbc:mysql://localhost:3306/db_test1?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC
driver-class-name: com.mysql.jdbc.Driver
mybatis:
mapper-locations: classpath:mapper/*Mapper.xml
type-aliases-package: com.yzyfdf.shares.entity
4、運(yùn)行插件
右側(cè)Maven
- 展開Plugins
- mybatis-genertor
邀摆,雙擊mybatis-generator:generate
,等待編譯施逾,出現(xiàn)下面的就成功了
[INFO] Saving file ShareMapper.xml
[INFO] Saving file Share.java
[INFO] Saving file ShareMapper.java
[INFO] ---------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ---------------------------------------------
[INFO] Total time: 1.141 s
自動生成了三個文件
五例获、補(bǔ)全代碼
1、給ShareMapper.java
類加上@Repository
注解
2榨汤、在service
下新建ShareService
類
import com.yzyfdf.shares.entity.Share;
import com.yzyfdf.shares.mapper.ShareMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class ShareService {
@Autowired
ShareMapper shareMapper;
public Share getShareById(int id) {
return shareMapper.selectByPrimaryKey(id);
}
}
3收壕、在controller
下新建ShareController
import com.yzyfdf.shares.entity.Share;
import com.yzyfdf.shares.service.ShareService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/shares")
public class ShareController {
@Autowired
private ShareService shareService;
@RequestMapping("/{id}")
public Share getShare(@PathVariable int id) {
return shareService.getShareById(id);
}
}
4、給啟動類加上注解@MapperScan("com.yzyfdf.shares.mapper")
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@MapperScan("com.yzyfdf.shares.mapper")
@SpringBootApplication
public class SharesApplication {
public static void main(String[] args) {
SpringApplication.run(SharesApplication.class, args);
}
}
5虫埂、在application.yml
中加上端口
server:
port: 8015
六端壳、啟動
1枪蘑、在啟動類右鍵,選擇Run
照捡,等待啟動完
2话侧、在瀏覽器輸入http://localhost:8015/shares/1
,就能看到數(shù)據(jù)了
七瞻鹏、分環(huán)境配置
1、application.yml
中datasource
部分放入application-dev.yml
2薪夕、再創(chuàng)建一個application-prod.yml
赫悄,內(nèi)容同application-dev.yml
馏慨,用戶名密碼自行修改
3姑隅、application.yml
中加入
spring:
profiles:
active: prod
修改active改變環(huán)境