SpringJDBCフレームワークについて簡単に説明します
1. JDBCフレームワークの概要
JDBCの操作プロセスはより面倒であり、ほとんどの操作ステップは比較的固定されているため、多くのフレームワークがそれをカプセル化して最適化しました。よく知られているSpringフレームワークもJDBCを単純にカプセル化します。
Springフレームワークは、JDBCの開発を簡素化するためのJdbcTemplateクラスを提供します。その特徴は、シンプルで便利です。このテクノロジーは、一般にSpringJDBCテクノロジーとして知られています。
Spring JDBCは、Springフレームワークが永続層のJDBC操作をカプセル化して最適化し、面倒な作業を単純で実用的なものに変えること、つまり永続層の技術的な問題を解決することを意味すると理解されています。
通常のJDBCデータベースを使用する場合、例外を処理したり、データベース接続を開いたり閉じたりするための不要なコードを書くのは面倒です。ただし、Spring JDBCフレームワークは、最初に接続を開き、SQLステートメントを準備して実行し、例外を処理し、トランザクションを処理し、最後に接続を閉じるまで、すべての低レベルの詳細を担当します。
したがって、データベースからデータを取得するときは、接続パラメーターを定義し、実行するSQLステートメントを指定して、各反復に必要な作業を完了します。
2. SpringJDBCの例
jdbcテンプレートクラスを使用したSpringJDBCフレームワークの関連概念を理解したいので、簡単なクエリ操作を実装するための簡単な例を書いてみましょう。
これが私のプロジェクト構造です
(1)データベーステーブルを作成します。私のテーブルデータは次のとおりです。
(2)エンティティクラスを作成します。
package cn.tb.entity;
public class City {
private int cid;
private String cname;
private int pid;
public int getCid() {
return cid;
}
public void setCid(int cid) {
this.cid = cid;
}
public String getCname() {
return cname;
}
public void setCname(String cname) {
this.cname = cname;
}
public int getPid() {
return pid;
}
public void setPid(int pid) {
this.pid = pid;
}
public City(int cid, String cname, int pid) {
this.cid = cid;
this.cname = cname;
this.pid = pid;
}
public City() {
}
}
(3)インターフェースを作成する
package cn.tb.dao;
import cn.tb.entity.City;
import java.util.List;
public interface CityDao {
public List<City> findCnameAll(); //查询所有城市
(4)インターフェース実装クラスはJdbcTemplateクラスとJdbcTemplateクラスを使用します。Springはデータベースの操作をjdbcに詳細にカプセル化します
。Spring
インジェクション関数を使用すると、DataSourceをJdbcTemplateに登録できます。
JdbcTemplateは、主に次の5種類のメソッドを提供します。executeメソッド:
一般にDDLステートメントの実行に使用される任意のSQLステートメントの実行に使用できます
。updateメソッドとbatchUpdateメソッド:new、変更、および削除されたステートメントの実行に使用されます。batchUpdateメソッドはバッチ関連ステートメントの実行に使用され
ます。queryメソッドとqueryForXXXメソッド:クエリ関連ステートメントの実行に使用されます。callメソッド:
ストアドプロシージャと関数関連ステートメントの実行に使用されます。
package cn.tb.dao.impl;
import cn.tb.dao.CityDao;
import cn.tb.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.lang.Nullable;
import org.springframework.stereotype.Repository;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
@Repository
public class CityImpl implements CityDao{
//JdbcTemplate 类
@Autowired
private JdbcTemplate jdbcTemplate;
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
@Override
public List<City> findCnameAll() {
return jdbcTemplate.query("select * from city",new RowMapper(){
@Nullable
@Override
public City mapRow(ResultSet rs, int i) throws SQLException {
City city = new City();
city.setCid(rs.getInt(1));
city.setCname(rs.getString(2));
city.setPid(rs.getInt(3));
return city;
}
});
}
(5)サービス
package cn.tb.service;
import cn.tb.dao.CityDao;
import cn.tb.entity.City;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class CityService {
@Autowired
private CityDao cityDao;
public CityDao getCityDao() {
return cityDao;
}
public void setCityDao(CityDao cityDao) {
this.cityDao = cityDao;
}
public List<City> findCnameAll(){
return cityDao.findCnameAll();
}
public void update(City cid){
cityDao.update(cid);
}
}
(6)XMLファイルを構成します(これも最も重要なステップです)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd
">
<!--自动扫描上下文包 自动扫描cn.tb下的所有包-->
<context:component-scan base-package="cn.tb.*" />
<!--配置数据源-->
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<!--加载mysql驱动-->
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<!--获取数据库连接 jqueryajaxdemo 数据库名-->
<property name="url" value="jdbc:mysql://localhost:3306/jqueryajaxdemo?serverTimezone=UTC" />
<!--获取数据库账号-->
<property name="username" value="root" />
<!--获取数据库密码-->
<property name="password" value="root" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource" />
</bean>
<!--事务管理-->
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<tx:advice id="txadvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="find*" read-only="true"/>
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="*" />
</tx:attributes>
</tx:advice>
<aop:config>
<aop:pointcut id="services" expression="execution(* cn.tb.dao.impl.*.*(..))" />
<aop:advisor advice-ref="txadvice" pointcut-ref="services" />
</aop:config>
</beans>
(7)テスト
package cn.tb.test;
import cn.tb.entity.City;
import cn.tb.service.CityService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.List;
public class Test {
public static void main(String[] args) {
ApplicationContext alc = new ClassPathXmlApplicationContext("Spring.xml");
CityService ct = alc.getBean("cityService", CityService.class);
List<City> list = ct.findCnameAll();
for (City c : list) {
System.out.println(c.getCname());
}
}
}
(8)試験結果