该用户从未签到
|
spring JDBC' D+ i, W c& l( I9 C
1、管理数据源
6 d' M" y* Z% P0 M4 K0 C# a; o 2、连接关闭
; H. Y! L: Z, S6 v. ] 3、简化数据访问层操作 jdbcTemplate
' t6 ]6 X" I/ S/ R& E4 g
& g' K4 P$ r6 o$ zSpring JDBC开发步骤
6 w9 l& Y' I# J, E: c* d1、在spring配置文件中配置数据源
* y6 O; s6 w8 g/ O& i% [0 a: l <bean id="dataSource"
! W! f8 x* \6 n A: a1 | class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close"># d4 @- h3 N3 d: x# y- d
<property name="driverClassName" value="com.mysql.jdbc.Driver" />2 r* ?/ z% s9 n% ]2 G9 q v3 C' o
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb" />. ?% H6 [, G0 \3 R. m7 i
<property name="username" value="root" />
1 s, w( _& Z2 \9 g- H <property name="password" value="1234" />
, I0 Q: E/ y0 w </bean>+ ]% ? ^! q3 t n
2、配置JDBC模板类0 M9 r0 g9 L9 J+ H# x. n2 S
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
/ Z, K6 U* c% l$ j9 t- ?! y <property name="dataSource" ref="dataSource"/>
V2 i* H, y/ K: R, i: N2 I# l3 } </bean>
/ r, B q" f2 {1 [0 c3、spring容器管理数据访问层) n' C$ t0 f6 C U# |4 _
<bean id="userDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">
1 p. _5 V, W% r# N) k! r/ { <property name="jdbcTemplate" ref="jdbcTemplate"/>
. r' b% |+ m8 ]% [% Y! z </bean>' y" E* t/ m- J U$ i7 a
4、在数据访问层注入JdbcTemplate(set)+ ^$ S' h1 G( N/ {/ m. V7 `9 e/ p
5、使用JdbcTemplate完成插入7 r* C4 i6 F* l% `/ ]
6、sql语句让spring容器管理8 b0 Y+ x3 F# M) b
<bean id="userDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">; G9 g- S- ~% B+ a9 W
<property name="jdbcTemplate" ref="jdbcTemplate"/>
4 ]+ `- e0 l" j0 D5 H. m- c" L8 E" E" S <property name="sql" value="insert into t_user(username,password) values (?,?)"/>
; }9 C1 A& x. w9 F/ x7 [ </bean>2 `, }3 x5 l" ~
1 L" o/ _$ P/ Y( s; X2 r% O |
|