Technology excerpt that can be used for creating EC sites in Java training

Settings used this time

build.gradle

dependencies {
	compile('org.springframework.boot:spring-boot-starter-data-jpa')
	compile('org.springframework.boot:spring-boot-starter-thymeleaf')
	compile('org.springframework.boot:spring-boot-starter-web')
	providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
	compile('mysql:mysql-connector-java')
}

Login information retention

It is convenient to pack in a session to carry around login information

@SessionScope
@Component
public class AccountModel {
	private String id;
	private String name;
}
@Controller
public class MainController {

	@Autowired
	private AccountModel accountModel;
	
	/**
 * Login process
	 * @param model
	 * @param id
	 * @param post
	 * @return
	 */
	@RequestMapping("login")
	public String login(Model model,@RequestParam("id") String id,@RequestParam("pass") String pass) {
		accountModel.setId(id);
 // TODO password check
		return "shop";
	}
}
  <form method="post" th:action="@{login}">
  	<input type="text" name="id" />
  	<input type="text" name="pass" />
 <button> Login </ button>
  </form>

With this, you can check it every time you log in for the first time. If you do not check and retain the session information at the time of purchase, you can move to the personal information registration screen.

Data search

See below for the naming rules for methods that are automatically implemented in SpringDataJPA. [Spring Data JPA] Naming rules for automatically implemented methods

@Entity
@Table(name="item")
public class Item {
	@Id
	@GeneratedValue
	private int id;
	private String name;
	private int price;
}
public interface ItemRepos extends JpaRepository<Item, Integer> {
	/**
	 * select * from item where name like %{name}%
	 * @param name
	 * @return
	 */
	public List<Item> findByNameContains(String name);
}
@Controller
public class MainController {

	@Autowired
	private ItemRepos itemRepos;

	/**
 * Search process
	 * @param model
	 * @param name
	 * @return
	 */
	@RequestMapping("search")
	public String search(Model model,@RequestParam("name") String name) {
		List<Item> itemList = itemRepos.findByNameContains(name);
		model.addAttribute("itemList", itemList);
		return "shop";
	}

}
  <form method="post" th:action="@{search}">
  		<input type="text" name="name"/>
 <button> Search </ button>
  </form>

Data registration

JpaRepository # save performs update if pk is duplicated, insert if not

	/**
 * registration process
	 * @param model
	 * @param name
	 * @param price
	 * @return
	 */
	@RequestMapping("submit")
	public String submit(Model model,@RequestParam("name") String name,@RequestParam("price") String price) {
		Item item = new Item();
		item.setName(name);
		item.setPrice(Integer.valueOf(price));
		itemRepos.save(item);
		return "shop";
	}

Caution

Since we are assuming training this time, we have not implemented detailed parts, but there are many points to consider such as input value check and separation of processing layer.

Recommended Posts

Technology excerpt that can be used for creating EC sites in Java training
Summary of ORM "uroboroSQL" that can be used in enterprise Java
Write a class that can be ordered in Java
Convenient shortcut keys that can be used in Eclipse
Problems that can easily be mistaken for Java and JavaScript
Java (super beginner edition) that can be understood in 180 seconds
[Java 8] Sorting method in alphabetical order and string length order that can be used in coding tests
Reference memo / In-memory LDAP server that can be embedded in Java
Static analysis tool that can be used on GitHub [Java version]
Note that system properties including JAXBContext cannot be used in Java11
I made a question that can be used for a technical interview
SwiftUI View that can be used in combination with other frameworks
[Java 8] Until converting standard input that can be used in coding tests into a list or array
Java file input / output processing that can be used through historical background
Java code that cannot be used from Kotlin (for in-house study sessions)
[Spring Boot] List of validation rules that can be used in the property file for error messages
[Android Studio] Description that can be continuously input in SQLite Database [Java]
[Android] I want to create a ViewPager that can be used for tutorials
I made a THETA API client that can be used for plug-in development
Technology for reading Java source code in Eclipse
Organize methods that can be used with StringUtils
[Ruby] Methods that can be used with strings
The story that the port can no longer be used in the Spring boot sample program
Basic functional interface that can be understood in 3 minutes
This and that for editing ini in Java. : inieditor-java
[Java] Explains ConcurrentModificationException that occurs in java.util.ArrayList for newcomers
About the matter that hidden_field can be used insanely
Ruby on Rails 5 quick learning practice guide that can be used in the field Summary
Introduction of jQuery-until it can be used (record executed in preparation for asynchronous communication) haml
Summary of css selectors that can be used with Nookogiri
Create a page control that can be used with RecyclerView
Create a jar file that can be executed in Gradle
The case that @Autowired could not be used in JUnit5
Firebase-Realtime Database on Android that can be used with copy
[Question] Can nullif be used in the count function in JPQL?
Introduction to Rakefile that can be done in about 10 minutes
Java 14 new features that could be used to write code
Whether options can be used due to different Java versions
Tools and commands that may be useful for Java troubleshooting
Object-oriented design that can be used when you want to return a response in form format
How to make a key pair of ecdsa in a format that can be read by Java