mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2021-05
This commit is contained in:
-1
@@ -21,7 +21,6 @@ public class PersonDaoJdbc implements PersonDao {
|
||||
|
||||
public PersonDaoJdbc(NamedParameterJdbcOperations namedParameterJdbcOperations)
|
||||
{
|
||||
// Это просто оставили, чтобы не переписывать код
|
||||
// В идеале всё должно быть на NamedParameterJdbcOperations
|
||||
this.jdbc = namedParameterJdbcOperations.getJdbcOperations();
|
||||
this.namedParameterJdbcOperations = namedParameterJdbcOperations;
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
|
||||
|
||||
node/
|
||||
/node_modules
|
||||
/output
|
||||
package-lock.json
|
||||
@@ -0,0 +1,26 @@
|
||||
{
|
||||
"name": "client",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
"dev": "webpack-dev-server --config webpack.dev.config.js",
|
||||
"build": "webpack"
|
||||
},
|
||||
"author": "",
|
||||
"license": "ISC",
|
||||
"dependencies": {
|
||||
"react": "^16.2.0",
|
||||
"react-dom": "^16.2.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"babel-core": "^6.26.0",
|
||||
"babel-loader": "^7.1.2",
|
||||
"babel-preset-env": "^1.6.1",
|
||||
"babel-preset-react": "^6.24.1",
|
||||
"html-webpack-plugin": "^3.2.0",
|
||||
"webpack": "^3.10.0",
|
||||
"webpack-cli": "^3.2.3",
|
||||
"webpack-dev-server": "^2.9.7"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-17-boot-and-react</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.4.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.github.eirslett</groupId>
|
||||
<artifactId>frontend-maven-plugin</artifactId>
|
||||
<version>1.6</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>install node and npm</id>
|
||||
<goals>
|
||||
<goal>install-node-and-npm</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<nodeVersion>v10.15.1</nodeVersion>
|
||||
<npmVersion>6.4.1</npmVersion>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm install</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<phase>generate-resources</phase>
|
||||
<configuration>
|
||||
<arguments>install</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>npm run build</id>
|
||||
<goals>
|
||||
<goal>npm</goal>
|
||||
</goals>
|
||||
<phase>generate-resources</phase>
|
||||
<configuration>
|
||||
<arguments>run build</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
repository.save(new Person("Pushkin"));
|
||||
repository.save(new Person("Lermontov"));
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
List<Person> findAll();
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
import ru.otus.spring.rest.dto.PersonDto;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
public class PersonController {
|
||||
|
||||
private final PersonRepository repository;
|
||||
|
||||
@Autowired
|
||||
public PersonController(PersonRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@GetMapping("/api/persons")
|
||||
public List<PersonDto> getAllPersons() {
|
||||
return repository.findAll().stream().map(PersonDto::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2016 Russian Post
|
||||
*
|
||||
* This source code is Russian Post Confidential Proprietary.
|
||||
* This software is protected by copyright. All rights and titles are reserved.
|
||||
* You shall not use, copy, distribute, modify, decompile, disassemble or reverse engineer the software.
|
||||
* Otherwise this violation would be treated by law and would be subject to legal prosecution.
|
||||
* Legal use of the software provides receipt of a license from the right name only.
|
||||
*/
|
||||
package ru.otus.spring.rest.dto;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
/**
|
||||
* DTO that represents Account
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class PersonDto {
|
||||
|
||||
private int id = -1;
|
||||
private String name;
|
||||
|
||||
public PersonDto() {
|
||||
}
|
||||
|
||||
public PersonDto(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static PersonDto toDto(Person person) {
|
||||
return new PersonDto(person.getId(), person.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
import React from 'react'
|
||||
|
||||
const Header = (props) => (
|
||||
<h1>{props.title}</h1>
|
||||
);
|
||||
|
||||
export default class App extends React.Component {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
this.state = {persons: []};
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
fetch('/api/persons')
|
||||
.then(response => response.json())
|
||||
.then(persons => this.setState({persons}));
|
||||
}
|
||||
|
||||
render() {
|
||||
return (
|
||||
<React.Fragment>
|
||||
<Header title={'Persons'}/>
|
||||
<table>
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{
|
||||
this.state.persons.map((person, i) => (
|
||||
<tr key={i}>
|
||||
<td>{person.id}</td>
|
||||
<td>{person.name}</td>
|
||||
</tr>
|
||||
))
|
||||
}
|
||||
</tbody>
|
||||
</table>
|
||||
</React.Fragment>
|
||||
)
|
||||
}
|
||||
};
|
||||
@@ -0,0 +1,15 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<title>Minimal React Boilerplate</title>
|
||||
</head>
|
||||
<body>
|
||||
<noscript>
|
||||
You need to enable JavaScript to run this app.
|
||||
</noscript>
|
||||
|
||||
<div id="root"></div>
|
||||
|
||||
<script src="./bundle.js" type="text/javascript"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,9 @@
|
||||
import React from 'react'
|
||||
import ReactDOM from 'react-dom'
|
||||
|
||||
import App from './components/App'
|
||||
|
||||
ReactDOM.render(
|
||||
<App />,
|
||||
document.getElementById('root')
|
||||
)
|
||||
@@ -0,0 +1,47 @@
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin')
|
||||
const path = require('path');
|
||||
const webpack = require('webpack');
|
||||
|
||||
module.exports = {
|
||||
entry: './src/ui/index.js',
|
||||
output: {
|
||||
path: path.resolve(__dirname, 'target/classes/public/'),
|
||||
filename: 'bundle.min.js',
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
|
||||
module: {
|
||||
loaders: [
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: /(node_modules|bower_components|build)/,
|
||||
use: {
|
||||
loader: 'babel-loader',
|
||||
options: {
|
||||
presets: ['env', 'react']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
|
||||
plugins: [
|
||||
new webpack.DefinePlugin({
|
||||
"process.env": {
|
||||
NODE_ENV: JSON.stringify("production")
|
||||
}
|
||||
}),
|
||||
new webpack.optimize.UglifyJsPlugin({
|
||||
compress: {
|
||||
warnings: false,
|
||||
},
|
||||
output: {
|
||||
comments: false,
|
||||
},
|
||||
}),
|
||||
new HtmlWebpackPlugin({
|
||||
filename: 'index.html',
|
||||
template: 'src/ui/index.html'
|
||||
})
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
const HtmlWebpackPlugin = require('html-webpack-plugin')
|
||||
const path = require('path');
|
||||
|
||||
module.exports = {
|
||||
entry: './src/ui/index.js',
|
||||
devtool: 'inline-source-map',
|
||||
output: {
|
||||
path: path.resolve(__dirname),
|
||||
filename: 'bundle.js',
|
||||
libraryTarget: 'umd'
|
||||
},
|
||||
|
||||
devServer: {
|
||||
contentBase: path.resolve(__dirname) + '/src/ui',
|
||||
compress: true,
|
||||
port: 9000,
|
||||
host: 'localhost',
|
||||
open: true,
|
||||
before: (app) => {
|
||||
app.get('/api/persons', (req, res) => res.send([
|
||||
{id: '1', name: 'Привяу'}
|
||||
]));
|
||||
}
|
||||
},
|
||||
|
||||
module: {
|
||||
loaders: [
|
||||
{
|
||||
test: /\.js$/,
|
||||
exclude: /(node_modules|bower_components|build)/,
|
||||
use: {
|
||||
loader: 'babel-loader',
|
||||
options: {
|
||||
presets: ['env', 'react']
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
},
|
||||
plugins: [
|
||||
new HtmlWebpackPlugin({
|
||||
filename: 'index.html',
|
||||
template: 'src/ui/index.html'
|
||||
})
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
@@ -0,0 +1,60 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-17-jquery</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.4.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.webjars</groupId>
|
||||
<artifactId>jquery</artifactId>
|
||||
<version>3.3.1</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</project>
|
||||
@@ -0,0 +1,27 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Main.class);
|
||||
}
|
||||
|
||||
@SuppressWarnings("SpringJavaAutowiredFieldsWarningInspection")
|
||||
@Autowired
|
||||
private PersonRepository repository;
|
||||
|
||||
@PostConstruct
|
||||
public void init() {
|
||||
repository.save(new Person("Pushkin"));
|
||||
repository.save(new Person("Lermontov"));
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Person {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private int id;
|
||||
private String name;
|
||||
|
||||
public Person() {
|
||||
}
|
||||
|
||||
public Person(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package ru.otus.spring.page;
|
||||
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
|
||||
@Controller
|
||||
public class PersonPagesController {
|
||||
|
||||
@GetMapping("/")
|
||||
public String listPage(Model model) {
|
||||
model.addAttribute("keywords", "list users in Omsk, omsk, list users, list users free");
|
||||
return "list";
|
||||
}
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
package ru.otus.spring.repostory;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonRepository extends CrudRepository<Person, Integer> {
|
||||
|
||||
List<Person> findAll();
|
||||
}
|
||||
+30
@@ -0,0 +1,30 @@
|
||||
package ru.otus.spring.rest;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.repostory.PersonRepository;
|
||||
import ru.otus.spring.rest.dto.PersonDto;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@RestController
|
||||
public class PersonController {
|
||||
|
||||
private final PersonRepository repository;
|
||||
|
||||
@Autowired
|
||||
public PersonController(PersonRepository repository) {
|
||||
this.repository = repository;
|
||||
}
|
||||
|
||||
@GetMapping("/api/persons")
|
||||
public List<PersonDto> getAllPersons() {
|
||||
return repository.findAll().stream().map(PersonDto::toDto)
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
+50
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright 2016 Russian Post
|
||||
*
|
||||
* This source code is Russian Post Confidential Proprietary.
|
||||
* This software is protected by copyright. All rights and titles are reserved.
|
||||
* You shall not use, copy, distribute, modify, decompile, disassemble or reverse engineer the software.
|
||||
* Otherwise this violation would be treated by law and would be subject to legal prosecution.
|
||||
* Legal use of the software provides receipt of a license from the right name only.
|
||||
*/
|
||||
package ru.otus.spring.rest.dto;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
/**
|
||||
* DTO that represents Account
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
public class PersonDto {
|
||||
|
||||
private int id = -1;
|
||||
private String name;
|
||||
|
||||
public PersonDto() {
|
||||
}
|
||||
|
||||
public PersonDto(int id, String name) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(int id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public static PersonDto toDto(Person person) {
|
||||
return new PersonDto(person.getId(), person.getName());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en" xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8"/>
|
||||
<meta name="keywords" th:content="${keywords}"/>
|
||||
<title>List of all persons</title>
|
||||
<style type="text/css">
|
||||
body {
|
||||
padding: 50px;
|
||||
}
|
||||
|
||||
.persons, .persons td {
|
||||
border: 1px solid lightgray;
|
||||
padding: 5px;
|
||||
}
|
||||
</style>
|
||||
<script src="/webjars/jquery/3.3.1/jquery.min.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Persons:</h1>
|
||||
|
||||
<table class="persons">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>ID</th>
|
||||
<th>Name</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
</tbody>
|
||||
</table>
|
||||
<script>
|
||||
$(function () {
|
||||
$.get('/api/persons').done(function (persons) {
|
||||
persons.forEach(function (person) {
|
||||
$('tbody').append(`
|
||||
<tr>
|
||||
<td>${person.id}</td>
|
||||
<td>${person.name}</td>
|
||||
</tr>
|
||||
`)
|
||||
});
|
||||
})
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
@@ -0,0 +1,31 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<!-- Здесь находится много настроек, в частности maven-surefire-plugin -->
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.4.6</version>
|
||||
<relativePath/>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- В этой зависимости и JUnit 5 (без JUnit 4), Mockito, AssertJ -->
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,16 @@
|
||||
package ru.otus.junit.dao;
|
||||
|
||||
import ru.otus.junit.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person getByName(String name) throws PersonNotFoundException;
|
||||
|
||||
List<Person> getAll();
|
||||
|
||||
void deleteByName(String name) throws PersonNotFoundException;
|
||||
|
||||
void save(Person person);
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
package ru.otus.junit.dao;
|
||||
|
||||
import ru.otus.junit.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PersonDaoImpl implements PersonDao {
|
||||
|
||||
// здесь будет поле - список Person-ов
|
||||
|
||||
@Override
|
||||
public Person getByName(String name) throws PersonNotFoundException {
|
||||
// TODO: реализовать поиск в списке по имени (по технике Test-First)
|
||||
// TODO: да, этот метод может бросать Exception, и это нужно покрыть другим тестом
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> getAll() {
|
||||
// TODO: реализовать получние всех Person (по технике Test-First)
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteByName(String name) throws PersonNotFoundException {
|
||||
// TODO: реализовать удаление по имени (по технике Test-First)
|
||||
// TODO: да, этот метод может бросать Exception, и это нужно покрыть другим тестом
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Person person) {
|
||||
// TODO: этот метод должен найти по имени в списке
|
||||
// TODO: если такой есть - заменить
|
||||
// TODO: если такого нет - добавить
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.junit.dao;
|
||||
|
||||
public class PersonNotFoundException extends RuntimeException {
|
||||
|
||||
public PersonNotFoundException(String message) {
|
||||
super(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package ru.otus.junit.domain;
|
||||
|
||||
@SuppressWarnings({"WeakerAccess", "unused"})
|
||||
public class Person {
|
||||
|
||||
private int age;
|
||||
private String name;
|
||||
|
||||
public Person(int age, String name) {
|
||||
this.age = age;
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public void setAge(int age) {
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void birthDay() {
|
||||
this.age++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package ru.otus.junit.service;
|
||||
|
||||
import ru.otus.junit.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
|
||||
List<Person> getAll();
|
||||
|
||||
boolean existsWithName(String name);
|
||||
|
||||
void save(Person p);
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package ru.otus.junit.service;
|
||||
|
||||
import ru.otus.junit.dao.PersonDao;
|
||||
import ru.otus.junit.domain.Person;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private final PersonDao dao;
|
||||
|
||||
PersonServiceImpl(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Person getByName(String name) {
|
||||
return dao.getByName(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<Person> getAll() {
|
||||
// TODO: реализовать данный метод по технике Test-First
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean existsWithName(String name) {
|
||||
// TODO: реализовать данный метод по технике Test-First
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save(Person p) {
|
||||
// TODO: реализовать данный метод по технике Test-First
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package ru.otus.junit.domain;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
@DisplayName("Класс Person")
|
||||
class PersonTest {
|
||||
|
||||
@DisplayName("корректно создаётся конструктором")
|
||||
@Test
|
||||
void shouldHaveCorrectConstructor() {
|
||||
Person person = new Person(42, "Ivan");
|
||||
|
||||
assertEquals("Ivan", person.getName());
|
||||
assertEquals(42, person.getAge());
|
||||
}
|
||||
|
||||
// TODO: @DisplayName("должен")
|
||||
|
||||
// TODO: @DisplayName("должен увеличивать возраст при вызове birthDay")
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package ru.otus.junit.service;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.InjectMocks;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import ru.otus.junit.dao.PersonDao;
|
||||
import ru.otus.junit.domain.Person;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class PersonServiceImplTest {
|
||||
|
||||
@Mock
|
||||
private PersonDao personDao;
|
||||
@InjectMocks
|
||||
private PersonServiceImpl personService;
|
||||
|
||||
@Test
|
||||
void getByName() {
|
||||
// TODO: используйте eq() mapper вместо any()
|
||||
given(personDao.getByName(any()))
|
||||
.willReturn(new Person(10, "Ivan"));
|
||||
|
||||
assertThat(personService.getByName("Ivan"))
|
||||
.isNotNull(); // TODO: сравните с помощью equals
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/build/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
@@ -0,0 +1,17 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-01</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<packaging>pom</packaging>
|
||||
|
||||
<modules>
|
||||
<module>spring-01-exercise</module>
|
||||
<module>spring-01-solution</module>
|
||||
</modules>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-01-exercise</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- TODO: Добавьте зависимость spring-context -->
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
//import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
// TODO: создайте здесь класс контекста
|
||||
|
||||
// TODO: Получите Person Service
|
||||
|
||||
// Получите Person "Ivan"
|
||||
Person ivan = null;
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private final PersonDao dao;
|
||||
|
||||
public PersonServiceImpl(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public Person getByName(String name) {
|
||||
return dao.findByName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<!-- TODO: определите бин класса ru.otus.spring.dao.PersonDaoSimple -->
|
||||
|
||||
<!-- TODO: определите бин класса ru.otus.spring.PersonServiceImpl -->
|
||||
|
||||
</beans>
|
||||
@@ -0,0 +1,26 @@
|
||||
<?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>ru.otus</groupId>
|
||||
<artifactId>spring-01-solution</artifactId>
|
||||
<version>1.0</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<!-- Это зависимость spring-context,
|
||||
содержит базовую функциональность IoC -->
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-context</artifactId>
|
||||
<version>5.3.7</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring;
|
||||
|
||||
import org.springframework.context.support.ClassPathXmlApplicationContext;
|
||||
import ru.otus.spring.domain.Person;
|
||||
import ru.otus.spring.service.PersonService;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/spring-context.xml");
|
||||
PersonService service = context.getBean(PersonService.class);
|
||||
Person ivan = service.getByName("Ivan");
|
||||
System.out.println("name: " + ivan.getName() + " age: " + ivan.getAge());
|
||||
|
||||
// Данная операция, в принципе не нужна.
|
||||
// Мы не работаем пока что с БД, а Spring Boot сделает закрытие за нас
|
||||
// Подробности - через пару занятий
|
||||
context.close();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonDao {
|
||||
|
||||
Person findByName(String name);
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
package ru.otus.spring.dao;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonDaoSimple implements PersonDao {
|
||||
|
||||
public Person findByName(String name) {
|
||||
return new Person(name, 18);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package ru.otus.spring.domain;
|
||||
|
||||
public class Person {
|
||||
|
||||
private final String name;
|
||||
private final int age;
|
||||
|
||||
public Person(String name, int age) {
|
||||
this.name = name;
|
||||
this.age = age;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public interface PersonService {
|
||||
|
||||
Person getByName(String name);
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package ru.otus.spring.service;
|
||||
|
||||
import ru.otus.spring.dao.PersonDao;
|
||||
import ru.otus.spring.domain.Person;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
|
||||
private final PersonDao dao;
|
||||
|
||||
public PersonServiceImpl(PersonDao dao) {
|
||||
this.dao = dao;
|
||||
}
|
||||
|
||||
public Person getByName(String name) {
|
||||
return dao.findByName(name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xmlns="http://www.springframework.org/schema/beans"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans
|
||||
http://www.springframework.org/schema/beans/spring-beans.xsd">
|
||||
|
||||
<bean id="personDao" class="ru.otus.spring.dao.PersonDaoSimple">
|
||||
</bean>
|
||||
|
||||
<bean id="personService" class="ru.otus.spring.service.PersonServiceImpl">
|
||||
<constructor-arg name="dao" ref="personDao"/>
|
||||
</bean>
|
||||
|
||||
</beans>
|
||||
Reference in New Issue
Block a user