In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-12 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/03 Report--
1 steps for using JPA
JPA=Java Persistence API,Java persistence layer API. JDK5 introduces JPA ORM to simplify Java EE development and integrate ORM technology. JPA defines JPQL (Java Persistence Query Language).
The default integrated JPA Provider for Spring Data JPA is Hibernate.
a. Add JPA dependency in pom.xml
Org.springframework.boot
Spring-boot-starter-data-jpa
b. Then increase the MYSQL dependency:
Mysql
Mysql-connector-java
c. Add the annotation @ Entity before the Model class
@ Entity
Public class user {
d. Add @ Id and @ GeneratedValue tags in the ID field
@ Id
@ GeneratedValue (strategy=GenerationType.AUTO)
Private Integer id
e. Extend the data warehouse interface:
Public interface UserRepository extends CrudRepository {}
f. Add dimensions in front of the control class
@ Controller
@ RequestMapping (path= "/ demo")
Public class UserController {
g. Add annotations in front of the control class repository properties
@ Autowired
Private UserRepository repository
h. Add annotations in and before the operation functions of the control class
@ GetMapping (path = "/ add")
Public @ ResponseBody String addNewUser (@ RequestParam String name, @ RequestParam String email) {
User u = new user ()
U.setName (name)
U.setEmail (email)
Repository.save (u)
Return "Saved"
}
@ Query ("select id,username,sex,address from # {# entityName} u where u.username=:name")
List findUserModel (@ Param ("name") String username)
i. Add dimensions in front of the Application class
@ SpringBootApplication
Public class Application {
Public static void main (String [] args) {
SpringApplication.run (Application.class, args)
}
}
j. Add configuration items to Application.properties
Spring.jpa.hibernate.ddl-auto=create
Spring.datasource.url=jdbc:mysql://localhost:3306/db_example
Spring.datasource.username=springuser
Spring.datasource.password=ThePassword
K. Notes and comments
@ Entity indicates that this class is an entity class and uses the default ORM rule, that is, the class name is the table name in the database table, and the class field name is the field name in the table. If you want to change this default rule, use @ Table to change the mapping rule between the class name and the table name in the database, and @ Column to change the mapping rule between the field name in class and the field name in db.
@ Query this is a way for JPA to support heavyweight queries, and there are two ways: one is JPQL's SQL language, and the other is native SQL language. Common scenarios are as follows:
* like expression:
@ Query (value = "select id,username,sex,address from UserModel b where b.name like%: name%")
List findByNameMatch (@ Param ("name") String name)
* * Native SQL language
@ Query (value = "select * from user b where b.username=?1", nativeQuery = true)
List findByName (String name)
* * use @ Param to annotate the injection parameters
@ Query (value = "select id,username,sex,address from UserModel b where b.username =: name AND b.address=:address AND b.sex=:sex")
List findByNamedParam (@ Param ("name") String name, @ Param ("address") String address, @ Param ("sex") long sex)
* * SPEL expression
@ Query (value = "select * from # {# entityName} b where b.name=?1", nativeQuery = true)
List findByName (String name)
L.JPA frame diagram
M.JPQL syntax
The basic format is as follows:
Select entity alias. Attribute name, entity alias. Property name from entity name as entity alias where entity alias. Entity attribute op comparison value
2 comparison between JPA query method and HQL query statement
Expression.
Query method
Hql query statement
And
FindByLastnameAndFirstname
... Where x.lastname =? 1 and x.firstname =? 2
Or
FindByLastnameOrFirstname
... Where x.lastname =? 1 or x.firstname =? 2
Is,Equals
FindByFirstname,findByFirstnameIs,findByFirstnameEqual
... Where x.firstname = 1?
Between
FindByStartDateBetween
... Where x.startDate between 1? And? 2
LessThan
FindByAgeLessThan
... Where x.age
< ?1 LessThanEqual findByAgeLessThanEqual … where x.age ⇐ ?1 GreaterThan findByAgeGreaterThan … where x.age >? 1
GreaterThanEqual
FindByAgeGreaterThanEqual
... Where x.age > =? 1
After
FindByStartDateAfter
... Where x.startDate >? 1
Before
FindByStartDateBefore
... Where x.startDate
< ?1 IsNull findByAgeIsNull … where x.age is null IsNotNull,NotNull findByAge(Is)NotNull … where x.age not null Like findByFirstnameLike … where x.firstname like ?1 NotLike findByFirstnameNotLike … where x.firstname not like ?1 StartingWith findByFirstnameStartingWith … where x.firstname like ?1 (parameter bound with appended %) EndingWith findByFirstnameEndingWith … where x.firstname like ?1 (parameter bound with prepended %) Containing findByFirstnameContaining … where x.firstname like ?1 (parameter bound wrapped in %) OrderBy findByAgeOrderByLastnameDesc … where x.age = ?1 order by x.lastname desc Not findByLastnameNot … where x.lastname ?1 In findByAgeIn(Collection ages) … where x.age in ?1 NotIn findByAgeNotIn(Collection age) … where x.age not in ?1 True findByActiveTrue() … where x.active = true False findByActiveFalse() … where x.active = false IgnoreCase findByFirstnameIgnoreCase … where UPPER(x.firstame) = UPPER(?1) 3 MyBatis和Hibernate的区别 SpringBoot都支持mybatis和hibernate,实际上它们都属于ORM框架,整体架构也差不多,如下:Contrast item
MyBatis
Hibernate
JDBC
Support
Support
JTA transaction
Support
Support
SessionFactoryBuilder
Support
Support
SessionFactory
Support
Support
Session
Support
Support
Development difficulty
The framework is easy to master.
Frame mastery is complex.
SQL statement
Support native SQL for performance optimization
HQL, which generally does not need to write native SQL statements, has low performance.
Caching
Second-level cache is supported. Dirty data may exist.
Support for secondary caching
Portability
Poor
Better
Automation
Semi-automatic
Fully automatic
Security.
Poor
Better
Journal
Poor
Better
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.