本文章為 一名Android 小白的 自學(xué) 搭建 五天的 總結(jié)俐银,肯定會(huì)有不對(duì)的 或者不規(guī)范的地方驯杜,如果有 還望指出,本人 迫切需要這方面大佬指點(diǎn)與學(xué)習(xí)
1. Sprint Boot 基礎(chǔ)搭建
通過(guò) idea 的創(chuàng)建新工程玩般,即可添加spring boot 的工程
1.1 打開(kāi)idea爆袍,左上角File→New→Project,通過(guò)彈出框在左側(cè)選擇 spring initializr,右側(cè)選擇 jdk 版本號(hào)坞琴,本人為 1.8版本哨查。
1.2 next后 可以 選擇 type:maven ,jre :1.8本地版本 剧辐。next
1.3 我們選擇數(shù)據(jù)庫(kù)MySQL和持久層框架MyBatis寒亥,next
1.4 選擇好項(xiàng)目的目錄 我們就可以 點(diǎn)擊Finish,開(kāi)始構(gòu)建
放一個(gè)項(xiàng)目目錄
ThingApplicttion.java 為項(xiàng)目的啟動(dòng)類
application.properties 為項(xiàng)目配置文件
pom.xml 主要描述了項(xiàng)目的maven坐標(biāo)荧关,依賴關(guān)系溉奕,項(xiàng)目所需jar包的管理。
2. 配置數(shù)據(jù)庫(kù)清單
在application.properties文件中添加如下數(shù)據(jù)庫(kù)配置
spring.datasource.url=jdbc:mysql://localhost:3306/(數(shù)據(jù)庫(kù)名稱)?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&autoReconnect=true&failOverReadOnly=false
spring.datasource.username=(數(shù)據(jù)庫(kù)用戶名)
spring.datasource.password=(數(shù)據(jù)庫(kù)密碼)
spring.datasource.driverClassName=com.mysql.jdbc.Driver
3. 創(chuàng)建測(cè)試表
CREATE TABLE `user_info` (
`id` int(32) NOT NULL AUTO_INCREMENT,
`user_name` varchar(255) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
4. 配置其他包
4.1 model
在根目錄下創(chuàng)建model包忍啤,創(chuàng)建實(shí)體類對(duì)象
public class UserInfo {
/**
* primary key
*/
@Id
private String id;
/**
* username
*/
@Column(name = "user_name")
private String userName;
private String password;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
4.2 Mapper
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.vip.things.dao.UserInfoMapper">
<resultMap id="BaseResultMap" type="com.vip.things.model.UserInfo">
<id column="id" jdbcType="INTEGER" property="id"/>
<result column="user_name" jdbcType="VARCHAR" property="userName"/>
</resultMap>
<sql id="Base_Column_List">
id,user_name
</sql>
<select id="selectById" parameterType="java.lang.Integer" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from user_info
where id = #{id,jdbcType=VARCHAR}
</select>
</mapper>
4.3 Dao
在根目錄創(chuàng)建 dao 目錄加勤,然后創(chuàng)建 dao層文件
public interface UserInfoMapper {
UserInfo selectById(@Param("id") Integer id);
}
4.4 Service
public interface UserInfoService {
UserInfo selectById(Integer id);
}
添加Service層 接口實(shí)現(xiàn)
@Service
public class UserInfoServiceImpl implements UserInfoService {
@Resource
private UserInfoMapper userInfoMapper;
public UserInfo selectById(Integer id){
return userInfoMapper.selectById(id);
}
}
4.5 Controller
@RestController
@RequestMapping("userInfo")
public class UserInfoController {
@Resource
private UserInfoService userInfoService;
@PostMapping("/hello")
public String hello(){
return "hello SpringBoot";
}
@PostMapping("/selectById")
public UserInfo selectById(Integer id){
return userInfoService.selectById(id);
}
}
Controller中@RestController注解的作用:
@RestController是由@Controller和@ResponseBody組成,表示該類是controller和返回的結(jié)果為JSON數(shù)據(jù)同波,不是頁(yè)面路徑鳄梅。
4.6 configurer
在根目錄下創(chuàng)建 core.configurer 目錄,添加 mapper 配置解析文件
@Configuration
public class MyBatisConfigurer {
@Bean
public SqlSessionFactory sqlSessionFactoryBean(DataSource dataSource) throws Exception {
SqlSessionFactoryBean factory = new SqlSessionFactoryBean();
factory.setDataSource(dataSource);
factory.setTypeAliasesPackage("com.vip.things.model");
// 添加X(jué)ML目錄
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));
return factory.getObject();
}
@Bean
public MapperScannerConfigurer mapperScannerConfigurer() {
MapperScannerConfigurer mapperScannerConfigurer = new MapperScannerConfigurer();
mapperScannerConfigurer.setSqlSessionFactoryBeanName("sqlSessionFactoryBean");
mapperScannerConfigurer.setBasePackage("com.vip.things.dao");
return mapperScannerConfigurer;
}
}
@Configuration表示該文件是一個(gè)配置文件
@Bean表示該方法是一個(gè)傳統(tǒng)xml配置文件中的/Bean id="">"”/Bean>
其中factory.setTypeAliasesPackage("com.example.demo.model")表示項(xiàng)目中model的存儲(chǔ)路徑未檩;
factory.setMapperLocations(resolver.getResources("classpath:mapper/*.xml"));表示mapper.xml存儲(chǔ)路徑戴尸;
mapperScannerConfigurer.setBasePackage("com.example.demo.dao");表示dao層的存儲(chǔ)路徑
1:運(yùn)行
2:調(diào)試
3:關(guān)閉
通過(guò)選擇 我們的 啟動(dòng)項(xiàng) application ,然后 就可以啟動(dòng)了冤狡。
我們可以通過(guò) Postman 簡(jiǎn)單的進(jìn)行調(diào)試
調(diào)試成功校赤,接下來(lái) 將會(huì)記錄 我的第二步開(kāi)發(fā)過(guò)程,netty 客戶端和 服務(wù)端的搭建筒溃。如果讀者發(fā)現(xiàn)那里有問(wèn)題马篮,還望指出。一起學(xué)習(xí)進(jìn)步