zoukankan      html  css  js  c++  java
  • Spring Boot(二)jpa操作数据库

    application.yml配置:

    spring:
    # 数据库连接
    datasource:
    url: jdbc:mysql://192.168.1.200:3306/demo?serverTimezone=UTC&useUnicode=true&characterEncoding=utf8&useSSL=false
    username: root
    password: 123456
    driver-class-name: com.mysql.cj.jdbc.Driver
    # jpa配置
    jpa:
    show-sql: true

    # 自定义配置类
    blog:
    name: cqrunnerxjzh
    address: https://www.cnblogs.com/gaoqiao/

    pom.xml配置

    <!--Lombox使用-->
    <dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <optional>true</optional>
    <version>1.18.2</version>
    </dependency>
    <!--数据库操作-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    </dependency>
    <dependency>
    <groupId>com.jayway.jsonpath</groupId>
    <artifactId>json-path</artifactId>
    </dependency>

    读取yml的内容:

    @Component
    @Data
    public class BlogProperties {

    @Value("${blog.name}")
    private String blogName;

    @Value("${blog.address}")
    private String blogAddress;

    }
    @Test
    public void getBlogProperties() {
    System.out.println(blogProperties.getBlogName()+" and "+blogProperties.getBlogAddress());
    }

    运行效果:

    实体类:

    @Entity
    @Data
    public class DeptInfo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
    * 部门ID,自动增长+1
    */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long deptId;
    /**
    * 部门名称
    */
    private String deptName;
    /**
    * 备注
    */
    private String remark;
    }

    dao层:

    @Repository
    public interface DeptRepository extends JpaRepository<DeptInfo, Long> {
    DeptInfo findByDeptId(Long id);

    DeptInfo findByDeptIdAndDeptName(Long id, String deptName);
    }

    测试:

    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class DeptRepositoryTests {
    @Autowired
    private DeptRepository deptRepository;

    @Autowired
    private BlogProperties blogProperties;

    @Test
    public void findByDeptId() {
    DeptInfo deptInfo = deptRepository.findByDeptId(1L);
    System.out.println(deptInfo);
    }

    @Test
    public void findByDeptIdAndDeptName() {
    DeptInfo deptInfo = deptRepository.findByDeptIdAndDeptName(1L,"研发部");
    System.out.println(deptInfo);
    }

    @Test
    public void saveDept() {
    DeptInfo deptInfo = new DeptInfo();
    deptInfo.setDeptName("行政");
    deptInfo.setRemark("jpa add");
    System.out.println(deptRepository.save(deptInfo));
    }

    @Test
    public void getBlogProperties() {
    System.out.println(blogProperties.getBlogName()+" and "+blogProperties.getBlogAddress());
    }
    }

    controller层:

    @RestController
    public class HelloWorldController {
    @RequestMapping("/getDept")
    public DeptInfo getDept() {
    DeptInfo deptInfo = new DeptInfo();
    deptInfo.setDeptId(1L);
    deptInfo.setDeptName("研发部");
    deptInfo.setRemark("测试专用");
    return deptInfo;
    }
    }

    运行效果:http://localhost:8080/getDept

    以上操作中,遇到的报错,解决办法如下:

    解决引入jpa报错

    Lombok使用

    com.mysql.jdbc.Driver与com.mysql.cj.jdbc.Driver的区别

     springBoot jpa主键策略

    参考原文:https://www.cnblogs.com/ityouknow/p/5730412.html (纯洁的微笑)

  • 相关阅读:
    Android桌面快捷方式的实现
    Java之currenHashMap
    windows 2003 配置邮件服务器 hMailServer+squirrelmail
    bat脚本 cmd 命令
    强制客户端更新Silverlight XAP文件方法汇总(转)
    Silverlight 常用StringFormat格式总结(转)
    Excel 操作 DLL
    silverlight 开发软件列表
    通用错误处理
    use XSD.exe in VS2010 from a xsd file to class
  • 原文地址:https://www.cnblogs.com/gaoqiao/p/10860927.html
Copyright © 2011-2022 走看看