美文网首页
springboot整合多数据源(一)

springboot整合多数据源(一)

作者: 冷枫abc | 来源:发表于2019-07-23 09:24 被阅读0次
    首先整合springboot+jap(为例)

    新建工程springbootdata引入所需的pom依赖

    <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>1.4.2.RELEASE</version>
        </parent>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-data-jpa</artifactId>
            </dependency>
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
                <version>5.1.47</version>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
        </dependencies>
    

    java/resources目录下新建application.properties文件配置数据源

    spring.datasource.url=jdbc:mysql://localhost:3306/my-first
    spring.datasource.username=root
    spring.datasource.password=root
    spring.datasource.driver-class-name=com.mysql.jdbc.Driver
    

    然后新建对应的实体类entity

    package com.lwq.entity;
    
    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.GeneratedValue;
    import javax.persistence.Id;
    
    @Entity(name = "users")
    public class User {
        @Id
        @GeneratedValue
        private Integer id;
        @Column
        private String name;
        @Column
        private Integer age;
        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;
        }
        public Integer getAge() {
            return age;
        }
        public void setAge(Integer age) {
            this.age = age;
        }
        
    }
    

    新建dao

    package com.lwq.dao;
    
    import org.springframework.data.jpa.repository.JpaRepository;
    
    import com.lwq.entity.User;
    
    public interface UserDao extends JpaRepository<User, Integer> {
    }
    

    本文省略service
    创建启动类

    package com.lwq;
    
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
    import org.springframework.boot.autoconfigure.domain.EntityScan;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
    
    @ComponentScan(basePackages = { "com.lwq" })
    @EnableJpaRepositories(basePackages = "com.lwq.dao")
    @EnableAutoConfiguration
    @EntityScan(basePackages = "com.lwq.entity")
    public class App {
        public static void main(String[] args) {
            SpringApplication.run(App.class, args);
        }
    
    }
    
    

    创建访问类

    package com.lwq.controller;
    
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    import com.lwq.dao.UserDao;
    import com.lwq.entity.User;
    
    @RestController
    public class TestController {
    
        @Autowired
        private UserDao userDao;
        
        @RequestMapping("/index")
        public String index(Integer id) {
            User findUser = userDao.findOne(id);
            System.out.println(findUser.getName());
            return "success";
        } 
    }
    
    

    启动项目访问即刻
    注意:启动项目如果报错java.math.BigInteger cannot be cast to java.lang.Long问题,原因如下:
    在连接数据库进行查询数据时,大家可能会遇见这样一个问题:java.math.BigInteger cannot be cast to ``java.lang.Long,然后去检查代码中的类型为Long,接着检查数据表中字段的类型为bigint,按道理类型是匹``配的啊,那么问题到底出现在哪儿呢?

    如果出现以上问题,大家就应该注意了,是不是数据库版本与mysql-connector.jar包的版本不兼容造成的。本人用的是Mysql8.0.12免安装版本,出现问题时的jar包版本为5.1.26,更换为5.1.47后解决了该问题。希望能够帮助到遇到这类问题的朋友。
    到此,整合JPA完毕。

    相关文章

      网友评论

          本文标题:springboot整合多数据源(一)

          本文链接:https://www.haomeiwen.com/subject/evxglctx.html