JdbcTemplateの基本的な知識
1)JdbcTemplateを初めて見る
JdbcTemplateは、面倒なネイティブJdbcAPIオブジェクトのカプセル化です。
Springフレームワークは、多くの操作テンプレートクラスを提供します。例:リレーショナルデータを操作するためのJdbcTemplateとHibernateTemplate、nosqlデータベースを操作するためのRedisTemplate、メッセージキューを操作するためのJmsTemplate。
2)JdbcTemplateの簡単な開発手順
最初にDataSourceデータソースオブジェクトを構成し、次にそれをJdbcTemplateテンプレートオブジェクトに挿入してから、テンプレートオブジェクトを使用してデータベースに対して多くの操作を実行します。
3)JdbcTemplateはMaven座標をインポートする必要があります
<!-- 下面的Maven坐标可以参考一下 >_< -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.16</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
JdbcTemplate開発(Springを使用しない)
①使用して4つのパラメーター設定するためのデータソースデータソースオブジェクト
②使用するように構成されたデータソースデータソースオブジェクトJdbcTemplateテンプレートオブジェクトを構成する
③それを使用することができます
public class JdbcTemplateTest {
@Test
public void test() throws PropertyVetoException {
// 创建DataSource数据源对象
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/loliDB");
dataSource.setUser("root");
dataSource.setPassword("123456");
// 创建JdbcTemplate模板对象
JdbcTemplate template = new JdbcTemplate();
template.setDataSource(dataSource);
// 使用
int row = template.update("insert into account values(?, ?)", "Hana", 10);
System.out.println(row);
}
}
JdbcTemplate開発(Springを使用)
データソースの構成であろうとテンプレートの構成であろうと、コードはsetXxxメソッドを使用していることがわかりました-依存性注入についてすぐに考えてください
jdbc.properties ---------------------------------------------------------------------------
jdbc.driver=com.mysql.cj.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/loliDB
jdbc.username=root
jdbc.password=123456
applicationContext.xml --------------------------------------------------------------------
<!-- 从外部加载properties资源文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- DataSource数据源对象 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driver}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- JdbcTemplate模板对象 -->
<bean id="template" class="org.springframework.jdbc.core.JdbcTemplate" >
<property name="dataSource" ref="dataSource" />
</bean>
test.JdbctemplateTest ---------------------------------------------------------------------
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbctemplateTest {
@Autowired
private JdbcTemplate template;
@Test
public void test() {
int row = template.update("insert into account values(?, ?)", "Hana", 10);
System.out.println(row);
}
}
JdbcTemplateの使用
1)更新-追加、削除、変更に使用してください!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate template;
@Test
public void test1() {
template.update("insert into lolihouse values(?, ?)", "Hana", 8);
}
@Test
public void test2() {
template.update("delete from lolihouse where name = ?", "Hana");
}
@Test
public void test3() {
template.update("update lolihouse set age = ? where name = ?", 11, "Alice");
}
}
2)クエリ-あらゆる種類のクエリ!
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {
@Autowired
private JdbcTemplate template;
@Test
// 查询结果为出对象列表
public void test4() {
List<Loli> loliList = template.query("select * from lolihouse", new BeanPropertyRowMapper<Loli>(Loli.class));
System.out.println(loliList);
}
@Test
// 查询结果为一个对象
public void test5() {
Loli loli = template.queryForObject("select * from lolihouse where name = ?", new BeanPropertyRowMapper<Loli>(Loli.class), "Alice");
System.out.println(loli);
}
@Test
// 查询结果为统计个数
public void test6() {
Integer count = template.queryForObject("select count(*) from lolihouse", Integer.class);
System.out.println(count);
}
}