Use IntelliJ idea to develop SPRINGMVC Web site (iv) User management __spring

Source: Internet
Author: User
Absrtact: Through the management of the user table, more in-depth explain the operation of SPRINGMVC.

Reprint please specify the Source: Gaussic (a dedicated AI research but have to take into account the project's graduate).

Note: Before reading this article, please read:

Use IntelliJ idea to develop SPRINGMVC Web site (i) Development environment

Use IntelliJ idea to develop SPRINGMVC Web site (ii) Framework Configuration

Use IntelliJ idea to develop SPRINGMVC Web site (iii) database configuration

Visit GitHub Download the latest source code: Https://github.com/gaussic/SpringMVCDemo article has been for idea 15 made a certain update, part of the update is more important, please read the article and download the latest source code. vii. User Management

Since we want to do a blog management system, of course, first of all to achieve our user management. In the previous article, we have configured the database. Next, you need to implement some of the business logic of the site. 1. JPA Operation Definition

Before implementing user management operations, you need to explain the development work of JPA.

First, create a new userrepository interface within the Com.gaussic.repository package:

Let the interface inherit jparepository:

Package com.gaussic.repository;

Import com.gaussic.model.UserEntity;
Import org.springframework.data.jpa.repository.JpaRepository;
Import org.springframework.stereotype.Repository;

/**
* Created by Dzkan on 2016/3/8.
* *
@Repository public
interface Userrepository extends Jparepository<userentity, integer> {
}

In Jparepository, several simplified methods of manipulating the database are defined:

(1) FindAll (): Find all records in the table;

(2) FindOne (Integer ID): To find a record by ID;

(3) findbyxxx (Object xxx): Here xxx is a field name, based on the value of the field to find all records;

(4) Save () and delete (): Add a record and delete a record.

In addition, we can customize the new method in the repository, which will be mentioned later in actual development. 2, backstage management

In order to save as much space as possible, this eliminates the development of administrator operations. By default, when accessing/admin, it enters the background management.

(1) View all users

Add Maincontroller to the following form:

package com.gaussic.controller;
import com.gaussic.model.userentity;
import com.gaussic.repository.userrepository;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.stereotype.controller;
import org.springframework.ui.modelmap;
import org.springframework.web.bind.annotation.requestmapping;

import org.springframework.web.bind.annotation.requestmethod;

import java.util.list;
/** &NBSP;*&NBSP;CREATED&NBSP;BY&NBSP;DZKAN&NBSP;ON&NBSP;2016/3/8.  */@Controller public class maincontroller {    //  Automatic assembly Database Interface, No need to write the original connection to manipulate the database      @Autowired     UserRepository 

Userrepository;
     @RequestMapping (value =  "/",  method = requestmethod.get)     public string index ()  {        
return  "Index"; &NBSP;&NBSP;&NBSP;&NBSP}      @RequestMapping (value =  "/admin/users",  method  = requestmethod.get)     public string getusers (ModelMap modelMap )  {        //  query all records in the user table      

   list<userentity> userlist = userrepository.findall ();         //  pass all records to the JSP page to be returned, put it in the userlist     

    modelmap.addattribute ("UserList",  userlist);         //  return to the Pages directory admin/users.jsp page     
    return  "Admin/users"; &NBSP;&NBSP;&NBSP;&NBSP}}

Explain:

Automatic assembly: the equivalent of the database operation of the extremely simplified, as long as the definition can be directly to the database operation, no longer go to the Tube open connection, close the connection and other issues

All records found: FindAll () using the default method of Jparepository.

Modelmap: Used to pass the parameters in the Controller method to the desired JSP page for the related display.

Now you need to create a new directory admin under pages and create a new users.jsp page to manage your users:

<%@ page contenttype= "Text/html;charset=utf-8"  language= "java"  %> <%@ taglib  prefix= "C"  uri= "Http://java.sun.com/jsp/jstl/core"  %> <! doctype html> 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.