Nine. Spring Boot jpahibernatespring Data

Source: Internet
Author: User
Tags findone iterable

1. Project structure

2. Import Jar Package

<!--add SPRING-DATA-JPA dependency. -        <dependency>            <groupId>org.springframework.boot</groupId>            <artifactid >spring-boot-starter-data-jpa</artifactId>        </dependency>        <dependency>            < Groupid>org.springframework.boot</groupid>            <artifactid>spring-boot-starter-jdbc</ Artifactid>        </dependency>

3. Add and edit files: application.properties

Spring.datasource.url = Jdbc:mysql://localhost:3306/appleforwindows_test1?characterencoding=utf8&usessl=trueDescription: 5.7 of MySQL must be added: characterencoding=utf8&usessl=trueSpring.datasource.username=Rootspring.datasource.password=AppleforWindowsspring.datasource.driverClassName=Com.mysql.jdbc.Driverspring.datasource.max-active=20Spring.datasource.max-idle=8Spring.datasource.min-idle=8spring.datasource.initial-size=10CREATE TABLE Table Spring.jpa.database in database automatically=mysql# Show or not log forEach SQL Queryspring.jpa.show-sql =true# Hibernate DDL Auto (Create, create-drop, update) SPRING.JPA.HIBERNATE.DDL-auto =update# naming strategy#[org.hibernate.cfg.improvednamingstrategy #org. Hibernate.cfg.DefaultNamingStrategy] Spring.jpa.hibernate.naming-strategy =org.hibernate.cfg.improvednamingstrategy# stripped before adding them to the Entity manager) Spring.jpa.properties.hibernate.dialect= Org.hibernate.dialect.MySQL5Dialect

4. Add entity class Set\get

 PackageCom.ld.bean;Importjava.util.Date;Importjavax.persistence.Entity;ImportJavax.persistence.GeneratedValue;ImportJavax.persistence.GenerationType;Importjavax.persistence.Id;ImportCom.alibaba.fastjson.annotation.JSONField;/*an entity class was created. * * How to persist? * * 1, using @Entity to persist the entity class, when JPA detects that there are * * @Entity annotations in our entity classes, the corresponding table structure information is generated in the database. * * * How to specify the primary key and the primary key generation strategy? * * 2, use @id to specify the primary key.*/@Entity Public classSysuser {/*set as primary key*/@Id @GeneratedValue (Strategy=Generationtype.auto)PrivateLong ID; PrivateString name; PrivateString Phone; PrivateString Mail; @JSONField (Format= "Yyyy-mm-dd hh:mm")    PrivateDate Createtime; PrivateString Remark;  PublicLong getId () {returnID; }     Public voidsetId (Long id) { This. ID =ID; }     PublicString GetName () {returnname; }     Public voidsetName (String name) { This. Name =name; }     PublicString Getphone () {returnphone; }     Public voidSetphone (String phone) { This. Phone =phone; }     PublicString Getmail () {returnMail; }     Public voidSetmail (String mail) { This. Mail =Mail; }     PublicDate Getcreatetime () {returnCreatetime; }     Public voidsetcreatetime (Date createtime) { This. Createtime =Createtime; }     PublicString Getremark () {returnremark; }     Public voidSetremark (String remark) { This. Remark =remark; }    }

6. Create Server

Hint: can be viewed according to custom (Daorepository interface and inherit crudrepository<t, integer> the source of the class)

 PackageCom.ld.service;ImportJavax.annotation.Resource;Importjavax.transaction.Transactional;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;ImportOrg.springframework.stereotype.Service;ImportCom.ld.bean.SysUser;Importcom.ld.repository.SysUserRepository; @Service ("Sysuserservice") Public classSysuserservice {Static FinalLogger Logger = Loggerfactory.getlogger (sysuserservice.class); @ResourcePrivatesysuserrepository sysuserrepository;  PublicIterable<sysuser>FindAll () {Logger.info ("=>:findall"); returnSysuserrepository.findall (); }     PublicSysuser FindOne (Integer id) {Logger.info ("=>:findone"); returnSysuserrepository.findone (ID); } @Transactional PublicSysuser Save (Sysuser sysuser) {logger.info ("=>:save"); returnSysuserrepository.save (Sysuser); }     Public Longcount () {Logger.info ("=>:count"); returnSysuserrepository.count (); } @Transactional Public voidDelete (Integer id) {Logger.info ("=>:delete");    Sysuserrepository.delete (ID); }}

7. Test controller

 PackageCom.ld.controller;Importjava.util.Date;ImportJavax.annotation.Resource;Importorg.springframework.web.bind.annotation.RequestMapping;ImportOrg.springframework.web.bind.annotation.ResponseBody;ImportOrg.springframework.web.bind.annotation.RestController;ImportOrg.springframework.web.servlet.ModelAndView;ImportCom.ld.bean.SysUser;ImportCom.ld.service.SysUserService;ImportCom.ld.util.CommonTool, @RestController @requestmapping ("Sysuserweb") Public classSysuserweb {@ResourcePrivateSysuserservice Sysuserservice; @RequestMapping ("/index")     PublicModelandview Index () {Modelandview mv=NewModelandview ("redirect:amazeui/error.html"); returnMV; } @RequestMapping ("/getall") @ResponseBody PublicIterable<sysuser>GetAll () {returnSysuserservice.findall (); } @RequestMapping ("/save") @ResponseBody PublicSysuser Save () {Sysuser Sysuser=NewSysuser (); Sysuser.setname (Commontool.getrandomstring (6));        Sysuser.setphone (Commontool.getrandomphone ());        Sysuser.setmail (Commontool.getrandommail ()); Sysuser.setcreatetime (NewDate ());        Sysuserservice.save (Sysuser); returnSysuser; } @RequestMapping ("/queryone") @ResponseBody PublicSysuser Queryone () {returnSysuserservice.findone (1); }}

Nine. Spring Boot jpahibernatespring Data

Related Article

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.