springboot 数据库假面_springboot访问数据库(MySql)

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

org.springframework.boot

spring-boot-starter-jdbc

2.数据源配置

dependency>

mysql

mysql-connector-java

5.1.21

3.配置数据库

spring.datasource.url=jdbc:mysql://localhost:3306/test

spring.datasource.username=root

spring.datasource.password=123456spring.datasource.driver-class-name=com.mysql.jdbc.Driver

4.新建接口

/*** user的service*/

public interfaceIUserService {/***

*@paramname

*@paramage*/

intcreate(String name,Integer age);/*** 根据用户名删除用户

*@paramname*/

voiddeleteByName(String name);/*** 获取用户总数

*@return

*/Integer getUsersCount();/*** 删除所有用户*/

voiddeleteAllUsers();

}

5.实现接口

@Servicepublic class UserServiceImpl implementsIUserService {

@Autowiredprivate JdbcTemplate jdbcTemplate;//Spring的JdbcTemplate是自动配置的,可直接使用

@Overridepublic intcreate(String name, Integer age) {int flag = jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);returnflag;

}

@Overridepublic voiddeleteByName(String name) {

jdbcTemplate.update("delete from USER where NAME = ?", name);

}

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

}

@Overridepublic voiddeleteAllUsers() {

jdbcTemplate.update("delete from USER");

}

}

6.测试代码

/*** 测试数据库连接*/@AutowiredprivateIUserService userSerivce;

@Testpublic void testJdbc() throwsException {//插入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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值