一、 創(chuàng)建spring-boot項(xiàng)目
前置條件
idea
java環(huán)境
tomcat (解決坑用)
mysql
使用官方初始化工具
1、直接使用默認(rèn)default,也可以搭建私服模板
2、 使用選擇gradle,其他可以保持默認(rèn)
3、 勾選依賴
4、配置好路徑,F(xiàn)inish
5、 選擇圖上兩項(xiàng),自動(dòng)創(chuàng)建一些目錄,以及使用默認(rèn)gradle wrapper
6、 如果網(wǎng)速好,此處可以無視,安裝時(shí)候卡在下載gradle這一步
解決方法
1、手動(dòng)下載對(duì)應(yīng)版本gradle,放到tomcat目錄里面
webapps\ROOT
,啟動(dòng)tomcat
2、找到gradle-wrapper.properties文件,修改到本地路徑
image.png
3、重啟idea,右側(cè)邊欄出現(xiàn)Gradle圖標(biāo),說明安裝成功了
image.png
7、點(diǎn)刷新按鈕下載依賴,如果下載不了,可以替換maven倉庫
8、到此位置,依賴環(huán)境搭建完成,可以愉快的寫代碼了 —— so easy,媽媽再也......
二、使用Mybatis+MVC
1、數(shù)據(jù)庫建表及填充數(shù)據(jù)
2、 建立如下目錄結(jié)構(gòu) —— 推薦這種目錄結(jié)構(gòu),畢竟官方包也是這么玩的
controller – Controller 層
dao – 數(shù)據(jù)操作層 DAO
domain – 實(shí)體類
mapper – 注解方式的配置文件
service – 業(yè)務(wù)邏輯層
Application – 應(yīng)用啟動(dòng)類
3、編寫domain、dao、application、service、controller文件
domian/City.java
package com.example.domain;
import java.io.Serializable;
/**
* Created by user on 2017/5/5.
*/
public class City implements Serializable {
private static final long serialVersionUID = 1L;
private Long id;
private String name;
private String state;
private String country;
public Long getId() {
return this.id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getState() {
return this.state;
}
public void setState(String state) {
this.state = state;
}
public String getCountry() {
return this.country;
}
public void setCountry(String country) {
this.country = country;
}
@Override
public String toString() {
return getId() + "," + getName() + "," + getState() + "," + getCountry();
}
}
dao/CityDao.java
package com.example.dao;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Component;
import com.example.domain.City;
import javax.annotation.Resource;
/**
* Created by user on 2017/5/5.
*/
@Component
public class CityDao {
@Resource
private SqlSession sqlSession;
public City selectCityById(long id) {
return sqlSession.selectOne("selectCityById", id);
}
}
service/CityService.java
package com.example.service;
import com.example.domain.City;
import com.example.dao.CityDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by user on 2017/5/5.
*/
@Service
public class CityService {
@Autowired
private CityDao cityDao;
public City selectCityById(int id){
return cityDao.selectCityById(id);
}
}
controller/DemoController.java
package com.example.controller;
import com.example.domain.City;
import com.example.service.CityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Created by user on 2017/5/5.
*/
@Controller
public class DemoController {
@Autowired
private CityService cityService;
@RequestMapping("/")
@ResponseBody
public City index() {
return cityService.selectCityById(1);
}
}
DemoApplication.java
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
4、mapper我采取配置的方式,因?yàn)橐院罂梢杂霉ぞ呱?[捂臉]
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2015-2016 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.example.mapper.CityMapper">
<select id="selectCityById" resultType="City">
select * from city where id = #{id}
</select>
</mapper>
5、在application.properties配置數(shù)據(jù)源
我用mysql5.7需要帶上useSSL防止報(bào)warn
spring.datasource.url = jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=true
spring.datasource.username = work
spring.datasource.password = 123456
mybatis.config-location=classpath:mybatis-config.xml
6、 配置mybatis-config.xml,mapper文件在這里指定路徑
<?xml version="1.0" encoding="UTF-8" ?>
<!--
Copyright 2015-2016 the original author or authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<typeAliases>
<package name="com.example.domain"/>
</typeAliases>
<mappers>
<mapper resource="com/example/mapper/CityMapper.xml"/>
</mappers>
</configuration>
三、Run起來
點(diǎn)這個(gè)
臥槽......
加依賴
刷新再run,用自帶的工具測(cè)試,Tools -> Test RESTful Web Service(這個(gè)碉堡,省了postman)
完美!!!!
四、小節(jié)
- 構(gòu)建工具使用了Gradle,擴(kuò)展性強(qiáng),可以滿足項(xiàng)目后期需要。
- 整個(gè)構(gòu)建基于最新的組件搭建,選用Spring-Boot,Spring-MVC 和
Mybatis 也是使用starter來構(gòu)建完成,基本實(shí)現(xiàn)了開箱即用,隨官方更新迭代風(fēng)險(xiǎn)小 - 代碼及目錄結(jié)構(gòu)采取規(guī)范形式,方便后續(xù)擴(kuò)展,大型項(xiàng)目,在example下一級(jí)增加子項(xiàng)目目錄即可劃分工程。
- mybatis采取xml配置形式,可以利用現(xiàn)有mybatsi-generator工具進(jìn)行代碼生成,方便后續(xù)開發(fā)
- 使用@autowired 和 @Resource 方式自動(dòng)注入,充分利用了框架的簡(jiǎn)化優(yōu)勢(shì),開發(fā)更容易
后記:新手第一次搭建這套開發(fā)環(huán)境,踩了好多坑,陸陸續(xù)續(xù)搞了兩天,記錄過程防止遺忘,也希望對(duì)同行新手有幫助~