Skip to content

Commit b17a256

Browse files
committed
Mysql connection
1 parent a329a10 commit b17a256

File tree

17 files changed

+117
-90
lines changed

17 files changed

+117
-90
lines changed

pom.xml

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,23 @@
1515
<name>CrudEmpleats</name>
1616
<description>Crud de Empleats</description>
1717
<properties>
18-
<java.version>15</java.version>
18+
<java.version>11</java.version>
1919
</properties>
2020
<dependencies>
2121
<dependency>
2222
<groupId>org.springframework.boot</groupId>
2323
<artifactId>spring-boot-starter-data-jpa</artifactId>
2424
</dependency>
25+
<dependency>
26+
<groupId>com.h2database</groupId>
27+
<artifactId>h2</artifactId>
28+
<scope>runtime</scope>
29+
</dependency>
30+
<dependency>
31+
<groupId>mysql</groupId>
32+
<artifactId>mysql-connector-java</artifactId>
33+
<scope>runtime</scope>
34+
</dependency>
2535
<dependency>
2636
<groupId>org.springframework.boot</groupId>
2737
<artifactId>spring-boot-starter-validation</artifactId>

src/.DS_Store

6 KB
Binary file not shown.

src/main/java/com/itacademy/CrudEmpleats/CrudEmpleatsApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
5-
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
65

