目錄
建站的原因。
首先我們如果需要寫文章的話,CSDN給了我們很好的平臺,但這並不代表不需要自己建站,建站的好處在於,你可以發揮你的任意想象,將你的所學都有一個練手的地方。至少目前我是這麼認爲,目前自己只是一個程序員小白,不知道還要多久才能完成這個網站,就慢慢來吧。
一、項目搭建
1.創建SpringBoot項目。
創建maven項目,這我就不上圖了,隨便看一下有對應的文章,我就直接貼pom文件的依賴和插件代碼了。每個jar包都有對應的註釋
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<!-- 熱啓動 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- JSTL for JSP spring boot搭建web項目,跳轉到jsp一定要添加下面的jar包,否則出現跳轉的時候,變成下載的功能,保存該頁面 -->
<!-- <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId>
</dependency> Need this to compile JSP <dependency> <groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency> -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<!--引入數據庫驅動 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!--引入druid數據源 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.12</version>
</dependency>
<!--spring整合mybatis 暫時 -->
<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>1.3.2</version>
</dependency>
<!--springboot測試 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
<version>1.2.5.RELEASE</version>
</dependency>
</dependencies>
</plugin>
<!--跳過測試類打包 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</build>
需要注意的是:其中有一個關於jsp依賴被我註釋了,因爲我原本的打算是使用html來作爲自己的頁面主要元素,這個就不多說了。
2.application.yml文件
server:
port: 9001
servlet:
context-path: /
spring:
mvc:
view:
prefix: /pages/
suffix: .html
datasource:
#引入druid數據源
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://本地數據庫serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true
username: root
password: root
mybatis:
type-aliases-package: pojo包路徑
mapper-locations: classpath:/mybatis/mappers/*.xml
#開啓駝峯映射
configuration:
map-underscore-to-camel-case: true
3.看一下頁面及mapper文件的位置,
4.配置之後,測試頁面和數據庫是否整合成功
測試頁面跳轉:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
hello world!!!
welcome!
</body>
</html>
Controller
@Controller
public class PageController {
@RequestMapping("/home")
public String home() {
System.out.println("去首頁");
return "index";
}
}
5.測試結果
二.數據庫測試
1. 測試數據庫連接
首先創建一個數據庫,在數據庫中創建一張tuser表
CREATE TABLE `tuser` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`password` varchar(18) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;
在項目中添加User對象
//此處使用了lombox插件,所以只需添加註解,在項目運行時會自動爲你生成getter、setter等一系列方法,chain 表示是否是鏈式結構
@Data
@Accessors(chain = true)
public class User {
private Integer id;
private String name;
private Integer password;
}
dao接口文件
public interface UserMapper {
List<User> findAll();
}
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.pri.blog.自己根據自己路徑來寫.dao.UserMapper">
<!--返回值需要添加別名包 -->
<select id="findAll" resultType="User">
select * from tuser
</select>
</mapper>
2.編寫springBoot測試類
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestMybatis {
@Autowired
private UserMapper userMapper;
@Test
public void findAll() {
List<User> res = userMapper.findAll();
System.out.println(res);
}
}
3.測試結果
大功告成,下次有時間了去找一些界面,看看哪個比較順眼,開始做,在做的過程中如果想加入什麼功能會再次補充
鹹魚IT技術交流羣:89248062,在這裏有一羣和你一樣有愛、有追求、會生活的朋友! 大家在一起互相支持,共同陪伴,讓自己每天都活在豐盛和喜樂中!同時還有龐大的小夥伴團體(sha diao wang you),在你遇到困擾時給予你及時的幫助,讓你從自己的坑洞中快速爬出來,元氣滿滿地重新投入到生活中!