• Spring Boot访问mysql(JPA方式)最简单配置


    0.先推荐一个工具——lombok,pom文件如下:

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <scope>compile</scope>
    </dependency>

    可以使用注解@Data 编译时自动生成get,set方法,构造函数,toString方法。

    @Data
    @Entity
    public class Account
    {
        @Id
        private String id;
        private String account;
        @Column(name = "call_phone")
        private String phone;
        @Column(name = "nick_name")
        private String nickname;
        private String password;
        private String salt;
        private int userType;
        private String createUser;
        private Timestamp createTime;
        private int state;
    }

    生成后的效果如下:

    image

    1.pom.xml文件下添加如下依赖,引入spring-boot-jpa的jar包,以及mysql驱动

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
    </dependency>

    2.在/src/main/resources/application.properties中配置spring datasource及hibernate方言

    (Spring boot 默认使用hibernate作为JPA的实现)

    spring.datasource.url=jdbc:mysql://localhost:3306/test?useSSL=false
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.tomcat.max-active=100
    spring.datasource.tomcat.max-idle=200
    spring.datasource.tomcat.initialSize=20
    spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect

    3.定义Entity和Repository(接口)。

    image

    Entity--Account实体类如下:

      @Entity会被spring扫描并加载,

      @Id注解在主键上

      @Column name="call_phone" 指该字段对应的数据库的字段名,如果相同就不需要定义。数据库下划线间隔和代码中的驼峰法视为相同,如数据库字段create_time等价于Java类中的createTime,因此不需要用@Column注解。

    @Data
    @Entity
    public class Account
    {
        @Id
        private String id;
        private String account;
        @Column(name = "call_phone")
        private String phone;
        @Column(name = "nick_name")
        private String nickname;
        private String password;
        private String salt;
        private int userType;
        private String createUser;
        private Timestamp createTime;
        private int state;
    }

    Repository如下:

    @Repository
    public interface AccountRepository extends JpaRepository<Account, String>
    {
        Account findOneByAccount(String account);
    }

    4.在其他@Component中调用

    @RestController
    @RequestMapping("/subsystem")
    public class SubsystemAuthorityService
    {
        @Autowired
        AccountRepository accountRepository;
    
        @PostMapping(path = "/admin/info")
        public String getAdminInfo(String currentAccount)
        {
            Account account = accountRepository.findOneByAccount(currentAccount);
            System.out.println(account);
            return account.toString();
        }
    }
  • 相关阅读:
    4.9版本的linux内核中实时时钟芯片pt7c4338的驱动源码在哪里
    4.9版本的linux内核中eeprom存储芯片at24c512的驱动源码在哪里
    4.9版本的linux内核中温度传感器adt7461的驱动源码在哪里
    4.9版本linux内核的ina220电流检测芯片源码在哪里
    nxp的layerscape系列芯片中的rcw指定了一些什么信息
    openwrt的编译系统是如何制作根文件系统的
    openwrt的编译系统在哪里对程序进行开机自动启动配置
    makefile中的word函数作用是什么
    jquery 动态增加table行,动态删除table行
    使用jQuery创建可删除添加行的动态表格,超级简单实用的方法
  • 原文地址:https://www.cnblogs.com/arli/p/6914057.html
Copyright © 2020-2023  润新知