欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring boot中的crud方式

程序员文章站 2022-04-23 15:58:10
...

1.引入的CrudRepository中的crud等等

2.引入的PagingAndSortingRepository中的crud等等

3.自己写的jdbcTemplate

0.数据库引入和pom.xml

########################################################
###datasource -- 指定mysql数据库连接信息.
########################################################
spring.datasource.url = jdbc:mysql://localhost:3306/test
spring.datasource.username = root
spring.datasource.password = 123
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的配置信息.
########################################################
# 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
	<!-- 添加MySQL数据库驱动依赖包. -->
		<dependency>
				<groupId>mysql</groupId>
				<artifactId>mysql-connector-java</artifactId>
		</dependency>
		
		<!-- 添加Spring-data-jpa依赖. -->
		<dependency>
		    <groupId>org.springframework.boot</groupId>
		    <artifactId>spring-boot-starter-data-jpa</artifactId>
		</dependency>

1.引入的CrudRepository中的crud等等

 实体类

/**
 * 创建了一个实体类。
 * 
 * 如何持久化呢?
 * 
 * 1、使用@Entity进行实体类的持久化操作,当JPA检测到我们的实体类当中有
 * 
 * @Entity 注解的时候,会在数据库中生成对应的表结构信息。
 * 
 * 
 * 如何指定主键以及主键的生成策略?
 * 
 * 2、使用@Id指定主键.
 * 

 */
@Entity
public class Cat {
	
	/**
	 * 使用@Id指定主键.
	 * 
	 * 使用代码@GeneratedValue(strategy=GenerationType.AUTO)
	 * 指定主键的生成策略,mysql默认的是自增长。
	 * 
	 */
	@Id @GeneratedValue(strategy=GenerationType.AUTO)
	private int id;//主键.
	
	private String catName;//姓名. cat_name
	
	private int catAge;//年龄. cat_age;
//省略get set
}

controller

package com.kfit.demo.controller;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.kfit.demo.bean.Cat;
import com.kfit.demo.service.CatService;

@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();
	}
	
	@RequestMapping("/findByCatName")
	public Cat findByCatName(String catName){
		return catService.findByCatName(catName);
	}
	
	@RequestMapping("/findByCatName2")
	public Cat findByCatName2(String catName){
		System.out.println("CatController.findByCatName2()");
		return catService.findByCatName2(catName);
	
	}
	
	@RequestMapping("/selectByCatName")
	public Cat selectByCatName(String catName){
		return catService.selectByCatName(catName);
	}
}

service

package com.kfit.demo.service;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import com.kfit.demo.bean.Cat;
import com.kfit.demo.dao.CatDao;
import com.kfit.demo.repository.Cat2Repository;
import com.kfit.demo.repository.CatRepository;

@Service
public class CatService {
	
	@Resource
	private CatRepository catRepository;
	
	@Resource
	private Cat2Repository cat2Repository;
	
	@Resource
	private CatDao catDao;
	
	
	/**
	 * save,update ,delete 方法需要绑定事务.
	 * 
	 * 使用@Transactional进行事务的绑定.
	 * 
	 * @param cat
	 */
	
	//保存数据.
	@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();
	}
	
	
	public Cat findByCatName(String catName){
		return cat2Repository.findByCatName(catName);
	}
	
	
	public Cat findByCatName2(String catName){
		return cat2Repository.findMyCatName(catName);
	}
	
	
	public Cat selectByCatName(String catName){
		return catDao.selectByCatName(catName);
	}
}

dao---CrudRepository的使用

package com.kfit.demo.repository;

import org.springframework.data.repository.CrudRepository;

import com.kfit.demo.bean.Cat;

/**
 * Repository -- 是接口 ,不是class.

 */
public interface CatRepository extends CrudRepository<Cat, Integer>{

}

dao--PagingAndSortingRepository使用

package com.kfit.demo.repository;

import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

import com.kfit.demo.bean.Cat;

public interface Cat2Repository extends PagingAndSortingRepository<Cat, Integer>{
	
	
	/**
	 * 1/ 查询方法 以 get | find | read 开头.
	 * 2/ 涉及查询条件时,条件的属性用条件关键字连接,要注意的是条件属性以首字母大写。
	 */
	
	//根据catName进行查询 : 根据catName进行查询.
	public Cat findByCatName(String catName);
	
	/**
	 * 如何编写JPQL语句, 
	 * Hibernate  -- HQL语句.
	 * JPQL 语句 和HQL语句是类似的.
	 */
	@Query("from Cat where catName=:cn")
	public Cat findMyCatName(@Param("cn")String catName);
}

dao--jdbcTemplate使用

import javax.annotation.Resource;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import com.kfit.demo.bean.Cat;

/**
 * 使用@Repository注解,标注这是一个持久化操作对象.

 */
@Repository
public class CatDao {
	
	@Resource
	private JdbcTemplate jdbcTemplate;
	
	public Cat selectByCatName(String catName){
		
		/**
		 * 1、定义一个Sql语句;
		 * 2、定义一个RowMapper.
		 * 3、执行查询方法.
		 */
		String sql = "select *from cat where cat_name=?";
		RowMapper<Cat> rowMapper = new BeanPropertyRowMapper<>(Cat.class);
		Cat cat = jdbcTemplate.queryForObject(sql, new Object[]{catName}, rowMapper);
		
		return cat;
	}
	
}