- 1依赖
依赖包含两部分,jpa+jdbc+mysql 和 thymeleaf+web
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
- 2属性配置
#数据库配置
spring:
datasource:
#添加?useUnicode=true&characterEncoding=utf-8防止中文乱码
url: jdbc:mysql://localhost:3306/myschool?useUnicode=true&characterEncoding=utf-8
username: root
password:
driver-class-name: com.mysql.jdbc.Driver
#创建数据库表
jpa:
hibernate:
ddl-auto: update
#显示sql语句
show-sql: true
#引入国际化
messages:
basename: i18n.messages
#禁止thymeleaf缓存
thymeleaf:
cache: false
- 3实体类
//员工表
@Entity
@Table(name = "emp")
public class Emp {
//主键
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String name;
@Column
private String deptid;
public int getId() {
return id;
}
//部门表
@Entity
@Table(name = "dept")
public class Dept {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
@Column
private String name;
public int getId() {
return id;
}
- 4持久层
public interface DeptDao extends JpaRepository<Dept, Integer> {
}
public interface EmpDao extends JpaRepository<Emp, Integer> {
//自定义sql,nativeQuery = true指定使用原生sql语句
//因为只用于列表展示,所以返回List<map>方便处理
@Query(value = "select e.id as id ,e.name as name,d.id as deptid ,d.name as deptname from emp e ,dept d where e.deptid = d.id", nativeQuery = true)
List<Map<String, String>> findAllVo();
}
- 5控制层
@Controller
public class EmpController {
@Autowired
private EmpDao empDao;
@Autowired
private DeptDao deptDao;
/**
* 遍历列表
*
* @param model
* @return
*/
@RequestMapping("/emps")
public String emps(Model model) {
List<Map<String, String>> allVo = empDao.findAllVo();
model.addAttribute("list", allVo);
return "list";
}
/**
* 跳转到编辑页
*
* @param id
* @param model
* @return
*/
@GetMapping("/emp/{id}")
public String toEdit(@PathVariable("id") Integer id, Model model) {
List<Dept> depts = deptDao.findAll();
Emp emp = empDao.getOne(id);
model.addAttribute("emp", emp);
model.addAttribute("depts", depts);
return "edit";
}
/**
* 跳转到新增页面
*
* @param model
* @return
*/
@GetMapping("/emp")
public String toNew(Model model) {
List<Dept> depts = deptDao.findAll();
model.addAttribute("depts", depts);
model.addAttribute("emp", new Emp());
return "edit";
}
/**
* 保存数据
*
* @param emp
* @return
*/
@PostMapping("/emp")
public String save(Emp emp) {
empDao.save(emp);
return "redirect:/emps";
}
/**
* 删除数据
*
* @param id
* @return
*/
@DeleteMapping("/emp/{id}")
public String delete(@PathVariable("id") Integer id) {
empDao.deleteById(id);
return "redirect:/emps";
}
}
- 6列表页
<table class="table table-striped table-sm">
<thead>
<tr>
<th>id</th>
<th>姓名</th>
<th>部门id</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- 这里的emp不是emp对象而是名叫emp的map-->
<!-- 通过th:each产生遍历效果的tr,注意是加在tr上不是tbody上-->
<tr th:each="emp: ${list}">
<td th:text="${emp.id}">1,001</td>
<td th:text="${emp.name}">Lorem</td>
<td th:text="${emp.deptname}">ipsum</td>
<td>
<a th:href="@{/emp/}+${emp.id}" class="btn btn-sm btn-primary">编辑</a>
<!-- 通过th:attr可以设定任意属性的值 -->
<a href="javascript:void(0);" th:attr="url=@{/emp/}+${emp.id}"
class="btn btn-sm btn-danger">删除</a>
</td>
</tr>
</tbody>
</table>
<!-- 这个form用于删除时复用。_method的值可以取Post/Put/Get/Delete,来提交对应请求-->
<form method="post">
<input type="hidden" name="_method" value="delete">
</form>
<script type="text/javascript">
//处理删除操作
$(function () {
$(".btn-danger").bind("click", function () {
var url = $(this).attr("url");
$("form").attr("action", url)
$("form").submit();
});
})
</script>
- 7内容页
<form th:action="@{/emp}" method="post">
<input type="hidden" th:if="${emp.id}!=0" name="id" th:value="${emp.id}">
<div class="form-group">
<label for="name">姓名</label>
<input type="text" class="form-control" id="name" name="name" th:value="${emp.name}"
placeholder="输入你的姓名">
</div>
<div class="form-group">
<label for="deptid">选择所属的部门</label>
<select class="form-control" id="deptid" name="deptid">
<!-- th:each产生遍历的部门option,th:selected产生选中效果-->
<option th:each="dept:${depts}" th:value="${dept.id }" th:text=" ${dept.name}"
th:selected="${dept.id}==${emp.deptid}">
</option>
</select>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>