Spring Boot JPA-Hibernate for additions, deletions, and changes in springboot primary learning series nine

1. Add mysql, spring-data-jpa, hibernate dependencies in pom.xml

        Some do not need to add hibernate dependencies, some must be added, you can try which one does not report an error

<dependency>
		<groupId>mysql</groupId>
		<artifactId>mysql-connector-java</artifactId>
</dependency>


<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!-- hibernate -->
 <dependency>
	            <groupId>org.hibernate</groupId>
	            <artifactId>hibernate-core</artifactId>
 </dependency>

2. Configure the following in application.properties:

########################################################
###datasource -- \u6307\u5b9amysql\u6570\u636e\u5e93\u8fde\u63a5\u4fe1\u606f.
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/springboot?characterEncoding=utf-8
spring.datasource.username = root
spring.datasource.password = yiqing
spring.datasource.driverClassName = com.mysql.jdbc.Driver
spring.datasource.max-active=20
spring.datasource.max-idle=8
spring.datasource.min-idle=8
spring.datasource.initial-size=10


########################################################
### Java Persistence Api --  Spring jpa\u7684\u914d\u7f6e\u4fe1\u606f.
########################################################
# Specify the DBMS
spring.jpa.database = MYSQL
# Show or not log for each sql query
spring.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

3. The project structure is as follows:


//@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
@SpringBootApplication
@Configuration
public class HelloApplication {
    
    public static void main(String[] args)
    {
        SpringApplication.run(HelloApplication.class, args);
    }

}
/**
 * Created an entity class.
 *
 * How to persist it?
 *
 * 1. Use @Entity to persist entity classes. When JPA detects that our entity classes have
 *
 * When annotated with @Entity, the corresponding table structure information will be generated in the database.
 *
 *
 * How to specify the primary key and the generation strategy of the primary key?
 *
 * 2. Use @Id to specify the primary key.
 *
 *
 *
 * @author Angel -- guardian angel
 * @version v.0.1
 * @date December 17, 2016
 */
@Entity
public class Cat {
	
	/**
	 * Use @Id to specify the primary key.
	 *
	 * Use code @GeneratedValue(strategy=GenerationType.AUTO)
	 * Specify the generation strategy of the primary key, mysql defaults to self-growth.
	 *
	 */
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	private int id;//Primary key.
	
	private String catName;//姓名. cat_name
	
	private int catAge;//年龄. cat_age;

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getCatName() {
		return catName;
	}

	public void setCatName(String catName) {
		this.catName = catName;
	}

	public int getCatAge() {
		return catAge;
	}

	public void setCatAge(int catAge) {
		this.catAge = catAge;
	}
	
	
}

public interface CatRepository extends CrudRepository<Cat, Integer> {

}

@Service
public class CatService {
	@Resource
	private CatRepository catRepository;
	
	@Transactional
	public void save(Cat cat){
		catRepository.save(cat);
	}
	@Transactional
	public void delete(int id){
		catRepository.delete(id);
	}
	
	public Iterable<Cat> getAll(){
		return catRepository.findAll();
	}
}

@RestController
@RequestMapping("/cat")
public class CatController {
	
	@Resource
	private CatService catService;
	
	@RequestMapping("/save")
	public String save(){
		Cat cat = new Cat();
		cat.setCatName("jack");
		cat.setCatAge(3);
		catService.save(cat);
		return "save ok.";
	}
	
	@RequestMapping("/delete")
	public String delete(){
		catService.delete(1);
		return "delete ok";
	}
	
	@RequestMapping("/getAll")
	public Iterable<Cat> getAll(){
		return catService.getAll();
	}
	
}


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324678593&siteId=291194637