springboot使用Jpa连接数据库

1.pom.xml:

<?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> <groupId>com.example</groupId>
<artifactId>spring-data_JpaAndJdbcTemplate</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>spring-mybatis</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.10.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.21</version>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

2.application.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

3.Student:

package com.example.springmybatis.entity;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id; @Entity
public class Student { @Id
@GeneratedValue
private String name;
@Column(nullable = true)
private int age;
@Column(nullable = true)
private String address; private String collage; private String sex; 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 getAddress() {
return address;
} public void setAddress(String address) {
this.address = address;
} public String getCollage() {
return collage;
} public void setCollage(String collage) {
this.collage = collage;
} public String getSex() {
return sex;
} public void setSex(String sex) {
this.sex = sex;
} @Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", address='" + address + '\'' +
", collage='" + collage + '\'' +
", sex='" + sex + '\'' +
'}';
}
}

4.UserRepository:

package com.example.springmybatis.service;

import com.example.springmybatis.entity.Student;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository; import java.util.List; public interface UserRepository extends JpaRepository<Student, Long> { List<Student> findByName(String name); Student findByNameAndAge(String name, Integer age); int findByAge(int age); Page<Student> findAll(Pageable pageable); }

5.UserController:

package com.example.springmybatis.Controller;

import com.example.springmybatis.entity.Student;
import com.example.springmybatis.service.UserRepository;
import com.example.springmybatis.service_jdbc.UserService;
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.data.domain.Sort;
import org.springframework.web.bind.annotation.*; import java.util.List; @RestController
public class UserController { @Autowired
private UserRepository userRepository;
/**
* spring-data-jpa连接数据库
* @param name
* @param age
* @return
* @throws Exception
*/
@RequestMapping(value = "/name", method = RequestMethod.GET)
@ResponseBody
public Student GetGoodList(@RequestParam("name") String name, @RequestParam(value = "age", required = false,
defaultValue = "27") int age)throws Exception{ Student user = userRepository.findByNameAndAge(name, age);
return user;
} @RequestMapping(value = "/names", method = RequestMethod.GET)
@ResponseBody
public List<Student> GetName(@RequestParam(value = "name", defaultValue = "易水寒") String name)throws Exception{ List<Student> user = userRepository.findByName(name);
return user;
}
/**
* jpa排序分页
* @param page
* @param size
* @return
*/
@RequestMapping(value = "/finds", method=RequestMethod.GET)
public Page<Student> getEntryByParams(@RequestParam(value = "page", defaultValue = "0") Integer page,
@RequestParam(value = "size", defaultValue = "15") Integer size) { Pageable pageable = new PageRequest(page, size, Sort.Direction.DESC, "age");
return userRepository .findAll(pageable);
}
}

6.spring boot启动项:

package com.example.springmybatis;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class SpringDataJpaAPP { public static void main(String[] args) {
SpringApplication.run(SpringDataJpaAPP.class, args);
}
}

7.sql:

CREATE TABLE `student` (
`name` varchar(100) NOT NULL,
`age` int(10) NOT NULL,
`address` varchar(100) NOT NULL,
`collage` varchar(100) NOT NULL,
`sex` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8

最新文章

  1. T1加权像(T1 weighted image,T1WI)
  2. 关于 JSONP跨域示例
  3. PopupWindowAction breaking MEF import?
  4. 非对称加密算法--DH
  5. 我的Vim配置(自动补全/树形文件浏览)
  6. 怎么写jq插件?
  7. 连接Oracle远程数据库错误:ORA-12541,ORA-12514,ORA-01017的解决方法!
  8. ORACLE SELECT INTO NO_DATA_FOUND问题
  9. Linux ulimit 系统资源控制
  10. pymssql文档
  11. Python学习笔记 (1) :python简介、工具、编码及基础运算
  12. SQL查询练习一(From LeetCode)
  13. .net framework 4.5 +steeltoe+ springcloud 实现服务注册功能
  14. Mysql配置主从同步的基本步骤
  15. ThreadPoolExecutor代码解析
  16. 谷歌pixel手机解BL锁、刷机、破解电信(史上最详细的帖子)
  17. Python 正则表达式模块 (re) 简介
  18. Centos7安装出现的问题:找不到安装源或者检查软件配置出错
  19. ballerina 学习二十六 项目docker 部署&amp;&amp; 运行(二)
  20. java.lang.Object类

热门文章

  1. Activiti6系列(1)- 核心数据库表及字段注释说明
  2. vue面试题整理vuejs基础知识整理
  3. Spring入门(八):自动装配的歧义性
  4. 前端数据双向绑定原理:Object.defineProperty()
  5. web小知识点
  6. R 实用命令 1
  7. linux安装杀软 clamAV
  8. N个tomcat之间实现Session共享(写的不错,转来的)
  9. 基于 Vue+Mint-ui 的 Mobile-h5 的项目说明
  10. ECMAScript---布尔类型、null、undefined详解