Spring Boot整合Spring-data-JPA
阿新 • • 發佈:2018-12-18
一.整合Spring-data-JPA
- JPA(Java Persistence API的簡稱,中文名Java持久層API)
- 基於註解來去配置對映,實體類,資料庫表的規範(增刪改查的規範)
1.需求
- 使用Spring Boot + Spring MVC + Spring Data JPA + EasyUI 框架組合實現部門列表查詢
- 先建立一個數據庫,並加入點資料來進行操作
DROP TABLE IF EXISTS `user`; CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `user_name` varchar(100) DEFAULT NULL COMMENT '使用者名稱', `password` varchar(100) DEFAULT NULL COMMENT '密碼', `name` varchar(100) DEFAULT NULL COMMENT '姓名', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8; INSERT INTO `user` VALUES ('1', 'zhangsan', '123456', '張三'); INSERT INTO `user` VALUES ('2', 'lisi', '123456', '李四'); INSERT INTO `user` VALUES ('3', 'wangwu', '123456', '王五'); INSERT INTO `user` VALUES ('4', 'zhangwei', '123456', '張偉'); INSERT INTO `user` VALUES ('5', 'lina', '123456', '李娜'); INSERT INTO `user` VALUES ('6', 'lilei', '123456', '李磊');
2.建立Maven工程(打jar包)
- 在pom.xml中進行如下配置
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>it.playmaker</groupId> <artifactId>SpringBoot-play</artifactId> <version>0.0.1-SNAPSHOT</version> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <!--這些地方不寫版本號的原因在於會自動匹配合適版本--> </plugin> </plugins> </build> </project>
3.加入Spring-Boot配置檔案
- application.properties
- 此檔案用於覆蓋Spring Boot的預設配置
#DB Configuration: spring.datasource.driverClassName=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://127.0.0.1:3306/xxxx?characterEncoding=utf8 spring.datasource.username=root spring.datasource.password=xxxxx #JPA Configuration: spring.jpa.database=mysql spring.jpa.show-sql=true spring.jpa.generate-ddl=true #是否自動生成ddl spring.jpa.hibernate.ddl-auto=update #生成ddl的方式 spring.jpa.hibernate.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy #選擇的命名策略
(1).後端實現
- 建立實體類
@Entity
@Table(name="s_user")//對應表
public class User {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)//自增長
private Long id;
@Column(name="user_name")//在windows系統下mysql不區分大小寫。在Linux系統下,區分大小寫
private String user_name;
@Column(name="password")
private String password;
@Column(name="name")
private String name;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getUser_name() {
return user_name;
}
public void setUser_name(String user_name) {
this.user_name = user_name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User [id=" + id + ", user_name=" + user_name + ", password=" + password + ", name=" + name + "]";
}
}
- 建立DAO介面
/*
* 使用者持久層介面
*/
@Repository("userDao")
public interface IUserDao extends JpaRepository<User,Long>{//在持久層介面:如果用Spring data-JPA不想寫一些沒用方法
這個時候簡便方式,讓JPA來實現,繼承一個介面。引數分別是你要操作哪個類,你的實體類主鍵資料型別
/*
* 查詢所有使用者
*/
//List<User> findAll(); 繼承了這個介面就不用寫了
}
- 建立業務邏輯介面
public interface IUserService {
//查詢所有使用者
List<User> findAllUser();
}
- 建立業務邏輯實現類
@Service("userService")
public class UserServiceImpl implements IUserService {
@Autowired
private IUserDao userDao;
@Override
public List<User> findAllUser() {
// TODO Auto-generated method stub
return userDao.findAll();
}
}
- 建立Controller
/*
* 使用者的控制器
*/
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private IUserService userService;
@RequestMapping("/findAll")
public List<User> findAllUser(){
List<User> users = userService.findAllUser();
return users;
}
}
- 建立引導類(注意引導類所放的包)
/*
* spring boot引導類
*/
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
- 開啟瀏覽器訪問
http://localhost:8080/user/findAll
- 頁面顯示的就是JSON格式的資料
(2).前端實現
- Spring Boot支援我們編寫html,jsp檔案的
- user.html(要放在配置檔案下的static資料夾中)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" content="text/html;charset=UTF-8">
<title>人員資訊</title>
<link rel="stylesheet" type="text/css" href="ui/themes/default/easyui.css">
<script type="text/javascript" src="ui/jquery.min.js"></script>
<script type="text/javascript" src="ui/jquery.easyui.min.js"></script>
<script type="text/javascript" src="ui/locate/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">
$(function() {
$('#grid').datagrid({
url:'user/findAll',//要與控制器下的地址一致
columns:[[{
field:'id',
title:'編號',
width:50
},{
field:'name',
title:'姓名',
width:100
},{
field:'user_name',
title:'賬號',
width:200
}]]
});
});
</script>
</head>
<body>
<table id="grid"></table>
</body>
</html>
- 之後我們直接訪問
http://localhost:8080/user.html