目录
建站的原因。
首先我们如果需要写文章的话,优快云给了我们很好的平台,但这并不代表不需要自己建站,建站的好处在于,你可以发挥你的任意想象,将你的所学都有一个练手的地方。至少目前我是这么认为,目前自己只是一个程序员小白,不知道还要多久才能完成这个网站,就慢慢来吧。
一、项目搭建
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),在你遇到困扰时给予你及时的帮助,让你从自己的坑洞中快速爬出来,元气满满地重新投入到生活中!