7-
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class})
6+
7+
@SpringBootApplication
88
public class CrudEmpleatsApplication {
99

1010
public static void main(String[] args) {

src/main/java/com/itacademy/CrudEmpleats/controller/ControllerEmployees.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public Employee getFirstEmployee(@PathVariable("id") int id) {
4545
@PutMapping("/EmployeeList/{id}")
4646
@ResponseStatus(HttpStatus.ACCEPTED) // 202
4747
public Employee updateEmployee(@Valid @RequestBody Employee employee, @PathVariable("id") int id) {
48-
repositori.updateEmployee(employee,id);
48+
repositori.updateEmployee(employee);
4949
return repositori.getEmployeeById(id);
5050
}
5151

src/main/java/com/itacademy/CrudEmpleats/domain/Employee.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,37 @@
11
package com.itacademy.CrudEmpleats.domain;
22

3+
import javax.persistence.Column;
34
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
47
import javax.persistence.Id;
8+
import javax.persistence.Table;
59
import javax.validation.constraints.NotBlank;
610
import javax.validation.constraints.NotNull;
711

812
@Entity
13+
@Table(name="employee")
914
public class Employee {
1015

1116
@Id
17+
@GeneratedValue(strategy = GenerationType.IDENTITY)
18+
@Column(name="id")
1219
@NotNull
1320
private int id;
21+
1422
@NotBlank
23+
@Column(name="name")
1524
private String name;
25+
1626
@NotBlank
27+
@Column(name="job")
1728
private String job;
29+
@Column(name="salary")
1830
private double salary ;
1931

32+
public Employee() {
33+
}
34+
2035
//salari i cárreg en funció de la calsse Jobs Enum.
2136
public Employee(int id,String name, String job) {
2237
this.id=id;

src/main/java/com/itacademy/CrudEmpleats/persistence/EmployeeRepository.java

Lines changed: 0 additions & 62 deletions
This file was deleted.
Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,9 @@
11
package com.itacademy.CrudEmpleats.persistence;
22

3-
import java.util.List;
4-
3+
import org.springframework.data.jpa.repository.JpaRepository;
4+
import org.springframework.stereotype.Repository;
55
import com.itacademy.CrudEmpleats.domain.Employee;
66

7-
8-
public interface IEmployeeRepository {
9-
10-
public List<Employee> getAllEmployees();
11-
12-
public Employee getEmployeeById(int id);
13-
14-
public void addEmployee(Employee employee);
15-
16-
public void updateEmployee(Employee employee, int id);
17-
18-
public void deleteEmployee(int id);
19-
7+
@Repository
8+
public interface IEmployeeRepository extends JpaRepository<Employee, Integer> {
209
}

src/main/java/com/itacademy/CrudEmpleats/service/EmployeeServices.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,33 @@
66
import org.springframework.stereotype.Service;
77

88
import com.itacademy.CrudEmpleats.domain.Employee;
9-
import com.itacademy.CrudEmpleats.persistence.EmployeeRepository;
9+
import com.itacademy.CrudEmpleats.persistence.IEmployeeRepository;
1010

1111
@Service
1212
public class EmployeeServices {
1313

1414
@Autowired
15-
private EmployeeRepository repositori;
15+
private IEmployeeRepository repositori;
1616

1717
public List<Employee> getAllEmployees() {
18-
return repositori.getAllEmployees();
18+
return repositori.findAll();
1919
}
2020

2121
public Employee getEmployeeById(int id){
22-
return repositori.getEmployeeById(id);
22+
return repositori.findById(id).orElse(null);
2323
}
2424

2525
public void addEmployee(Employee employee) {
26-
repositori.addEmployee(employee);
26+
repositori.save(employee);
2727
}
2828

29-
public void updateEmployee(Employee employee, int id) {
30-
repositori.updateEmployee(employee, id);
29+
public void updateEmployee(Employee employee) {
30+
repositori.save(employee);
3131
}
3232

3333
public void deleteEmployee(int id) {
34-
repositori.deleteEmployee(id);
34+
repositori.deleteById(id);
3535
}
3636

37+
3738
}

src/main/resources/.DS_Store

6 KB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
##-- Mysql H2 en memoria --##
2+
3+
spring.datasource.platform=h2
4+
5+
spring.datasource.url = jdbc:h2:mem:testdb
6+
spring.datasource.driverClassName=org.h2.Driver
7+
spring.datasource.username=sa
8+
spring.datasource.password=password
9+
10+
spring.jpa.hibernate.ddl-auto = update
11+
12+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.H2Dialect
13+
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
##-- Mysql BD persistent --##
2+
3+
spring.datasource.url = jdbc:mysql://localhost:3306/m13_employee
4+
spring.datasource.username = root
5+
spring.datasource.password =
6+
7+
spring.datasource.platform=mysql
8+
9+
spring.jpa.show-sql = true
10+
11+
spring.jpa.hibernate.ddl-auto = update
12+
13+
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
14+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#-- Perfil "mysql" per treballar amb una Base de Dades persistent i "H2" per treballar amb una Base de Dades en memoria
2+
3+
spring.profiles.active=mysql
4+
5+
# (Test) Introdueix 11 entitats de prova a la base de dades
6+
# Si s'eliminen tornen a crear-se al recarregar el projecte
7+
spring.datasource.initialization-mode=always

src/main/resources/data-h2.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT INTO employee (id,name,job,Salary) VALUES
2+
(1,'Joan Gordi','Director de projectes',50000.0),
3+
(2,'Gerard Puig','Programador Mid-Level',29000.0),
4+
(3,'Pere Masnou','Administratiu',25000.0),
5+
(4,'Laura Klaus','Programador Senior',35000.0),
6+
(5,'Anaïs Iglesias','Programador Mid-Level',29000.0),
7+
(6,'Jhon Smith','Administratiu',25000.0),
8+
(7,'Ramón Dalmau','Programador Mid-Level',29000.0),
9+
(8,'Joana Gracia','Programador Junior',21000.0),
10+
(9,'Quique Mota','Administratiu',25000.0),
11+
(10,'Kevin Lohan','Director de projectes',50000.0),
12+
(11,'Maria Camps','Administratiu',25000.0);

src/main/resources/data-mysql.sql

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
INSERT IGNORE INTO employee (id,name,job,Salary) VALUES
2+
(1,'Joan Gordi','Director de projectes',50000.0),
3+
(2,'Gerard Puig','Programador Mid-Level',29000.0),
4+
(3,'Pere Masnou','Administratiu',25000.0),
5+
(4,'Laura Klaus','Programador Senior',35000.0),
6+
(5,'Anaïs Iglesias','Programador Mid-Level',29000.0),
7+
(6,'Jhon Smith','Administratiu',25000.0),
8+
(7,'Ramón Dalmau','Programador Mid-Level',29000.0),
9+
(8,'Joana Gracia','Programador Junior',21000.0),
10+
(9,'Quique Mota','Administratiu',25000.0),
11+
(10,'Kevin Lohan','Director de projectes',50000.0),
12+
(11,'Maria Camps','Administratiu',25000.0);

src/main/resources/static/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ function getJobCode(job) {
139139
return 'Director_Projectes';
140140
} if (job == 'Programador Senior') {
141141
return 'Programador_Senior';
142-
} if (job == 'Programador_Mid') {
142+
} if (job == 'Programador Mid-Level') {
143143
return 'Programador_Mid';
144144
} if (job == 'Programador Junior') {
145145
return 'Programador_Junior';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"id":19,"name":"Maria Camps","job":"Administratiu","salary":25000.0},{"id":17,"name":"Joan Gordi","job":"Director de projectes","salary":50000.0},{"id":18,"name":"Gerard Puig","job":"Programador Mid-Level","salary":29000.0},{"id":21,"name":"Pere Masnou","job":"Administratiu","salary":25000.0},{"id":22,"name":"Laura Klaus","job":"Programador Senior","salary":35000.0},{"id":20,"name":"Anaïs Iglesias","job":"Programador Mid-Level","salary":29000.0},{"id":23,"name":"Jhon Smith","job":"Administratiu","salary":25000.0},{"id":24,"name":"Ramón Dalmau","job":"Programador Mid-Level","salary":29000.0},{"id":25,"name":"Joana Gracia","job":"Programador Junior","salary":21000.0},{"id":26,"name":"Quique Mota","job":"Administratiu","salary":25000.0},{"id":27,"name":"Kevin Lohan","job":"Director de projectes","salary":50000.0}]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
spring.datasource.url=jdbc:h2:mem:testdb
2+
spring.datasource.driverClassName=org.h2.Driver
3+
spring.datasource.username=sa
4+
spring.datasource.password=password
5+
6+
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
7+
8+
spring.jpa.hibernate.ddl-auto=create
9+
10+
#[TEST] This comand will populate your database with the registers in data.sql file.
11+
#If the registers are deleted they will be recreated when reload this app.
12+
spring.datasource.initialization-mode=always
13+
14+

0 commit comments

Comments
 (0)