boot

package com.tuyano.springboot;

import java.util.Optional;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.tuyano.springboot.repositories.MyDataRepository;

//@RestController

//public class HeloController {
//for 1
		// public String index() {
		//return "Hello Spring-Boot World!!";
		//}
//for 2
		//	@RequestMapping("/{num}")
		//	public String index(@PathVariable int num) {
		//		int res = 0;
		//		for(int i = 1; i<=num;i++) {
		//			res += i;
		//		}
		//			return "total " + res;
		//	}
//for 3
		//	String[] names = {"taro", "jiro", "saburo", "shiro"};
		//	String[] mails = {"taro@mail", "jiro@mail", "saburo@mail", "shiro@mail"};
		//	
		//		@RequestMapping("/{id}")
		//		public DataObject index(@PathVariable int id) {
		//			return new DataObject(id, names[id], mails[id]);
		//		}

//}

////for 3
//class DataObject {
//	private int id;
//	private String name;
//	private String value;
//	public DataObject(int id, String name, String value) {
//		super();
//		this.id = id;
//		this.name = name;
//		this.value = value;
//	}
//
//	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 String getValue() {
//		return value;
//	}
//	public void setValue(String value) {
//		this.value = value;
//	}
//}



//@Controller
//public class HeloController {
//for 1
		//		@RequestMapping("/")
		//		public String index() {
		//			return "index";
		//		}
//for 2 use ModelAndView
		//@RequestMapping("/{num}")
		//	public ModelAndView index(@PathVariable int num, ModelAndView mav) {
		//		int res = 0;
		//		for(int i = 1; i<=num;i++) {
		//			res += i;
		//		}
		//		mav.addObject("msg", "total: " + res);
		//		mav.setViewName("index");
		//		return mav;
		//	}
		//}
//for 3 use EchoServlet(Form&Return)
		// 記述途中
		//	@RequestMapping("/{num}")
		//	public ModelAndView index(@PathVariable int num, ModelAndView mav) {
		//		int res = 0;
		//		for(int i = 1; i<=num;i++) {
		//			res += i;
		//		}
		//		mav.addObject("msg", "total: " + res);
		//		mav.setViewName("index");
		//		return mav;
		//	}
//}

		//@Controller
		//public class HeloController {
		//	@Autowired
		//	MyDataRepository repositrory;
		//	@RequestMapping("/")
		//		public ModelAndView index(ModelAndView mav) {
		//			mav.setViewName("index");
		//			mav.addObject("msg", "this is sample content.");
		//			Iterable<MyData> list = repositrory.findAll();
		//			mav.addObject("data", list);
		//			return mav;
		//		}
		//}

@Controller
public class HeloController {
	
		//	@Autowired
		//	MyDataRepository repository;
		//	
		//	@RequestMapping("/")
		//	public ModelAndView index(ModelAndView mav) {
		//		mav.setViewName("index");
		//		mav.addObject("msg","this is sample content.");
		//		Iterable<MyData> list = repository.findAll();
		//		mav.addObject("datalist",list);
		//		return mav;
		//	}
		//
		//	@RequestMapping(value = "/", method = RequestMethod.POST)
		//	public ModelAndView form(
		//			@ModelAttribute("formModel") MyData mydata, 
		//			ModelAndView mav) {
		//		repository.saveAndFlush(mydata);
		//		return new ModelAndView("redirect:/");
		//	}
			
		// P234 send data by form
		//	@Autowired
		//	MyDataRepository repository;
		//	
		//	@RequestMapping(value = "/", method = RequestMethod.GET)
		//	public ModelAndView index(@ModelAttribute("formModel") MyData mydata, ModelAndView mav) {
		//		mav.setViewName("index");
		//		mav.addObject("msg","this is sample content.");
		//		Iterable<MyData> list = repository.findAll();
		//		mav.addObject("datalist",list);
		//		return mav;
		//	}
		//
		//	@RequestMapping(value = "/", method = RequestMethod.POST)
		//	public ModelAndView form(
		//			@ModelAttribute("formModel") MyData mydata, 
		//			ModelAndView mav) {
		//		repository.saveAndFlush(mydata);
		//		return new ModelAndView("redirect:/");
		//	}
	
