实体类
package com.kfit.demo1.bean; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; /** * 创建了一个实体类。 * * 如何持久化呢? * * 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; 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; } }
DAO类
package com.kfit.demo1.dao; 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.demo1.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; } }
repository类
package com.kfit.demo1.repository; import org.springframework.data.repository.CrudRepository; import com.kfit.demo1.bean.Cat; /** * Repository -- 是接口 ,不是class. */ public interface CatRepository extends CrudRepository<Cat, Integer>{ }
package com.kfit.demo1.repository; import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.PagingAndSortingRepository; import org.springframework.data.repository.query.Param; import com.kfit.demo1.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); }
service类
package com.kfit.demo1.service; import javax.annotation.Resource; import javax.transaction.Transactional; import org.springframework.stereotype.Service; import com.kfit.demo1.bean.Cat; import com.kfit.demo1.repository.CatRepository; import com.kfit.demo1.dao.CatDao; import com.kfit.demo1.repository.Cat2Repository; @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); } }
controller类
package com.kfit.demo1.controller; import javax.annotation.Resource; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import com.kfit.demo1.bean.Cat; import com.kfit.demo1.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); } }