Skip to content

Add Docker MySQL setup and instructions for Issue #72 #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions JtProject/basedata.sql
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# SQL configs
--# SQL configs
SET SQL_MODE ='IGNORE_SPACE,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';

# create database and use it
--# create database and use it
CREATE DATABASE IF NOT EXISTS ecommjava;
USE ecommjava;

# create the category table
--# create the category table
CREATE TABLE IF NOT EXISTS CATEGORY(
category_id int unique key not null auto_increment primary key,
name varchar(255) null
);

# insert default categories
--# insert default categories
INSERT INTO CATEGORY(name) VALUES ('Fruits'),
('Vegetables'),
('Meat'),
Expand All @@ -22,7 +22,7 @@ INSERT INTO CATEGORY(name) VALUES ('Fruits'),
('Sweets'),
('Other');

# create the customer table
--# create the customer table
CREATE TABLE IF NOT EXISTS CUSTOMER(
id int unique key not null auto_increment primary key,
address varchar(255) null,
Expand All @@ -33,12 +33,12 @@ username varchar(255) null,
UNIQUE (username)
);

# insert default customers
--# insert default customers
INSERT INTO CUSTOMER(address, email, password, role, username) VALUES
('123, Albany Street', '[email protected]', '123', 'ROLE_ADMIN', 'admin'),
('765, 5th Avenue', '[email protected]', '765', 'ROLE_NORMAL', 'lisa');

# create the product table
--# create the product table
CREATE TABLE IF NOT EXISTS PRODUCT(
product_id int unique key not null auto_increment primary key,
description varchar(255) null,
Expand All @@ -51,13 +51,13 @@ category_id int null,
customer_id int null
);

# insert default products
--# insert default products
INSERT INTO PRODUCT(description, image, name, price, quantity, weight, category_id) VALUES
('Fresh and juicy', 'https://freepngimg.com/save/9557-apple-fruit-transparent/744x744', 'Apple', 3, 40, 76, 1),
('Woops! There goes the eggs...', 'https://www.nicepng.com/png/full/813-8132637_poiata-bunicii-cracked-egg.png', 'Cracked Eggs', 1, 90, 43, 9);


# create indexes
--# create indexes
CREATE INDEX FK7u438kvwr308xcwr4wbx36uiw
ON PRODUCT (category_id);

Expand Down
17 changes: 17 additions & 0 deletions JtProject/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
services:
mysql:
image: mysql:8.0
container_name: mysql-container
environment:
MYSQL_ROOT_PASSWORD: rootpass
MYSQL_DATABASE: jtproject
MYSQL_USER: appuser
MYSQL_PASSWORD: apppassword
ports:
- "3306:3306"
volumes:
- mysql_data:/var/lib/mysql

volumes:
mysql_data:

Empty file modified JtProject/mvnw
100644 → 100755
Empty file.
10 changes: 5 additions & 5 deletions JtProject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>


<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<version>8.0.32</version>
</dependency>



<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,44 @@
package com.jtspringproject.JtSpringProject;

import java.util.Properties;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.orm.hibernate5.HibernateTransactionManager;
import org.springframework.orm.hibernate5.LocalSessionFactoryBean;
import org.springframework.transaction.annotation.EnableTransactionManagement;

