基于springboot的Spring-Data-JPA
注意:代码不全,只粘贴重要代码。整体架构springboot+spring-data-jpa+freemarker
application.properties
#打印查询sql
spring.jpa.show-sql=true
1.pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
2.User.java
package com.niugang.entity;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
//javax.persistence.Entity
@Entity
@Table(name="user")
public class User {
/**
*mysql 自定生成主键
*指示持久性提供者必须分配使用数据库标识列的实体的主键
*/
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="id" ,insertable=false,updatable=false)
private Integer id;
/**
* @Column中的name默认和字段名相同
*/
@Column(name = "name")
private String name;
@Column(name = "age")
private Integer age;
@Column(name = "phone")
private String phone;
@Column(name = "password")
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
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;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
3.UserDao.java
package com.niugang.dao;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import com.niugang.entity.User;
/**
* JpaRepository springboot已经自动配置了,也已经注入数据源了
* @author niugang
*
*/
@Repository
public interface UserDao extends JpaRepository<User,Integer> {
}
4.UserService.java
package com.niugang.service;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;
import com.niugang.dao.UserDao;
import com.niugang.entity.User;
@Service
public class UserService {
@Resource
private UserDao userDao;
/**
* 查询全部
*
* @return
*/
public List<User> queryList() {
//查询所有
return userDao.findAll();
}
/**
* 保存
*
* @param user
*/
public void save(User user) {
//添加对象
userDao.save(user);
}
/**
* 根据id查询
*
* @param id
* @return
*/
public User get(int id) {
//根据id获取对象
return userDao.getOne(id);
}
/**
* 删除
*
* @param id
*/
public void delete(int id) {
//删除对象
userDao.delete(id);
}
/**
* page分页查询
* @return
*/
public Page<User> queryForPage(Integer page) {
/**
* PageRequest page起始页从0开始
*/
PageRequest pageRequest = null;
if (page== null) {
pageRequest = new PageRequest(0, 4);
} else {
pageRequest = new PageRequest((page-1), 4);
}
Page<User> findAll = userDao.findAll(pageRequest);
return findAll;
}
}
5.IndexController.java
package com.niugang.controller;
import java.util.List;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.niugang.entity.User;
import com.niugang.service.UserService;
/**
*
* @author niugang spring boot helloWrold
*/
@Controller
public class IndexController {
@Resource
private UserService userService;
/**
* 跳转到index页面
*
* @return
*/
@RequestMapping(value = "/index")
public String index(ModelMap map) {
List<User> list = userService.queryList();
map.put("users", list);
return "index";
}
/**
* 详情页面
* @param id
* @param map
* @return
*/
@RequestMapping(value = "/detail/{id}")
public String detail(@PathVariable(value="id") Integer id,ModelMap map){
User user = userService.get(id);
map.put("user", user);
return "detail";
}
/**
* 删除
* @param id
* @return
*/
@RequestMapping(value = "/delete/{id}")
public String delete(@PathVariable(value="id") Integer id){
userService.delete(id);
return "redirect:/index";
}
/**
* 跳转到添加页面
* @param map
* @return
*/
@RequestMapping(value = "/save",method = RequestMethod.GET)
public String toSave(ModelMap map) {
return "add";
}
/**
* 保存添加信息
* @param user
* @return
*/
@RequestMapping(value = "/save",method = RequestMethod.POST)
public String save(User user,ModelMap map){
if(StringUtils.isBlank(user.getName())){
map.put("error", "用户名不能为空");
return "add";
}
if(user.getAge()==null){
map.put("error", "非法年龄");
return "add";
}
if(StringUtils.isBlank(user.getPhone())){
map.put("error", "手机号不能为空");
return "add";
}
user.setPassword("123456");
userService.save(user);
return "redirect:/index";
}
@RequestMapping(value = "/indexPage")
public String indexPage(ModelMap map,Integer page) {
Page<User> queryForPage = userService.queryForPage(page);
map.put("page", queryForPage);
return "index";
}
}
6.index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<style type="text/css">
table {
border-collapse: collapse;
margin: 0 auto;
}
table, td, th {
border: 1px solid black;
padding: 15px;
}
</style>
</head>
<body>
<input type="button" value="添加" onclick="add();">
<a href="indexPage">分页查询</a>
<table>
<thead>
<tr>
<th>用户名</th>
<th>年龄</th>
<th>电话</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<#if users??> <#list users as user>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.phone}</td>
<td><a href="javascript:void(0)" onclick="edit('${user.id}')" >编辑</a>
<a href="javascript:void(0)" onclick="del('${user.id}')">删除</a>
<a href="javascript:void(0)" onclick="detail('${user.id}')">详情</a>
</td>
</tr>
</#list> </#if>
<#if page??>
<#list page.content as user>
<tr>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.phone}</td>
<td><a href="javascript:void(0)" onclick="edit('${user.id}')" >编辑</a>
<a href="javascript:void(0)" onclick="del('${user.id}')">删除</a>
<a href="javascript:void(0)" onclick="detail('${user.id}')">详情</a>
</td>
</tr>
</#list>
<tr>
<td>总页数</td><td>${page.totalPages}</td>
<td>当前页面</td><td>${page.number+1}</td>
<td>总记录数</td><td>${page.totalElements}</td>
</tr>
<tr>
<td>
<#if (page.totalPages gt page.number+1)>
<a href="indexPage?page=${page.number+2}">下一页</a>
<#if (page.number !=0)>
<a href="indexPage?page=${page.number}">上一页</a>
</#if>
</#if>
<#if (page.totalPages lt page.number+2)>
<a href="indexPage?page=${page.number}">上一页</a>
</#if>
</td>
</tr>
</#if>
</tbody>
</table>
<script type="text/javascript">
function edit(id){
alert(id);
}
function del(id){
var flag=confirm("你确定要删除此选项吗");
if(flag==true){
window.location.href="delete/"+id;
}
}
function detail(id){
window.location.href="detail/"+id;
}
function add(){
window.location.href="save";
}
</script>
</body>
</html>
7.detail.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<h2>详情页面</h2>
用户名:<input type="text" readonly value="${user.name!''}">
年龄:<input type="text" readonly value="${user.age!''}">
电话:<input type="text" readonly value="${user.phone!''}">
</body>
</html>
微信公众号