• 【Spring boot 整合 JPA 保存数据和Spring boot 使用 JdbcTemplate 保存数据】


    Spring boot 整合 JPA 保存数据
    总体步骤:
    (1) 创建实体类 Demo, 如果已经存在,可以忽略。
    (2) 创建 jpa repository 类操作持久化。
    (3) 创建 service 类。
    (4) 创建 restful 请求类。
    (5) 测试
    代码如下:
    com.hpit.test.bean.Demo :
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    import javax.persistence.Table;
    /**
    * TODO DEMO标的实体类映射
    *
    * @author 郑江山
    * @Entity //加入这个注解,Demo就会进行持久化了
    */
    @Entity
    @Table (name = "DEMO" , schema = "ROOT" )
    public class Demo {
    @Id
    @GeneratedValue
    private Integer id ;
    @Column (name = "name" )
    private String name ;
    public Demo() {
    super ();
    }
    public Demo(Integer id , String name ) {
    super ();
    this . id = id ;
    this . name = name ;
    }
    public Integer getId() {
    return id ;
    }
    public void setId(Integer id ) {
    this . id = id ;
    }
    public String getName() {
    return name ;
    }
    public void setName(String name ) {
    this . name = name ;
    }
    }
    com.hpit.test.dao.DemoRepository(这是一个接口,没有具体的实现,这就是 JPA)
    :
    import org.springframework.data.repository.CrudRepository;
    import com.hpit.springboot01.entity.Demo;
    /**
    * TODO Demo表的DAO层接口,并没有具体的实现,继承基础CRUD实现
    * 泛型1:实体类 泛型2:主键映射类型
    *
    */
    public interface IDemoRepository extends CrudRepository {
    }
    到这里保存数据的方法就写完了。 CrudRepository 类把一些常用的方法都已经进行定义和实现了。那么你现在
    就可以在别的类引入调用了。
    另外就是 在 Spring Data 的核心接口里面 Repository 是最基本的接口了, spring 提供了很多实现了该接口的基本
    接口,如:CrudRepository,PagingAndSortingRepository,SimpleJpaRepository,QueryDslJpaRepository 等大
    量查询接口
    com . hpit . test . service .DemoService :
    import com.hpit.springboot01.dao.IDemoRepository;
    import com.hpit.springboot01.entity.Demo;
    /**
    * TODO 简单业务逻辑层
    *
    *
    */
    @Service ( "demoService" ) // 定义业务逻辑层
    public class DemoService {
    @Autowired // 自动装配DAO
    private IDemoRepository demoRepository ;
    @Transactional // 自动事务托管
    public void save(Demo demo ) {
    demoRepository .save( demo );
    }
    }
    开发数据保存控制器:
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import com.hpit.springboot01.entity.Demo;
    import com.hpit.springboot01.services.DemoService;
    /**
    * TODO 开发测试数据保存控制器
    *
    *
    */
    @Controller
    @RequestMapping ( "/demo2" )
    public class DemoController {
    @Autowired
    private DemoService demoService ;
    @ResponseBody
    @RequestMapping ( "/save" )
    public String save() {
    demoService .save( new Demo( "angle" ));
    return "ok the data was saved" ;
    }
    }
    运行程序,查看效果:
    8 Spring boot 使用 JdbcTemplate 保存数据
    整体步骤:
    (1) 在 pom.xml 加入 jdbcTemplate 的依赖;
    (2) 编写 DemoDao 类,声明为:@Repository,引入 JdbcTemplate
    (3) 编写 DemoService 类,引入 DemoDao 进行使用
    (4) 编写 Demo2Controller 进行简单测试。
    具体操作流程如下:
    使用 JdbcTemplate 类需要加入(如果在 JPA 已经加入的话,这个步骤就可以忽略了)
    那么只需要在需要使用的类中加入:
    @Resource
    private JdbcTemplate jdbcTemplate ;
    这样就可以使用 jdbcTemplate 进行数据库的操作了。
    比如:
    String sql = "insert into Demo(name,age) values(?,?)" ;
    jdbcTemplate .update(sql, new Object[]{demo.getName(),demo.getAge()});
    具体案例
    定义 Dao 层代码
    import org.springframework.beans.factory.annotation.Autowired;
    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.hpit.springboot01.entity.Demo;
    /**
    * TODO 使用JPA jdbc模板操作数据
    *
    *
    */
    @Repository ( "demoDao1" )
    public class DemoDaoUseJdbcTemplate {
    @Autowired //自动装配模板
    private JdbcTemplate jdbcTemplate ;
    /**
    * TODO 根据主键获取数据
    * @param id 主键
    * @return 实体对象
    */
    public Demo getById(Integer id ) {
    String sql = "select * from Demo where id = ?" ;
    //获取数据映射
    RowMapper mapper = new BeanPropertyRowMapper<>(Demo. class );
    return jdbcTemplate .queryForObject( sql , mapper , id );
    }
    }
    2.开发业务逻辑层
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Service;
    import com.hpit.springboot01.dao.DemoDaoUseJdbcTemplate;
    import com.hpit.springboot01.entity.Demo;
    /**
    * TODO 定义业务逻辑
    *
    *
    */
    @Service ( "demoService2" )
    public class DemoService2 {
    @Autowired
    private DemoDaoUseJdbcTemplate daoUseJdbcTemplate ;
    public Demo getById(Integer id ) {
    return daoUseJdbcTemplate .getById( id );
    }
    }
    3.开发控制器
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    import com.hpit.springboot01.entity.Demo;
    import com.hpit.springboot01.services.DemoService;
    import com.hpit.springboot01.services.DemoService2;
    /**
    * TODO 开发测试数据保存控制器
    *
    *
    */
    @Controller
    @RequestMapping ( "/demo2" )
    public class DemoController {
    @Autowired
    private DemoService demoService ;
    @Autowired
    private DemoService2 demoService2 ;
    @ResponseBody
    @RequestMapping ( "/save" )
    public String save() {
    demoService .save( new Demo( "angle" ));
    return "ok the data was saved" ;
    }
    @ResponseBody
    @RequestMapping ( "/show" )
    public Demo showDemo( @RequestParam (name = "no" , defaultValue = "1" , required = true ) Integer id ) {
    return demoService2 .getById( id );
    }
    }
    4.启动应用,查看效果
    当前前提是你的数据库中有 id=1 的数据了,不然会报错的:
    org.springframework.dao.EmptyResultDataAccessException
  • 相关阅读:
    会议审批 查询&会议签字
    vue项目中引入地图的详细教程
    spring bean 生命周期
    elementUI select组件value注意事项(value失效问题)
    开源项目有哪些机遇与挑战?
    对前端项目打包产物分析入库的好处
    利用面向对象方法,处理数据文件【Python】
    从“Hello,World”谈起(C++入门)
    excel导入poi中的数据使用cell.getStringCellValue()获取报错
    第十六章总结:反射和注解
  • 原文地址:https://blog.csdn.net/m0_72254454/article/details/127732565