JPA是Java Persistence API的簡稱,中文名Java持久層API,是JDK 5.0註解或XML描述對象-關係表的映射關係,並將運行期的實體對象持久化到數據庫中。
使用Springboot和jpa對數據庫進行操做時,可以大大減小咱們的工做量,在jpa中,已經在底層封裝好了增刪查的功能和sql語句,可使咱們進行快速開發
javascript
#數據源配置 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver spring.datasource.url=jdbc:mysql://localhost:3306/stu?useSSL=false&useUnicode=true&characterEncoding=utf-8&autoReconnect=true&serverTimezone=Asia/Shanghai spring.datasource.username=root spring.datasource.password=root spring.datasource.initialSize=20 spring.datasource.minIdle=50 spring.datasource.maxActive=500 #上下文配置 server.port=8888 server.servlet.context-path=/stu #配置jpa #幫咱們自動生成表結構 spring.jpa.properties.hibernate.hbm2ddl.auto=update spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect spring.jpa.show-sql= true spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
<?xml version="1.0" encoding="UTF-8"?> <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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.4.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.kude.stu</groupId> <artifactId>kudestu</artifactId> <version>0.0.1-SNAPSHOT</version> <name>kudestu</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <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-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- druid數據庫鏈接池--> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.10</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>1.2.10</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
新建一個數據庫stu,建立student表和user表html
package com.kude.stu.kudestu.stu.entity; import javax.persistence.*; import javax.xml.soap.Name; @Entity @Table(name = "student") public class Student { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; private String name; private int age; private String sex; @Override public String toString() { return "Student{" + "id=" + id + ", name='" + name + '\'' + ", age=" + age + ", sex='" + sex + '\'' + '}'; } 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 int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Student() { } }
package com.kude.stu.kudestu.stu.entity; import javax.persistence.*; @Entity @Table(name="user") public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; private String username; private String password; public User() { } @Override public String toString() { return "User{" + "id=" + id + ", username='" + username + '\'' + ", password='" + password + '\'' + '}'; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
package com.kude.stu.kudestu.stu.dao; import com.kude.stu.kudestu.stu.entity.Student; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; import java.util.List; public interface StudentDao extends JpaRepository<Student,Integer> { Student findStuById(Integer id); @Query(name = "findStuByName",nativeQuery = true,value = "select * from student where name=:name ") List<Student> findStuByName(@Param("name") String name); }
package com.kude.stu.kudestu.stu.dao; import com.kude.stu.kudestu.stu.entity.User; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.query.Param; public interface UserDao extends JpaRepository<User,Integer> { /** * 根據id查詢 */ User findUserById(int id); @Query(name = "login",nativeQuery = true,value = "select * from user where username=:username and password=:password") User login(@Param("username") String username,@Param("password") String password); }
package com.kude.stu.kudestu.stu.service; import com.kude.stu.kudestu.stu.entity.Student; import org.springframework.data.domain.Page; import java.util.List; public interface StudentService { Student save(Student student); Student update(Student student); void delete(Integer id); Student findStuById(Integer id); List<Student> findStuByName(String name); /** * 分頁查詢全部數據 * @param page 當前頁 * @param pageSize 每頁記錄數 * @return */ Page<Student> findAll(int page,int pageSize); }
package com.kude.stu.kudestu.stu.service; import com.kude.stu.kudestu.stu.dao.StudentDao; import com.kude.stu.kudestu.stu.entity.Student; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; @Service public class StudentServiceImpl implements StudentService { @Autowired private StudentDao studentDao; @Override public Student save(Student student) { return studentDao.save(student); } @Override public Student update(Student student) { return studentDao.save(student); } @Override public void delete(Integer id) { studentDao.deleteById(id); } @Override public Student findStuById(Integer id) { return studentDao.findStuById(id); } @Override public List<Student> findStuByName(String name) { return studentDao.findStuByName(name); } @Override public Page<Student> findAll(int page, int pageSize) { Pageable pageable = PageRequest.of(page,pageSize); return studentDao.findAll(pageable); } }
package com.kude.stu.kudestu.stu.service; import com.kude.stu.kudestu.stu.entity.User; import java.util.List; public interface UserService { List<User> findAll(); User findUserById(int id); User save(User user); User update(User user); User login(String username,String password); }
package com.kude.stu.kudestu.stu.service; import com.kude.stu.kudestu.stu.dao.UserDao; import com.kude.stu.kudestu.stu.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; @Override public List<User> findAll() { return userDao.findAll(); } @Override public User findUserById(int id) { return userDao.findUserById(id); } @Override public User save(User user) { return userDao.save(user); } @Override public User login(String username, String password) { return userDao.login(username,password); } @Override public User update(User user) { return null; } }
package com.kude.stu.kudestu.stu.controller; import com.kude.stu.kudestu.stu.entity.Student; import com.kude.stu.kudestu.stu.service.StudentService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletResponse; import java.util.List; @RestController @RequestMapping("/s") public class StudentController { @Autowired private StudentService studentService; /** * 添加學生 * @param student 要添加的學生對象 * @return */ @PostMapping("/add") public Student save(Student student){ return studentService.save(student); } /** * 修改學生 * @param student 要修改的學生對象 * @return */ @PostMapping("/update") public Student update(Student student){ return studentService.save(student); } /** * 刪除學生 * @param id 要刪除學生對象的id */ @GetMapping("/del/{id}") public String del(@PathVariable Integer id){ studentService.delete(id); return "yes"; } /** * 經過姓名查詢學生信息 * @param name 要查詢的學生的姓名 * @return */ @GetMapping("/findByName/{name}") public List<Student> findStuByName(@PathVariable String name){ return studentService.findStuByName(name); } /** * 分頁查詢 * @param page * * @return */ @GetMapping("/query") public Page<Student> findByPage(Integer page, HttpServletResponse response){ response.setHeader("Access-Control-Allow-Origin","*"); if(page==null||page<=0){ page=0; }else { page -=1; } return studentService.findAll(page,5); } }
package com.kude.stu.kudestu.stu.controller; import com.kude.stu.kudestu.stu.entity.User; import com.kude.stu.kudestu.stu.service.UserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import java.util.List; /** *處理用戶信息的控制層 * @author 張玉賢 * */ @RestController @RequestMapping("/user") public class UserController { @Autowired private UserService userService; /** *查詢用戶的方法 * */ @RequestMapping(value = "/findAll",method = RequestMethod.POST) public List<User> findAll(){ return userService.findAll(); } /** * 根據id查詢用戶 */ @RequestMapping(value = "/findById") public User findUserById(int id){ return userService.findUserById(id); } /** * 註冊用戶 */ @RequestMapping(value = "/reg",method = RequestMethod.POST) public User reg(User user){ return userService.save(user); } /** * 用戶登陸 */ @PostMapping("/login") public User login(String username,String password){ return userService.login(username,password); } }
新建一個項目前端
百度搜索菜鳥教程並打開java
在js目錄下新建一個jquery.jsmysql
將全部內容全選複製粘貼進js目錄下的jquery.js中保存便可jquery
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> <script src="js/jquery.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> var page = 1; var tp = 0; $(function(){ showData(); }); function upPage(){ if(page>1){ page--; showData(); } } function downPage(){ if(page<tp){ page++; showData(); } } function showData(){ $.ajax({ url:'http://localhost:8888/stu/s/query?page='+page, success:function(result){ var rel = result.content; tp = result.totalPages; var htmlStr = "<table width='80%' algin='center' border='1'>"+ "<tr><th>ID</th><th>姓名</th><th>年齡</th><th>性別</th><th>操做</th></tr>"; for(var i=0;i<rel.length;i++){ var stu = rel[i]; htmlStr += "<tr><td>"+stu.id+"</td><td>"+stu.name+"</td><td>"+stu.age+"</td><td>"+stu.sex+"</td>"+ "<td><a href=''>修改</a> <a href='http://localhost:8888/stu/s/del/"+stu.id+"'>刪除</a></td></tr>"; } htmlStr += "</table>"; $("#show").html(htmlStr); } }); } </script> </head> <body> <h1 style="text-align: center;">學生管理系統</h1> <hr> <a href="add.html">添加學生</a> <div id="show"> </div> <a href="javascript:upPage();">上一頁</a> <a href="javascript:downPage();">下一頁</a> </body> </html>
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title></title> </head> <body> <h1>添加學生</h1> <hr> <form action="http://localhost:8888/stu/s/add" method="post"> <p> 姓名:<input type="text" name="name" /> </p> <p> 年齡:<input type="text" name="age" /> </p> <p> 性別: <input type="radio" name="sex" value="男" checked />男 <input type="radio" name="sex" value="女" />女 </p> <p> <input type="submit" value="保存" /> </p> </form> </body> </html>