ZB-045-01Spring生态系统

目标

  • 在 Spring 容器中引入 Bean
  • Spring + Mybatis(H2/MySQL/Postgres)
  • 模版引擎 后端渲染HTML
  • 前后端分离和后端渲染

初始项目结构如下仓库内容

后端开发三层

  • Controller
  • Service
  • Dao
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    HTTP
^
| |
v
________________
Controller 只做和HTTP请求相关

Service 具体的业务逻辑 业务代码

Dao 只做数据库
________________
^
| |
v
db数据库

配置MyBatis

001MyBatis依赖

1
2
3
4
5
6
7
8
9
搜索 springboot mybatis starter
得到 https://mybatis.org/spring-boot-starter/mybatis-spring-boot-autoconfigure/
找到依赖,添加到pom.xml

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
<version>2.1.1</version>
</dependency>

002数据库依赖,我们先用h2

1
2
3
4
5
6
7
8
9
10
11
12
搜索 h2 maven
得到 https://mvnrepository.com/artifact/com.h2database/h2/1.4.200
找到依赖,添加到pom.xml

<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.200</version>
<!--注释掉这句 -->
<!--<scope>test</scope>-->
</dependency>

003添加 src/main/resources/application.properties

1
2
3
4
5
spring.datasource.url=jdbc:h2:file:./target/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.h2.Driver
mybatis.config-location=classpath:db/mybatis/config.xml

004 新建数据库初始化文件

src/main/resources/db/migration/V1__CreateTables.sql

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
create table user(
id bigint primary key auto_increment,
name varchar(100)
);

create table match(
id bigint primary key auto_increment,
user_id bigint,
score int
);

insert into user(id,name) values(1,'AAA');
insert into user(id,name) values(2,'BBB');
insert into user(id,name) values(3,'CCC');


insert into match(id,user_id,score) values(1,1,1000);
insert into match(id,user_id,score) values(2,1,2000);
insert into match(id,user_id,score) values(3,2,500);
insert into match(id,user_id,score) values(4,3,300);

005 安装 flyway 数据库迁移工具

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# pom.xml 里添加
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.0.7</version>
</plugin>


#配置用户名密码
<plugin>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-maven-plugin</artifactId>
<version>6.0.7</version>
<configuration>
<url>jdbc:h2:file:${project.basedir}/target/test</url>
<user>root</user>
<password>root</password>
</configuration>
</plugin>

006点击maven里 plugin里的 flyway –> migrate 运行 此时数据库初始化完成

007 配置 MyBatis

  • 修改src/main/resources/application.properties.xml
1
2
3
4
5
6
spring.datasource.url=jdbc:h2:file:.target/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=org.h2.Driver
# 配置mybatis
mybatis.config-location=classpath:db/mybatis/config.xml
  • 新建 src/main/resources/db/mybatis/config.xml
1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<setting name="logImpl" value="LOG4J"/>
</settings>
<mappers>
<mapper resource="db/mybatis/MyMapper.xml"/>
</mappers>
</configuration>

008创建 Mapper和 Entity

  • 创建 src/main/java/dao/UserMappper.java
1
2
3
4
5
@Mapper
public interface UserMapper {
@Select("select * from user where id = #{id}")
public User getUserById(@Param("id") Integer id);
}
  • 创建 src/main/java/entity/User.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
public class User {
private Integer id;
private String name;

public Integer getId() {
return id;
}

public void setId(Integer id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

009 新建 src/main/resources/db/mybatis/MyMapper.xml

  • 搜 mybatis 入门文档抄过来
1
2
3
4
5
6
7
<?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="org.mybatis.example.BlogMapper">
<!-- 你是注解方式写的sql -->
</mapper>

010 修改 config.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--<settings>-->
<!--<setting name="logImpl" value="LOG4J"/>-->
<!--</settings>-->
<mappers>
<mapper resource="db/mybatis/MyMapper.xml"/>
<mapper class="hello.dao.UserMapper"/>
</mappers>
</configuration>

如何在 Spring 中使用 Bean

@Service

1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class RankService {
@Autowired
private UserMapper userMapper;

@Autowired
RankDao rankDao;

public void doService(){

}
}

待更新

模版引擎

搜索freemarker springboot starter

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>

新建 rescources/templates/index.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<head>
<title>排行榜</title>
</head>
<body>
<h1>排行榜!</h1>
<table>
<tr>
<th>排名</th>
<th>名字</th>
<th>分数</th>
</tr>

<#list items as item>
<tr>
<td>${item?index}</td>
<td>${item.user.name}</td>
<td>${item.score}</td>
</tr>
</#list>

</table>
</body>

HelloController.java

1
2
3
4
5
6
7
8
@RequestMapping("/rank2")
public ModelAndView rank2() {
List<RankItem> items = rankService.doService();
HashMap<String,Object> model = new HashMap<>();
model.put("items",items);
// 自动去 templates目录找 index.ftl
return new ModelAndView("index",model);
}