1. Windows 7 Enterprise Edition
2. Idea 14
3. JDK 1.8
4. Maven 3.5.2
5. Mariadb
6. Sqlyog
Agregar settings.xml en el directorio de conf en el directorio Maven al siguiente contenido:
1. Uso de Alibaba Cloud El repositorio es mucho más rápido que el sitio web oficial.
<prirror> <id> nexus-aliyun </id> <precorof> Central </prrorof> <name> nexus aliyun </name> <url> http://maven.aliyun.com/nexus/content/groups/public </sl> </prorror>
2. Configuración global de JDK
< <maven.compiler.target> 1.8 </maven.compiler.target> <maven.compiler.compilerversion> 1.8 </maven.compiler.compilerversion> </propiies> </propiedades> </propiedades> </file>
1. Configuración de maven: seleccione el directorio Maven y configure el archivo y el repositorio local al mismo tiempo.
2. Configuración de codificación de personajes
Seleccione Habilitar Auto-Import y cree el directorio del proyecto como se muestra a continuación:
1. Pom.xml
<? xml versión = "1.0" encoding = "utf-8"?> <Project xmlns = "http://maven.apache.org/pom/4.0.0" xmlns: xsi = "http://www.w3.org/2001/xmlschemainstance" xsi: schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelever> 4.0.0 </modelversion> <MoupRid> cn.temptation </groupid> <artifactid> <versión> 1.0-snapshot </versión> <!-Configuración predeterminada para usar el arranque de Spring-> <Parent> <MoupRoD> org.springframework.boot </groupid> <artifactid> spring-boot-sharter-parent </artifactid> <versión> 2.0.0.0.release </versión> </parent> <fependency> <!-web-> <rependency> <MoupRoD> org.springframework.boot </groupid> <artifactid> spring-boot-starter-web </artifactid> </dependency> <!-thymeleaf-> <ependency> <proupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-thymeleaf </artifactid> </artifactid> mysql ---> <pendency> <MoupRid> mysql </proupid> <artifactid> mysql-connector-java </artifactid> <versión> 5.1.21 </versión> </pendency> <!-JPA-> <pendency> <proupid> Org.springframeWork.Boot </groupid> <artifactID> spring-boot-starter-data-jPa </artifactid> </pendency> </dependencias> </proyecto>
2. Cree una nueva aplicación.
# Conexión de la base de datos Spring.datasource.url = jdbc: mysql: //127.0.0.1: 3306/testSpring.dataSource.username = rootspring.datasource.password = saspring.datasource.driver-class-name = com.mysql.jdbc.driver# jpA SEGURATION CONFIGURACIÓN spring.jpa.properties.hibernate.hbm2ddl.auto = actualización
3. Cree la clase de inicio del programa SpringBoot SpringBootApplication.java
paquete cn.temptation; import org.springframework.boot.springapplication; import org.springframework.boot.autoconfigure.springbootapplication; @springbootapplicationPublic Class springBootapplication {público estático videado (string [] args) {// springboot starts Springapplication.run (springbootapplication.class, args); }}4. Crear categoría de clase de entidad.java
paquete cn.temptation.model; import javax.persistence.*; // Library Building Table // Drop Table Categoría; //// Crear categoría de tabla // (// CategyId int Auto_incement Key primario, // CategoryName Varchar (10) no null //); //////////////superó en los valores de categoría (null, 'móvil'), (null, 'book'), (null/'); (Nulo, 'zapatos'); //// select * de la categoría; @entity @table (name = "Categoría") Categoría de clase pública {@ID @GeneratedValue (estrategia = GenerationType.identity) @Column (name = "CategoryId") privado Integer CategoryId; @Column (name = "CategoryName") String privado CategoryName; public Integer getCategoryId () {return categoryId; } public void setCategoryId (Integer CategyId) {this.CategoryId = CategyId; } public String getCategoryName () {return categoryName; } public void setCategoryName (String CategyName) {this.CategoryName = CategyName; }}5. Crear interfaz DAO
paquete cn.temptation.dao; import cn.temptation.model.category; import org.springframework.data.jpa.repository.jparpository; public interfaz categorydao extiende jparepository <categoría, entero> {}6. Cree la clase de controlador CategyController.java
paquete cn.temptation.web; import cn.temptation.dao.categorydao; import cn.temptation.model.category; import org.springframework.beans.factory.annotation.aUtowired; import org.springfringfrfframeward; importación; org.springframework.data.domain.pageable; import org.springframework.data.domain.sort; import org.springframework.stereotype.controller; import org.springframework.web.bind.annotation.requestmapping; import org.springframeWork.webind.nannotation org.springframework.web.servlet.modelandview; import java.util.list; @ControllerPublic CategyController {@aUtowired private categorydao categorydao; /** * consulta de impagación * * @return * /// @requestmapping ("/categoryList") // public modelandView categoryList () {// list <Cateatory> list = categoryDao.findall (); ///// ModelandView mav = nuevo modelandview ("categoryList"); // mav.adobject (list (list "; }/** * consulta de paginación * * @return */@requestmapping ("/categoryList") public ModelAndView CategyList (@RequestParam (valor = "inicio", defaultValue = "0") Integer inicio, @RequestParam (value = "Limit", defaultValue = "2") Limit) {Start = Start <0? 0: Inicio; Sort sort = nuevo sort (sort.default_direction, "categoryId"); Concordante pagable = nuevo pagequest (inicio, límite, ordenación); Página <Category> Page = CategyDao.Findall (Pagable); // System.out.println (page.getNumber ()); // system.out.println (page.getNumberOfElements ()); // system.out.println (page.getSize ()); // system.out.pintln (pageTalements (); System.out.println (page.gettotalpages ()); // system.out.println (page.isfirst ()); // system.out.println (page.islast ()); ModelandView mav = new ModelAndView ("CategyList"); mav.addoBject ("página", página); regresar mav; }/*** nueva vista en la categoría* @return*/@RequestMapping ("/categoryInit") public String CategyInit () {return "CategyInit"; }/** * nueva operación en la categoría * @param modelo * @return */@requestmapping ("/categoryInsert") public String CategyInsert (modelo de categoría) {categoryDao.save (modelo); return "Redirect: CategyList"; }/** * Categy Eliminar operación * @param categoryId * @return */@requestmapping ("/categoryDelete") public String CategoryDelete (Integer CategyId) {CategyDao.DeleteById (CategyId); return "Redirect: CategyList"; }/** * Vista de edición de categorías * @param categoryId * @return */@requestmapping ("/categoryEdit") public ModelAndView CategyEdit (Integer CategoryId) {Category Model = CategyDao.getone (CategyId); ModelandView mav = new ModelAndView ("CategyEdit"); mav.addoBject ("categoría", modelo); regresar mav; }/** * Operación de edición de categorías * @param modelo * @return */@requestmapping ("/categoryUpdate") public String categoryUpdate (modelo de categoría) {categoryDao.save (modelo); return "Redirect: CategyList"; }}7. Cree un nuevo directorio de plantillas en el directorio de recursos y cree una capa de presentación: página de lista de categorías (categoryList.html), categoría nueva página (categoryInit.html), página de edición de categorías (categoryEdit.html)
Página CategoryList (CategyList.html)
<! DocType html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> Lista de categorías </title> <style> table, th, td {border: 1px sólido verde; Colapso de borde: colapso; } </style> </head> <body> <a th: href = "@{/categoryInit}"> Agregar </a> <topla> <tr> <th> número de categoría </th> <th> nombre de categoría </th> <th> operación </th> </tr> <!-No hay una página de página-> <!-<tr th: item: item: $ {list} " Traversal-> <tr th: cada uno = "item: $ {page.content}"> <td th: text = "$ {item.categoryID}"> Número de categoría </td> <td th: text = "$ {item.categoryName}"> nombre de categoría </td> <td> <a th: href = "@{/categoryEdit (categoryId = $ {item.categoryId})}"> editar </a> <a th: href = "@{/categoryDelete (categoryId = $ {item.categoryId})}"> delete </a> </td> </td> </tr> </table> <scategyId})} "> delete </a> </td> </td> </tr> </table> <tiver> <siver> th: href = "@{/categoryList (start = 0)}"> [home] </a> <a th: if = "$ {no página.isfirst ()}" th: href = "@{/categoryList (inicio = $ {page.totalPage-1}})}"> [Página anterior] </a> <a th: si = "$ {no página. th: href = "@{/categoryList (start = $ {page.number+1})}"> [página siguiente] </a> <a th: href = "@{/categoryList (start = $ {page.totalpages-1})}"> [Última página] </a> </div> </body> </hml>Nueva página para categoría (categoryInit.html)
<! DocType html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> nueva categoría </title> </head> <body> <formy action = "categorsert" método = "post"> <etiqueta for = "txtCategoryName"> Nombre de la categoría: </etlabe> <input type = "text" id = "ttxtcategy" " name = "CategoryName"/> <br/> <input type = "Envit" value = "Envir"> </form> </body> </html>
Página de edición de categorías (categoryEdit.html)
<! Doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> categoría editar </title> </head> <body> <formy action = "categoryUpdate" método = "post"> <input type = "hidden" id = "txtCategoryID" name = "categoryID" th: field = "$ {category.CategoryId}"/> <br/> <etiqueta for = "txtCategoryName"> Nombre de categoría: </etiqueta> <input type = "text" id = "txtCategoryName" name = "categoryName" th: campo = "$ {categoría valor = "enviar"> </form> </body> </html>Resumir
Lo anterior es la idea+Maven+SpringBoot+JPA+THYMELEaf Implementación de crud y paginación presentada por el editor. Espero que sea útil para todos. Si tiene alguna pregunta, déjame un mensaje y el editor responderá a todos a tiempo. ¡Muchas gracias por su apoyo al sitio web de Wulin.com!