		// P234 edit data by form	
		//	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
		//	public ModelAndView edit(
		//			@ModelAttribute MyData mydata,
		//			@PathVariable int id,
		//			ModelAndView mav) {
		//		mav.setViewName("edit");
		//		mav.addObject("title","this is edit page.");
		//		Optional<MyData> data = repository.findById((long)id);
		//		mav.addObject("formModel",data.get());
		//		return mav;
		//	}
		//	
		//	@RequestMapping(value = "/edit", method = RequestMethod.POST)
		//	public ModelAndView update(
		//			@ModelAttribute MyData mydata,
		//			ModelAndView mav) {
		//		repository.saveAndFlush(mydata);
		//		return new ModelAndView("redirect:/");
		//	}
	
	
	@Autowired
	MyDataRepository repository;
	
	@PersistenceContext
	EntityManager entityManager; //●
	
	MyDataDaolmpl dao; //●
	
//	@PostConstruct
//	public void init(){
//		dao = new MyDataDaolmpl(entityManager); //●
//		MyData d1 = new MyData();
//		d1.setName("tuyano");
//		d1.setMail("syoda@tuyano.com");
//		d1.setMemo("090999999");
//		repository.saveAndFlush(d1);
//		MyData d2 = new MyData();
//		d2.setName("hanako");
//		d2.setMail("hanako@flower");
//		d2.setMemo("080888888");
//		repository.saveAndFlush(d2);
//		MyData d3 = new MyData();
//		d3.setName("sachiko");
//		d3.setMail("sachico@happy");
//		d3.setMemo("070777777");
//		repository.saveAndFlush(d3);
//	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index(ModelAndView mav) {
		mav.setViewName("index");
		mav.addObject("msg","MyDataのサンプルです。");
		dao = new MyDataDaolmpl(entityManager);
		Iterable<MyData> list = dao.getAll(); //●
		mav.addObject("datalist", list);
		return mav;
	}
}


*******************************************************************

package com.tuyano.springboot;

import java.io.Serializable;
import java.util.List;
import com.tuyano.springboot.MyData;

public interface MyDataDao <T> extends Serializable{
	public List<T> getAll();
}

*******************************************************************************************************

package com.tuyano.springboot;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

import org.springframework.stereotype.Repository;

@Repository
public class MyDataDaolmpl implements MyDataDao<MyData> {
	private static final long serialVersionUID = 1L;
	
	private EntityManager entityManager;
	
	//Constructor
	public MyDataDaolmpl() {
		super();
	}
	
	public MyDataDaolmpl(EntityManager manager) {
		this();
		entityManager = manager;
	}
//use JPQLver p290 	
//			@Override
//			public List<MyData> getAll() {
//				Query query = entityManager.createQuery("from MyData");
//				@SuppressWarnings("unchecked")
//				List<MyData> list = query.getResultList();
//				entityManager.close();
//				return list;
//			}
//use CriteriaAPIver p318
	@Override
	public List<MyData> getAll() {
		List<MyData> list = null;
		CriteriaBuilder builder = entityManager.getCriteriaBuilder();
		CriteriaQuery<MyData> query = builder.createQuery(MyData.class);
		Root<MyData> root = query.from(MyData.class);
//		query.select(root);
//sorting
		query.select(root).orderBy(builder.desc(root.get("id")));
		list = (List<MyData>)entityManager.createQuery(query).getResultList();
		return list;
	}
}

*******************************************************************************************************************************************************

package com.tuyano.springboot;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;

@Entity
@Table(name="goods")
public class MyData {
	@Id
	@GeneratedValue(strategy=GenerationType.IDENTITY)
//	@GeneratedValue(strategy = GenerationType.AUTO)
	@Column
	private	long id;
	
	@Column(length = 50, nullable = false)
	private String name;
	
	@Column(length = 200, nullable = true)
	private String mail;
	
//	@Column(nullable = true)
//	private Integer age;
	
	@Column(nullable = true)
	private String memo;
	
	public long getId() {
		return id;
	}
	public void setId(long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getMail() {
		return mail;
	}
	public void setMail(String mail) {
		this.mail = mail;
	}
//	public Integer getAge() {
//		return age;
//	}
//	public void setAge(Integer age) {
//		this.age = age;
//	}
	public String getMemo() {
		return memo;
	}
	public void setMemo(String memo) {
		this.memo = memo;
	}
}