@Configuration
@EnableTransactionManagement
public class HibernateConfiguration {
@Value("${db.driver}")

// Updated property keys to match your application.properties configuration
@Value("${spring.datasource.driver-class-name}")
private String DRIVER;

@Value("${db.password}")
private String PASSWORD;

@Value("${db.url}")

@Value("${spring.datasource.url}")
private String URL;
@Value("${db.username}")

@Value("${spring.datasource.username}")
private String USERNAME;


@Value("${spring.datasource.password}")
private String PASSWORD;

@Value("${hibernate.dialect}")
private String DIALECT;

@Value("${hibernate.show_sql}")
private String SHOW_SQL;

@Value("${hibernate.hbm2ddl.auto}")
private String HBM2DDL_AUTO;

@Value("${entitymanager.packagesToScan}")
private String PACKAGES_TO_SCAN;

@Bean
public DataSource dataSource() {
DriverManagerDataSource dataSource = new DriverManagerDataSource();
Expand All @@ -48,7 +48,7 @@ public DataSource dataSource() {
dataSource.setPassword(PASSWORD);
return dataSource;
}

@Bean
public LocalSessionFactoryBean sessionFactory() {
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
Expand All @@ -59,14 +59,13 @@ public LocalSessionFactoryBean sessionFactory() {
hibernateProperties.put("hibernate.show_sql", SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", HBM2DDL_AUTO);
sessionFactory.setHibernateProperties(hibernateProperties);

return sessionFactory;
}

@Bean
public HibernateTransactionManager transactionManager() {
HibernateTransactionManager transactionManager = new HibernateTransactionManager();
transactionManager.setSessionFactory(sessionFactory().getObject());
return transactionManager;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.autoconfigure.domain.EntityScan;

@SpringBootApplication(exclude = HibernateJpaAutoConfiguration.class)
@EntityScan(basePackages = "com.jtspringproject.JtSpringProject.models")
public class JtSpringProjectApplication {

public static void main(String[] args) {
SpringApplication.run(JtSpringProjectApplication.class, args);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,48 @@
@Repository
public class userDao {
@Autowired
private SessionFactory sessionFactory;
private SessionFactory sessionFactory;

public void setSessionFactory(SessionFactory sf) {
this.sessionFactory = sf;
}
@Transactional
public List<User> getAllUser() {
Session session = this.sessionFactory.getCurrentSession();
this.sessionFactory = sf;
}
@Transactional
public List<User> getAllUser() {
Session session = this.sessionFactory.getCurrentSession();
List<User> userList = session.createQuery("from CUSTOMER").list();
return userList;
}
@Transactional
return userList;
}

@Transactional
public User saveUser(User user) {
this.sessionFactory.getCurrentSession().saveOrUpdate(user);
System.out.println("User added" + user.getId());
return user;
return user;
}
// public User checkLogin() {

// public User checkLogin() {
// this.sessionFactory.getCurrentSession().
// }
@Transactional
public User getUser(String username,String password) {
Query query = sessionFactory.getCurrentSession().createQuery("from CUSTOMER where username = :username");
query.setParameter("username",username);
try {
@Transactional
public User getUser(String username,String password) {
Query query = sessionFactory.getCurrentSession().createQuery("from CUSTOMER where username = :username");
query.setParameter("username",username);

try {
User user = (User) query.getSingleResult();
System.out.println(user.getPassword());
if(password.equals(user.getPassword())) {
return user;
}else {
}else {
return new User();
}
}catch(Exception e){
System.out.println(e.getMessage());
User user = new User();
return user;
}
}

}

@Transactional
public boolean userExists(String username) {
Expand All @@ -70,14 +70,14 @@ public boolean userExists(String username) {

@Transactional
public User getUserByUsername(String username) {
Query<User> query = sessionFactory.getCurrentSession().createQuery("from User where username = :username", User.class);
query.setParameter("username", username);
try {
return query.getSingleResult();
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
Query<User> query = sessionFactory.getCurrentSession().createQuery("from User where username = :username", User.class);
query.setParameter("username", username);

try {
return query.getSingleResult();
} catch (Exception e) {
System.out.println(e.getMessage());
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity(name="CUSTOMER")
@Table
@Entity
@Table(name = "CUSTOMER")
public class User {

@Id
Expand Down
74 changes: 43 additions & 31 deletions JtProject/src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,40 +1,52 @@
## Server configuration
#server.port=8080
#spring.mvc.view.prefix=/views/
#spring.mvc.view.suffix=.jsp
#
## PostgreSQL Database Configuration
#spring.datasource.url=jdbc:postgresql://localhost:5432/postgres
#spring.datasource.username=postgres
#spring.datasource.password=Sama@123
#spring.datasource.driver-class-name=org.postgresql.Driver
#
## Hibernate Configuration
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
#spring.jpa.show-sql=true
#spring.jpa.hibernate.ddl-auto=update
#
#hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
#hibernate.show_sql=true
#hibernate.hbm2ddl.auto=update
#entitymanager.packagesToScan=com
#
## Lazy loading fix
#spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true
#
#



# Server configuration
server.port=8080
spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp

#Database related properties
#database.driver=com.mysql.cj.jdbc.Driver
#database.url=jdbc:mysql://localhost:3306/ecomjava
#database.user=root
#database.password=
#
##Hibernate related properties
#hibernate.dialect=org.hibernate.dialect.MySQL5Dialect
#hibernate.show_sql=true
#hibernate.format_sql=true
#hibernate.hbm2ddl.auto=update
# MySQL Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/jtproject
spring.datasource.username=appuser
spring.datasource.password=apppassword
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver

# Hibernate Configuration
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update

# Hibernate
hibernate.dialect= org.hibernate.dialect.MySQL5Dialect
hibernate.show_sql= true
hibernate.hbm2ddl.auto= update
#entitymanager.packagesToScan:

spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
hibernate.show_sql=true
hibernate.hbm2ddl.auto=update
entitymanager.packagesToScan=com

db.driver= com.mysql.cj.jdbc.Driver
db.url= jdbc:mysql://localhost:3306/ecommjava?createDatabaseIfNotExist=true
db.username= root
db.password=
entitymanager.packagesToScan= com
# Lazy loading fix
spring.jpa.properties.hibernate.enable_lazy_load_no_trans=true

#spring.datasource.url=jdbc:mysql://localhost:3306/ecommjava?createDatabaseIfNotExist=true
#spring.datasource.username=root
#spring.datasource.password=
#spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
#spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL8Dialect
#spring.jpa.show-sql=true
#spring.jpa.hibernate.ddl-auto=update
Loading
Loading