该用户从未签到
|
spring JDBC9 J) S$ p" \, j
1、管理数据源
% L' C, J, K! L6 |9 U 2、连接关闭- g- T: I" r' I5 z/ j$ D
3、简化数据访问层操作 jdbcTemplate
0 y$ P, h0 W' F* R. n+ ~1 y% u0 R* J2 I) `# U
Spring JDBC开发步骤
' f% l/ p1 }# X3 l1、在spring配置文件中配置数据源
% j# [2 V; ?/ G3 m: q6 d <bean id="dataSource"
& l( V( j _+ [ class="org.springframework.jdbc.datasource.DriverManagerDataSource" destroy-method="close">/ _& Q9 W. K, p8 |% A" o6 k
<property name="driverClassName" value="com.mysql.jdbc.Driver" />0 ?8 f, Q, x* X0 i+ B5 E9 C
<property name="url" value="jdbc:mysql://127.0.0.1:3306/mydb" />1 e4 J5 o# x) c% ?9 p
<property name="username" value="root" />
8 |4 m- b2 r2 y1 U0 R8 b9 @ y <property name="password" value="1234" />
- G+ a% Q" D0 [& Q' z </bean>
# }; Q2 u" N q/ S4 z H2、配置JDBC模板类
h% N! |: |( i, [$ r6 M" i( y <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">$ P- T1 t1 w( F9 l6 m& ]% V
<property name="dataSource" ref="dataSource"/>
$ y, m" l$ e$ O! O: M4 Y9 e$ { </bean>
8 o2 B- M& g( M3、spring容器管理数据访问层
; F5 b3 O7 G2 C# ~3 C <bean id="userDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">
6 D y9 m8 _1 U$ j" z" f# c! H <property name="jdbcTemplate" ref="jdbcTemplate"/>$ S9 B$ W( L% H2 x% N4 ^4 D+ h
</bean>; A- c4 ?3 U0 @/ l
4、在数据访问层注入JdbcTemplate(set)
& J9 _+ l, A) P. m- A5、使用JdbcTemplate完成插入( s; P. _% ]5 ?" X9 h% S
6、sql语句让spring容器管理
1 q7 L' x0 B& `/ j2 Z <bean id="userDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">4 Z" z9 J; C4 m
<property name="jdbcTemplate" ref="jdbcTemplate"/>$ g% g9 H+ S- Q/ Z; n J
<property name="sql" value="insert into t_user(username,password) values (?,?)"/>
! E5 ^+ Z. z/ @# B </bean>
/ o6 {& C8 G6 ^$ C3 R, Z4 S7 b1 T( l% Z3 p9 k8 r @' f
|
|