springboot整合jpa小栗子

it2024-11-03  7

1:POM

<?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 https://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.2.10.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>dongshi.daddy</groupId> <artifactId>springboot-jpa-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-jpa-demo</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>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <!--<scope>test</scope>--> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>

2:dao

public interface UserDao extends JpaRepository<User, Long> { User findByUsername(String username); }

3:service

@Service public class UserService { @Autowired private UserDao userRepository; public User findUserByName(String usernarne) { return userRepository.findByUsername(usernarne); } }

4:controller

@RequestMapping("/user") @RestController public class UserController { @Autowired UserService userService; @GetMapping("/{username}") @ResponseBody public User getUser(@PathVariable("username") String username) { System.out.println("username is: " + username); return userService.findUserByName(username); } }

5:表和测试数据

CREATE TABLE `user` ( `id` bigint(20) NOT NULL AUTO_INCREMENT, `password` varchar(255) DEFAULT NULL, `username` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE KEY `UK_sb8bbouer5wak8vyiiy4pf2bx` (`username`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1; insert into user(password, username) values("111", "dongshidaddy");

6:测试类

@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT) public class UserControllerTest { @LocalServerPort private int port; private URL base; @Autowired private TestRestTemplate template; @Before public void setup() throws Exception { this.base = new URL( "http://localhost:" + port + "/user/dongshidaddy" ); } @Test public void getHello() throws Exception { ResponseEntity<String> response = template.getForEntity(base.toString(), String.class); assertEquals( "Expect <{\"id\":1,\"username\":\"dongshidaddy\",\"password\":\"111\"}>", "{\"id\":1,\"username\":\"dongshidaddy\",\"password\":\"111\"}", response.getBody() ); } }

7:配置文件

spring: datasource: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8 username: root password: 12345678 jpa: hibernate: ddl-auto: update #第一次简表 create 后面用 update show-sql: true

8:浏览器访问

9:源码

这里。

最后:都让开,我要喝瑞幸

最新回复(0)