springboot从入门到精通(二)
这一节我们一起用springboot开发一个应用程序,应用程序里的核心概念是玩家获取英雄列表上的英雄信息。
1、定义实体模型:
代码如下:
package com.dota.herolist.entity;
import javax.persistence.entity;
import javax.persistence.generatedvalue;
import javax.persistence.generationtype;
import javax.persistence.id;
@entity
public class hero {
private long id;
private string name;
private string type;
private integer bloodvalue;
private integer attack;
/*auto主键由程序控制, 是默认选项 ,不设置就是这个
-identity 主键由数据库生成, 采用数据库自增长, oracle不支持这种方式
-sequence 通过数据库的序列产生主键, mysql 不支持
-table 提供特定的数据库产生主键, 该方式更有利于数据库的移植*/
@id
@generatedvalue(strategy=generationtype.auto)
public long getid() {
return id;
}
public void setid(long id) {
this.id = id;
}
public string getname() {
return name;
}
public void setname(string name) {
this.name = name;
}
public string gettype() {
return type;
}
public void settype(string type) {
this.type = type;
}
public integer getbloodvalue() {
return bloodvalue;
}
public void setbloodvalue(integer bloodvalue) {
this.bloodvalue = bloodvalue;
}
public integer getattack() {
return attack;
}
public void setattack(integer attack) {
this.attack = attack;
}
}
hero类就是简单的java对象,@entity注解表示它是一个jpa实体,id属性为主键,
2、定义持久层,代码如下:
package com.dota.herolist.repository;
import java.util.list;
import org.springframework.data.jpa.repository.jparepository;
import com.dota.herolist.entity.hero;
public interface herolistrepository extends jparepository<hero,long>{
list<hero> findbyname(string name);
}
3、创建web界面:
package com.dota.herolist.controller;
import java.util.list;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.model;
import org.springframework.web.bind.annotation.pathvariable;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.requestmethod;
import com.dota.herolist.entity.hero;
import com.dota.herolist.repository.herolistrepository;
@controller
@requestmapping("/herolist")
public class herolistcontroller {
private herolistrepository herolistrepository;
@autowired
public herolistcontroller(herolistrepository herolistrepository){
this.herolistrepository=herolistrepository;
}
@requestmapping(value="/{name}",method=requestmethod.get)
public string herolist(@pathvariable("name") string name,model model){
list<hero> herolist=herolistrepository.findbyname(name);
if(herolist!=null){
model.addattribute("heros",herolist);
}
return "herolist";
}
@requestmapping(value="/{name}",method=requestmethod.post)
public string addtoherolist(@pathvariable("name") string name,hero hero){
herolistrepository.save(hero);
return "redirect:/herolist/{name}";
}
}
写完controller后在src/main/resources/templates里创建一个名为herolist.html的文件:
<html>
<head>
<title>hero list</title>
<link rel="stylesheet" th:href="@{/style.css}"></link>
</head>
<body>
<h2>dota hero list</h2>
<div th:unless="${#lists.isempty(heros)}">
<dl th:each="hero : ${heros}">
<dt class="heroheadline">
<span th:text="${hero.name}"></span>
<span th:text="${hero.type}"></span>
</dt>
<dd class="herodescription">
<span th:if="${hero.description}" th:text="${hero.description}">
description
</span>
<span th:if="${hero.description eq null}">
no description !
</span>
</dd>
</dl>
</div>
<div th:if="${#lists.isempty(heros)}">
<p>no hero!</p>
</div>
<hr/>
<h3>add a hero</h3>
<form method="post">
<label for="name">name:</label>
<input type="text" name="name" size="50"/><br/>
<label for="type">type:</label>
<input type="text" name="type" size="50"/><br/>
<label for="bloodvalue">bloodvalue:</label>
<input type="text" name="bloodvalue" size="50"/><br/>
<label for="attack">attack:</label>
<input type="text" name="attack" size="50"/><br/>
<label for="description">description:</label>
<textarea type="text" name="description" cols="80" rows="5"/></textarea><br/>
<input type="submit"></input>
</form>
</body>
</html>
再在src/main/resources/static下创建一个css文件:
body{background-color:#cccccc;
font-family:arial,helvetica,sans-serif;
}
.heroheadline{font-size:12pt;font-weight:bold;
}
.herodescription{font-size:10pt;}
label{font-weight:bold;}
然后运行程序,启动成功后,访问路径http://localhost:9090/herolist/zhangfei ,会显示如下界面:
然后可通过表单添加一些信息,如下图:
提交表单,然后访问:
很神奇!我们这节就到这里,下节再分析!
上一篇: 为物联网(IoT)确定一致API
下一篇: 微软崔宏禹:云中漫步—微软云计算揭秘