springboot2.0 集合JPA

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.JPA</groupId>
	<artifactId>jpa</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>jpa</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.2.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-data-jpa</artifactId>
		</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-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>
	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>


</project>

application.properties

#datasource tomcat - jdbc
spring.datasource.url=jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=UTF-8
spring.datasource.username=root
spring.datasource.password=root

#JPA
# 如果有的话,只会更新表结构而不会建立
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true

实体类:

package com.jpa.bean;

import javax.persistence.*;

/**
 * Created by  lpw'ASUS on 2018/5/30.
 */

@Entity
public class UserInfo {

    private Integer id;
    private String name;
    private String sex;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }
    @Column
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
    @Column
    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "UserInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                '}';
    }
}

dao层:

package com.jpa.dao;

import com.jpa.bean.UserInfo;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**JpaRepository<对象,主键类型>
 * Created by  lpw'ASUS on 2018/5/31.
 */
public interface UserInfoDao extends JpaRepository<UserInfo,Integer> {
    // 名字优于配置  @Quert vlaue 写jpql  ,且@Query优先级大于名字
//    @Query(value = "select u from UserInfo u where u.name like ?1")
//     List<UserInfo> findByName(String name);
     List<UserInfo> findByNameAndSex(String name,String sex);
     // 模糊查询
//     List<UserInfo> findByNameLike(String name);
    //分页查询,加模糊查询,
     Page<UserInfo> findByNameLike(String name, Pageable pageable);
}

测试类:

package com.jpa;

import com.jpa.bean.UserInfo;
import com.jpa.dao.UserInfoDao;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

@RunWith(SpringRunner.class)
@SpringBootTest
public class JpaApplicationTests {

	@Autowired
	private UserInfoDao userInfoDao;
	@Test
	public void contextLoads() {
	}

	@Test
	public void insertTest(){
		UserInfo userInfo = new UserInfo();
		userInfo.setName("张三");
		userInfo.setSex("1");
		userInfoDao.save(userInfo);
	}

	@Test
	public void updateTest(){
		UserInfo userInfo = new UserInfo();
		userInfo.setName("李四");
		userInfo.setSex("0");
		userInfo.setId(1);
		userInfoDao.save(userInfo);
	}

	@Test
	public void deleteTest(){
		UserInfo userInfo = new UserInfo();
		userInfo.setId(1);
		userInfoDao.delete(userInfo);
	}

	@Test
	public void queryTest(){
//		UserInfo userInfo = new UserInfo();
//		userInfo.setName("张三");
//		userInfo.setSex("1");
//		List<UserInfo>  list= userInfoDao.findByName("%李%");
////		List<UserInfo>  list1= userInfoDao.findByNameAndSex(userInfo.getName(),userInfo.getSex());
//
//		for(UserInfo user:list){
//			System.out.println("=="+user.toString());
//		}

//		for(UserInfo user:list1){
//			System.out.println("~~~~~"+user.toString());
//		}
	}


	@Test
	public void queryTest2(){
//
//		List<UserInfo>  list= userInfoDao.findByNameLike("%李%");
//
//		for(UserInfo user:list){
//			System.out.println("=="+user.toString());
//		}
	}

	@Test
	public void queryTest3(){
		Pageable pageable = new PageRequest(0,2);//第一个数0,代表第1页,2代表每页显示2条数据
               
		Page<UserInfo> page = userInfoDao.findByNameLike("%张%",pageable);

		for(UserInfo user:page.getContent()){
			System.out.println("=="+user.toString());
		}
	}

}

猜你喜欢

转载自blog.csdn.net/m0_38044453/article/details/80534507