⑴ springmvc連接資料庫時報錯,求解決方法
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
是driverClassName,不是driver
⑵ SpringMVC 連接 Oracle資料庫 增刪改查
缺少了spring-web-3.2.6.jar包
⑶ 請教在Spring MVC中,如何實現啟動時從資料庫讀取信息
讀db數據的對象,只要被Spring載入了,都會讀到ApplicationContext(應用環境)中,
然後任何控制器Controller可以實現ApplicationContextAware(打理應用環境)介面,可以自動載入ApplicationContext,也就可以在控制器的方法中操作其中任一數據對象。
比如某個spring-beans.xml中寫上
<bean id="myService" class="com.mypackage.MyDatabaseService" />
然後在控制器中
java">@Controller
{
;
@(ApplicationContextac){
service=(MyDatabaseService)ac.getBean("myService");
}
}
這樣就自動載入了
⑷ spring mvc 鎬庝箞閰嶇疆澶氫釜搴
銆銆鍦烘櫙鎻忚堪錛氭湁澶氫釜鏁版嵁搴擄紝姣忎釜鏁版嵁搴撳彲鑳藉垎甯冨湪涓嶅悓鐨刴ysql instance涓婇潰錛屾湁澶氫釜瀛樺偍榪囩▼錛屾瘡涓瀛樺偍榪囩▼鍙鑳藉垎甯冨湪涓嶅悓鐨勬暟鎹搴撲腑錛岄渶瑕佹湁涓や釜閰嶇疆鏂囦歡銆
銆銆1. mysql 鏁版嵁搴撴槧灝勶細
銆銆A.driverClassName=com.mysql.jdbc.Driver
A.url=jdbc:mysql://172.20.7.51:3308/blog
A.username=trappuser
A.password=Opera1!
B.driverClassName=com.mysql.jdbc.Driver
B.url=jdbc:mysql://localhost:3306/wedding
B.username=root
B.password=opera
銆銆涓婇潰瀹氫箟鐨凙銆丅涓轟袱涓猰ysql instance鐨勭緝鍐欍
銆銆2. 瀛樺偍榪囩▼涓巑ysql instance鐨勬槧灝勫叧緋伙細
銆銆SP_Get_User=A
GetStocks=B
銆銆瀹氫箟涓や釜妯℃嫙瀛樺偍榪囩▼錛岀涓涓鏁版嵁搴撯淪P_Get_User鈥滄槸鍦ㄦ暟鎹搴揂涓嬮潰錛岀鑴婄楁暎浜屼釜鏁版嵁搴撯滸etStocks鈥滄槸鍦ㄦ暟鎹搴揃涓嬮潰銆
3. 寤虹珛鑷瀹氫箟鐨剆essionFactory
銆銆3.1 xml閰嶇疆鐨刣atasource鍙妔essionFactory濡備笅錛
銆銆<bean class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" id="dataSource">
<property name="driverClassName" value="${database.driverClassName}"/>
<property name="url" value="${database.url}"/>
<property name="username" value="${database.username}"/>
<property name="password" value="${database.password}"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>妯辨皬
<鏃忓唽property name="packagesToScan" value="com.xx.assetcommander">
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
</bean>
銆銆姝ゅ勬垜浠瀹氫箟鐨剆essionFactory鐨勭被鍨嬩負LocalSessionFactoryBean錛屽畠鏄涓涓宸ュ巶瀵硅薄錛屼笌鎴戜滑鍐嶉渶瑕佺殑 SessionFactory涓嶆槸涓鍥炰簨錛屾垜浠闇瑕佺殑sessionfactory鏄痮rg.hibernate.SessionFactory錛岃繖涓瀵硅薄鍙浠ヨ絎涓涓猻essionFactory鐨刧etObject()鏂規硶鐢熸垚銆
銆銆3.2 鐢變簬鎴戜滑榪炴帴鐨勬槸澶氫釜mysql instance錛 涓嶆柟渚垮湪xml涓閰嶇疆澶氫釜datasource鍜屽氫釜sessionFactory錛屾晠鍙浠ラ氳繃綰痡ava鐨勫艦寮忓紑鍙戱紝鍙浠ヤ嬌鐢╩ap鏉ュ瓨鍌ㄥ瓨鍌ㄨ繃紼嬩笌mysql database鐨勫叧緋伙紝灝嗗瓨鍌ㄨ繃紼嬬殑鍚嶅瓧鍜屾暟鎹搴撳緩璁鍏崇郴錛岃繖鏍烽氳繃瀛樺偍榪囩▼鐨勫悕縐板氨鑳藉緱鍒版暟鎹搴撶殑緙╁啓鍚嶏紝閫氳繃鏁版嵁搴撶殑緙╁啓鍚嶈兘澶熸壘鍒板瑰簲鐨刴ysql instance錛屼嬌鐢ㄧ函java寮鍙戠殑榪囩▼綾諱技浜巟ml閰嶇疆錛屽備笅錛
銆銆ds.setDriverClassName(getDriver());
ds.setUrl(getUrl());
ds.setUsername(getUsername());
ds.setPassword(getPassword());
LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
sessionFactory.setDataSource(ds);
sessionFactory.setPackagesToScan("com.xx.assetcommander");
Properties params = new Properties();
params.setProperty("hibernate.dialect",
"org.hibernate.dialect.MySQLDialect");
params.setProperty("hibernate.show_sql", "true");
sessionFactory.setHibernateProperties(params);
銆銆褰撴垜浠鑾峰緱鍙浠ヤ嬌鐢ㄧ殑LocalSessionFactoryBean鏃跺欙紝鍦ㄨ皟鐢╣etObject()鑾峰緱SessionFactory涔嬪墠錛屽繀欏昏佽皟鐢╝fterPropertiesSet()鏂規硶錛屽惁鍒欏緱鍒扮殑sessionFactory涓虹┖銆
銆銆public Session getDsBySp(String spName) throws IOException {
//get the corresponding mysql database shortname by sp name
String dbName = getDbForSP(str);
//get the corresponding mysql instance connection by mysql database shortname
LocalSessionFactoryBean fB = getDsByDb(dbName);
// don't forget this line or null will be returned when you call getObject() method.
fB.afterPropertiesSet();
return fB.getObject().openSession();
}
銆銆娉錛氬湪tomcat鍚鍔ㄦ椂錛屽傛灉娌℃湁閰嶇疆浠諱綍datasource錛屼細鍑虹幇濡備笅閿欒錛
銆銆org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [javax.persistence.EntityManagerFactory] is defined
鏁呴渶瑕侀厤緗榛樿ょ殑datasource.
榪欑嶆柟寮忛渶瑕佸仛鍒頒笉鍚岀殑鏁版嵁搴搃nstance鐩存帴涓氬姟鐨勫畬鍏ㄧ嫭絝嬶紝涓嶅彲浠ュ嚭鐜拌法鏁版嵁搴撶殑琛╦oin錛屽惁鍒欏勭悊闅懼害浼氬炲姞銆
榪樻湁灝辨槸瀵逛簬澶氭暟鎹搴撶洿鎺ョ殑浜嬪姟綆$悊濡備綍鍘誨勭悊錛
⑸ spring涓涓轟粈涔堟槸閫氳繃榪炴帴鑾峰彇鎿嶄綔鏁版嵁搴撶殑瀵硅薄(springmvc榪炴帴鏁版嵁搴)
浣犵殑搴旇ユ槸Spring鑾峰緱鏁版嵁搴撹繛鎺ュ硅薄鐨勭被銆
(SpringFramework5.0.4.RELEASEAPI)
鍦↗ava鏁版嵁搴撴搷浣滅殑妗嗘灦涓棰傞晣錛屼笉璁烘槸Spring錛孲truts錛屾垨鑰呬綘鑷宸卞啓鐨勮繛鎺ユ帶鍒朵篃涓嶇′綘鏄鍚﹂泦鎴愯繛鎺ユ睜錛屾瘮濡傝碈3P0銆
涓鑸鏉ヨ達紝閮芥槸鍏堣佽幏寰楁暟鎹搴撹倖浜鐨勯摼鎺ャ傝傛ū瀹
鍦ㄨ繖閲屼綘鐨勮繛鎺ュ湪Connection閲岄潰銆
鑾峰緱榪炴帴鍚庢墠鑳藉規暟鎹搴撹繘琛屾搷浣溿