宅男在线永久免费观看网直播,亚洲欧洲日产国码无码久久99,野花社区在线观看视频,亚洲人交乣女bbw,一本一本久久a久久精品综合不卡

全部
常見問題
產品動態(tài)
精選推薦

Spring Boot中使用JdbcTemplate訪問數(shù)據庫

管理 管理 編輯 刪除

數(shù)據源配置

在我們訪問數(shù)據庫的時候,需要先配置一個數(shù)據源,下面分別介紹一下幾種不同的數(shù)據庫配置方式。

首先,為了連接數(shù)據庫需要引入jdbc支持,在pom.xml中引入如下配置:

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

#嵌入式數(shù)據庫支持

嵌入式數(shù)據庫通常用于開發(fā)和測試環(huán)境,不推薦用于生產環(huán)境。Spring Boot提供自動配置的嵌入式數(shù)據庫有H2、HSQL、Derby,你不需要提供任何連接配置就能使用。

比如,我們可以在pom.xml中引入如下配置使用HSQL

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>runtime</scope>
</dependency>

#連接生產數(shù)據源

以MySQL數(shù)據庫為例,先引入MySQL連接的依賴包,在pom.xml中加入:

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

src/main/resources/application.properties中配置數(shù)據源信息

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

#連接JNDI數(shù)據源

當你將應用部署于應用服務器上的時候想讓數(shù)據源由應用服務器管理,那么可以使用如下配置方式引入JNDI數(shù)據源。

spring.datasource.jndi-name=java:jboss/datasources/customers

#使用JdbcTemplate操作數(shù)據庫

Spring的JdbcTemplate是自動配置的,你可以直接使用@Autowired來注入到你自己的bean中來使用。

舉例:我們在創(chuàng)建User表,包含屬性nameage,下面來編寫數(shù)據訪問對象和單元測試用例。

  • 定義包含有插入、刪除、查詢的抽象接口UserService
public interface UserService {

    /**
     * 新增一個用戶
     * @param name
     * @param age
     */
    void create(String name, Integer age);

    /**
     * 根據name刪除一個用戶高
     * @param name
     */
    void deleteByName(String name);

    /**
     * 獲取用戶總量
     */
    Integer getAllUsers();

    /**
     * 刪除所有用戶
     */
    void deleteAllUsers();

}
  • 通過JdbcTemplate實現(xiàn)UserService中定義的數(shù)據訪問操作
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void create(String name, Integer age) {
        jdbcTemplate.update("insert into USER(NAME, AGE) values(?, ?)", name, age);
    }

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

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

    @Override
    public void deleteAllUsers() {
        jdbcTemplate.update("delete from USER");
    }
}
  • 創(chuàng)建對UserService的單元測試用例,通過創(chuàng)建、刪除和查詢來驗證數(shù)據庫操作的正確性。

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(Application.class)
public class ApplicationTests {

	@Autowired
	private UserService userSerivce;

	@Before
	public void setUp() {
		// 準備,清空user表
		userSerivce.deleteAllUsers();
	}

	@Test
	public void test() throws Exception {
		// 插入5個用戶
		userSerivce.create("a", 1);
		userSerivce.create("b", 2);
		userSerivce.create("c", 3);
		userSerivce.create("d", 4);
		userSerivce.create("e", 5);

		// 查數(shù)據庫,應該有5個用戶
		Assert.assertEquals(5, userSerivce.getAllUsers().intValue());

		// 刪除兩個用戶
		userSerivce.deleteByName("a");
		userSerivce.deleteByName("e");

		// 查數(shù)據庫,應該有5個用戶
		Assert.assertEquals(3, userSerivce.getAllUsers().intValue());

	}

}

上面介紹的JdbcTemplate只是最基本的幾個操作,更多其他數(shù)據訪問操作的使用請參考:JdbcTemplate API


請登錄后查看

CRMEB 最后編輯于2025-01-16 10:28:49

快捷回復
回復
回復
回復({{post_count}}) {{!is_user ? '我的回復' :'全部回復'}}
排序 默認正序 回復倒序 點贊倒序

{{item.user_info.nickname ? item.user_info.nickname : item.user_name}} LV.{{ item.user_info.bbs_level || item.bbs_level }}

作者 管理員 企業(yè)

{{item.floor}}# 同步到gitee 已同步到gitee {{item.is_suggest == 1? '取消推薦': '推薦'}}
{{item.is_suggest == 1? '取消推薦': '推薦'}}
沙發(fā) 板凳 地板 {{item.floor}}#
{{item.user_info.title || '暫無簡介'}}
附件

{{itemf.name}}

{{item.created_at}}  {{item.ip_address}}
打賞
已打賞¥{{item.reward_price}}
{{item.like_count}}
{{item.showReply ? '取消回復' : '回復'}}
刪除
回復
回復

{{itemc.user_info.nickname}}

{{itemc.user_name}}

回復 {{itemc.comment_user_info.nickname}}

附件

{{itemf.name}}

{{itemc.created_at}}
打賞
已打賞¥{{itemc.reward_price}}
{{itemc.like_count}}
{{itemc.showReply ? '取消回復' : '回復'}}
刪除
回復
回復
查看更多
打賞
已打賞¥{{reward_price}}
1350
{{like_count}}
{{collect_count}}
添加回復 ({{post_count}})

相關推薦

快速安全登錄

使用微信掃碼登錄
{{item.label}} 加精
{{item.label}} {{item.label}} 板塊推薦 常見問題 產品動態(tài) 精選推薦 首頁頭條 首頁動態(tài) 首頁推薦
取 消 確 定
回復
回復
問題:
問題自動獲取的帖子內容,不準確時需要手動修改. [獲取答案]
答案:
提交
bug 需求 取 消 確 定
打賞金額
當前余額:¥{{rewardUserInfo.reward_price}}
{{item.price}}元
請輸入 0.1-{{reward_max_price}} 范圍內的數(shù)值
打賞成功
¥{{price}}
完成 確認打賞

微信登錄/注冊

切換手機號登錄

{{ bind_phone ? '綁定手機' : '手機登錄'}}

{{codeText}}
切換微信登錄/注冊
暫不綁定
CRMEB客服

CRMEB咨詢熱線 咨詢熱線

400-8888-794

微信掃碼咨詢

CRMEB開源商城下載 源碼下載 CRMEB幫助文檔 幫助文檔
返回頂部 返回頂部
CRMEB客服