hibernate

This commit is contained in:
lukasgol committed 2014-04-21 18:08:49 +02:00
1 parent c6f7e13b5d
commit 24c7a287c6
16 files changed
+281 -75

No files matched your search

+26
View File
@@ -0,0 +1,26 @@
package application;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;
import application.repositories.RoleRepository;
/**
* Created by lukasgol on 01.04.14.
*/
@Configuration
@ComponentScan
@EnableAutoConfiguration
@ImportResource("Spring-Module.xml")
public class Config {
public static void main(String[] args) {
ConfigurableApplicationContext context = SpringApplication.run(Config.class, args);
RoleRepository rr = context.getBean(RoleRepository.class);
System.out.print(rr);
}
}
+22
View File
@@ -0,0 +1,22 @@
package application.jmx;
/**
* Created by lukasgol on 05.04.14.
*/
public class First implements FirstMBean {
int val;
@Override
public int getValue() {
return val;
}
@Override
public String changeVal(int val) {
this.val=val;
return "changed";
}
@Override
public void setValue(int val) {
this.val=val;
}
}
@@ -0,0 +1,10 @@
package application.jmx;
/**
* Created by lukasgol on 05.04.14.
*/
public interface FirstMBean {
public int getValue();
public void setValue(int val);
public String changeVal(int val);
}
+40
View File
@@ -0,0 +1,40 @@
package application.model;
import javax.persistence.*;
@Entity(name = "role")
public class Role {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic
private String role;
@Basic
private String password;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
+62
View File
@@ -0,0 +1,62 @@
package application.model;
import javax.persistence.*;
@Entity(name = "account")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Basic
private String firstName;
@Basic
private String lastName;
@Basic
private String email;
@Basic
private String role;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String name) {
this.firstName = name;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getRole() {
return role;
}
public void setRole(String role) {
this.role = role;
}
}
@@ -0,0 +1,14 @@
package application.model;
/**
* Created by Marcin on 2014-04-19.
*/
public class UserLevel {
public java.util.List<String> getItems() {
java.util.List<String> list = new java.util.ArrayList<String>();
list.add("Head of security");
list.add("CEO");
list.add("Cleaner");
return list;
}
}
@@ -0,0 +1,7 @@
package application.repositories;
import application.model.Role;
import org.springframework.data.repository.CrudRepository;
public interface RoleRepository extends CrudRepository<Role, Long> {
}
@@ -0,0 +1,7 @@
package application.repositories;
import application.model.User;
import org.springframework.data.repository.CrudRepository;
public interface UserRepository extends CrudRepository<User, Long> {
}
@@ -0,0 +1,32 @@
package application.utils;
/**
* Created by lukasgol on 02.04.14.
*/
public class Action {
String name;
String param;
public Action(String name, String param) {
this.name = name;
this.param = param;
}
public void setName(String name) {
this.name = name;
}
public void setParam(String param) {
this.param = param;
}
public String getName() {
return name;
}
public String getParam() {
return param;
}
}
@@ -0,0 +1,105 @@
package application.utils; /**
* Created by lukasgol on 01.04.14.
*/
import application.model.User;
import application.repositories.RoleRepository;
import application.repositories.UserRepository;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
@Controller
@PropertySource("app.properties")
public class Controller_test {
//application.repositories
@Autowired
private UserRepository userRepository;
@Autowired
private RoleRepository roleRepository;
//end
@SuppressWarnings("SpringJavaAutowiringInspection")
@Autowired
PropertiesSolver ps;
@Value("${my.prop}")
String val;
//get log4j handler
private static final Logger logger = Logger.getLogger(Controller_test.class);
@RequestMapping("/action")
public
@ResponseBody
String hello(@RequestParam(value = "name", required = false, defaultValue = "empty") String name,
@RequestParam(value = "param", required = false, defaultValue = "empty") String param) {
Action action=new Action(name,param);
return "application.utils.Action: " + action.getName() + " with parameter: "+action.getParam()+". :-) ";
}
@RequestMapping("/str")
@ResponseBody
String hello(@RequestParam(value = "action", required = false, defaultValue = "empty") String action) {
return "Just String: "+action;
}
@RequestMapping("/val")
@ResponseBody
String getVal(@RequestParam(value = "param", required = false, defaultValue = "my.prop") String param){
System.out.println("lala");
if(logger.isDebugEnabled()){
logger.debug("Property:"+param);
}
String recived_val;
if (param.equals("my.prop"))return "property "+param+" is: "+ val ;
else if((recived_val=ps.getProperty(param))!=null){
return "property "+param+" is: "+ recived_val;
}
else {
logger.debug("Value:"+recived_val);
return "not found";
}
}
@RequestMapping(value = "/users", method = RequestMethod.GET)
public
@ResponseBody
String listUsersJson(ModelMap model) throws JSONException {
JSONArray userArray = new JSONArray();
for (User user : userRepository.findAll()) {
JSONObject userJSON = new JSONObject();
userJSON.put("id", user.getId());
userJSON.put("firstName", user.getFirstName());
userJSON.put("lastName", user.getLastName());
userJSON.put("email", user.getEmail());
userJSON.put("role", user.getRole());
userArray.put(userJSON);
}
return userArray.toString();
}
@RequestMapping(value = "/adduser", method = RequestMethod.GET)
@ResponseBody public String addUser(@RequestParam String username) {
User user = new User();
user.setFirstName(username);
user.setEmail("lala@gmail.com");
userRepository.save(user);
return "saved user: "+user.toString();
}
@RequestMapping("/delete/{userId}")
@ResponseBody public String deleteUser(@PathVariable("userId") Long userId) {
userRepository.delete(userRepository.findOne(userId));
return "deleted user";
}
}
@@ -0,0 +1,30 @@
package application.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
/**
* Created by lukasgol on 05.04.14.
*/
@Component
@PropertySource("app.properties")
public class PropertiesSolver {
@Autowired
Environment env;
@Value("${my.prop}")
String val;
public PropertiesSolver() {
}
public String getProperty(String param){
System.out.println(val);
return env.getProperty(param);
}
}