Spring Boot Login Registration Demo (ii)--database access

Source: Internet
Author: User

Access to the database via DATA-JPA

<Dependency>            <groupId>Org.springframework.boot</groupId>            <Artifactid>Spring-boot-starter-data-jpa</Artifactid>        </Dependency>        <Dependency>            <groupId>Mysql</groupId>            <Artifactid>Mysql-connector-java</Artifactid>        </Dependency>

Remember to add Mysql-connector-java dependencies, or you won't find the JDBC driver

Data source configuration, Spring boot configuration file is: application.properties

Spring.datasource.url=jdbc:mysql://localhost/testdemospring.datasource.username=  Guestspring.datasource.password=guestspring.datasource.driver-class-name=  Com.mysql.jdbc.Driverspring.jpa.properties.hibernate.hbm2ddl.auto=update

Configure according to your needs

The operation of the database is very simple, and JPA will get the method based on parsing the parameters in the user class

 PackageCom.jwen.login.dao;Importjava.util.List;Importorg.springframework.data.jpa.repository.JpaRepository;ImportOrg.springframework.data.jpa.repository.Query;ImportOrg.springframework.data.repository.query.Param;ImportCom.jwen.login.domain.User; Public InterfaceUserrepositoryextendsJparepository<user, long> {     PublicList<user>findbyname (String name);  PublicList<user>Findbynameandpassword (String name,string password); @Query ("From User u where u.name=:name")     PublicList<user> Finduser (@Param ("name") String name);}

If you define the name and password two properties in user, then you can use the Findbyname and Findbynameandpassword methods to query the data from the DB, do not write SQL operations, is not very convenient!

The defined userrepository inherits the Jparepository,jparepository built-in query methods and can be used directly.

Just defined a few userrepository methods, see below to use, in the SERVCIE layer

 PackageCom.jwen.login.service;Importorg.springframework.beans.factory.annotation.Autowired;ImportOrg.springframework.stereotype.Service;Importcom.jwen.login.dao.UserRepository;ImportCom.jwen.login.domain.User; @Service Public classUserService {@AutowiredPrivateuserrepository userrepository;  Public Booleanverifyuser (user user) {if(Userrepository.findbynameandpassword (User.getname (), User.getpassword ()). IsEmpty ()) {return false; } Else {            return true; }    }     PublicString registeruser (user user) {if(Userrepository.findbyname (User.getname ()). IsEmpty ()) {userrepository.save (user); return"Username" + user.getname () + "Registration Successful"; } Else {            return"Username" + user.getname () + "is already occupied! "; }    }}

Pass

@Autowired

Private Userrepository userrepository;

Automatic assembly of a Userrepository instance, with additions and deletions to the function, Save,delete,save, check is just the find operation

There's a little hole here, and it's been a while.

The findbyname returned should be a list, but what I wrote earlier was to return a class user,

Pit: Public User findbyname (String name);

Correct: Public list<user> findbyname (String name);

The key is the unit test does not error, the actual operation of the time to report null pointer, only refers to the above pit this line, also did not specify what the problem!

Spring Boot Login Registration Demo (ii)--database access

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.