springboot集成hibernate-jpa方式
1、maven依赖<!-- Hibernate依赖 --> org.springframework.boot spring-boot-starter-data-jpa mysql mysql-connector-java2、application.yml# Hibernate JPAspring: datasource:
1、maven依赖
<!-- Hibernate 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2、application.yml
# Hibernate JPA
spring:
datasource:
url: jdbc:mysql://localhost:3306/db_demo
username: root
password: root
driverClassName: com.mysql.jdbc.Driver
jpa:
database: MYSQL
hibernate:
ddl-auto: update
dialect: org.hibernate.dialect.MySQL5Dialect
show-sql: true
3、jpa配置
@Configuration @EntityScan(basePackages="com.glodon.springboot.po") @EnableJpaRepositories(basePackages= "com.glodon.springboot.repository") @EnableTransactionManagement public class HibernateJpaConfig { }
4、测试demo
package com.glodon.springboot.controller;
import com.alibaba.fastjson.JSON;
import com.glodon.springboot.po.Person;
import com.glodon.springboot.repository.PersonRepository;
import com.glodon.springboot.service.IHibernateJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* Created by zhangh-ag on 2017-06-12.
*/
@RestController
@RequestMapping(value = "/api/hibernate")
public class HibernateController {
@Autowired
private PersonRepositorypersonRepository;
@Autowired
private IHibernateJpaServicejpaService;
@GetMapping(value = "/jpa")
public StringtestJpa() {
Person oPerson = new Person();
oPerson.setName("小扎");
oPerson.setAge(30);
try {
personRepository.save(oPerson);
List<Person> oPersonList = personRepository.findByName("小扎");
return JSON.toJSONString(oPersonList);
} catch(Exception e) {
e.printStackTrace();
return JSON.toJSONString(e);
}
}
@GetMapping(value = "/jpaTransaction")
public StringtestJpaTransaction(){
try {
jpaService.testHibernateJpaTransaction();
return "test";
} catch(Exception e) {
return e.getMessage();
}
}
}
package com.glodon.springboot.service;
import org.springframework.transaction.annotation.Transactional;
/**
* Created by zhangh-ag on 2017-06-12.
*/
public interface IHibernateJpaService {
@Transactional
void testHibernateJpaTransaction();
}
package com.glodon.springboot.service.impl;
import com.glodon.springboot.po.Person;
import com.glodon.springboot.repository.PersonRepository;
import com.glodon.springboot.service.IHibernateJpaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
/**
* Created by zhangh-ag on 2017-06-12.
*/
@Service
public class HibernateJpaServiceImpl implements IHibernateJpaService{
@Autowired
private PersonRepositorypersonRepository;
@Override
public void testHibernateJpaTransaction() {
Person oPerson1 = new Person();
oPerson1.setName("tran_1");
personRepository.save(oPerson1);
if (null== oPerson1.getAge() ||!oPerson1.getAge().equals(0)) {
throw new RuntimeException("事务测试");
}
Person oPerson2 = new Person();
oPerson2.setName("tran_2");
personRepository.save(oPerson2);
}
}
package com.glodon.springboot.repository;
import com.glodon.springboot.po.Person;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
/**
* Created by zhangh-ag on 2017-06-12.
*/
public interface PersonRepository extends JpaRepository<Person, String> {
@Query("from Person p where p.name =:name")
List<Person> findByName(@Param(value = "name") String name);
}
package com.glodon.springboot.po;
import javax.persistence.*;
/**
* Created by zhangh-ag on 2017-06-12.
*/
@Entity
@Table(name = "t_person")
public class Person {
@Id
@GeneratedValue(strategy= GenerationType.AUTO)
@Column(name="id")
private Integerid;
@Column(name="name")
private Stringname;
@Column(name="age")
private Integerage;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public StringgetName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public IntegergetAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
更多推荐
所有评论(0)