springboot访问数据库(MySql)

1.使用JDBC访问数据库:JDBC是用于在Java语言编程中与数据库连接的API

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

2.数据源配置

dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.21</version>
</dependency>

3.配置数据库

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

4.新建接口

/**
 * user的service
 */
public interface IUserService {
    /**
     *
     * @param name
     * @param age
     */
    int create(String name,Integer age);

    /**
     * 根据用户名删除用户
     * @param name
     */
    void deleteByName(String name);

    /**
     * 获取用户总数
     * @return
     */
     Integer getUsersCount();

    /**
     * 删除所有用户
     */
    void deleteAllUsers();
}

5.实现接口

@Service
public class UserServiceImpl implements IUserService {
    @Autowired
    private JdbcTemplate jdbcTemplate;//Spring的JdbcTemplate是自动配置的,可直接使用
    @Override
    public int create(String name, Integer age) {
        int flag = jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
        return flag;
    }

    @Override
    public void deleteByName(String name) {
        jdbcTemplate.update("delete from USER where NAME = ?", name);
    }

    @Override
    public Integer getUsersCount() {
        return jdbcTemplate.queryForObject("select count(1) from USER", Integer.class);
    }

    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from USER");
    }
}

6.测试代码

 /**
     * 测试数据库连接
     */
    @Autowired
    private IUserService userSerivce;

    @Test
    public void testJdbc() throws Exception {
        // 插入5个用户
        int flag = userSerivce.create("a", 1);
        System.out.println(flag);
        userSerivce.create("b", 2);
        userSerivce.create("c", 3);
        userSerivce.create("d", 4);
        userSerivce.create("e", 5);

        // 查数据库,应该有5个用户
        Assert.assertEquals(5, userSerivce.getUsersCount().intValue());

        // 删除两个用户
        userSerivce.deleteByName("a");
        userSerivce.deleteByName("e");

        // 查数据库,应该有5个用户
        Assert.assertEquals(3, userSerivce.getUsersCount().intValue());

    }

7.多数据源配置

  (1) 创建一个Spring配置类,定义两个DataSource用来读取application.properties中的不同配置

/**
* 多数据源配置
*/

@Configuration
public class DataSourceConfig {

@Bean(name = "primaryDataSource")
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="spring.datasource")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}

@Bean(name = "secondaryDataSource")
@Qualifier("secondaryDataSource")
@Primary
@ConfigurationProperties(prefix="spring.datasource1")
public DataSource secondaryDataSource() {
return DataSourceBuilder.create().build();
}

//将多数据源注入JdbcTemplate
@Bean(name = "primaryJdbcTemplate")
public JdbcTemplate primaryJdbcTemplate(
@Qualifier("primaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

@Bean(name = "secondaryJdbcTemplate")
public JdbcTemplate secondaryJdbcTemplate(
@Qualifier("secondaryDataSource") DataSource dataSource) {
return new JdbcTemplate(dataSource);
}

}

(2)配置文件

#单数据源时spring.datasource.url是可以的,多数据源时要写成spring.datasource.jdbc-url
#网上这样说,在2.0升级之后需要变更成:spring.datasource.jdbc-url和spring.datasource.driver-class-name即可解决!
spring.datasource.jdbc-url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.datasource1.jdbc-url=jdbc:mysql://localhost:3306/test1
spring.datasource1.username=root
spring.datasource1.password=123456
spring.datasource1.driver-class-name=com.mysql.jdbc.Driver

(3)测试

/***
* 多数据源
*/
@Autowired
@Qualifier("primaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate1;

@Autowired
@Qualifier("secondaryJdbcTemplate")
protected JdbcTemplate jdbcTemplate2;

@Test
public void test3() throws Exception {

// 往第一个数据源中插入两条数据
jdbcTemplate1.update("insert into user(name,age) values( ?, ?)", "aaa", 20);
jdbcTemplate1.update("insert into user(name,age) values( ?, ?)", "bbb", 30);

// 往第二个数据源中插入一条数据,若插入的是第一个数据源,则会主键冲突报错
jdbcTemplate2.update("insert into user(id,name,age) values(?,?, ?)", 5,"aaa", 20);
}v
原文地址:https://www.cnblogs.com/yxqing/p/10613641.html