笔者列举了常见的srping操作数据库的问题,列举了两种操作事务的方式(XML配置的方式和注解的方式)
希望能给学习spring框架或开发中的小伙伴一点点帮助。谢谢
一.spring与JDBC整合
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance"
;
xmlns:context
=
"
http://www.springframework.org/schema/context"
;
<!-- 配置
dao
层 -->
<
bean
name
=
"dao"
class
=
"com.briup.jdbc.UserDaoImplByJDBC"
>
<
property
name
=
"dateSource"
ref
=
"dataSource2"
></
property
>
</
bean
>
<!-- 第三种配置方式 -->
<!-- <context:property-placeholder location="jdbc.properties" />
<bean id="dataSource3"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>${jdbc.driverClassName}</value>
</property>
<property name="
url
">
<value>${jdbc.url}</value>
</property>
<property name="
username
">
<value>${jdbc.username}</value>
</property>
<property name="password">
<value>${jdbc.password}</value>
</property>
</bean> -->
<!-- 第二种配置方式 -->
<
context:property-placeholder
location
=
"jdbc.properties"
/>
<
bean
id
=
"dataSource2"
class
=
"org.apache.commons.dbcp.BasicDataSource"
>
<
property
name
=
"driverClassName"
value
=
"${jdbc.driverClassName}"
/>
<
property
name
=
"url"
value
=
"${jdbc.url}"
/>
<
property
name
=
"username"
value
=
"${jdbc.username}"
/>
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
<!-- 最大连接数 -->
<
property
name
=
"maxActive"
value
=
"80"
/>
<!-- 最大空闲连接数 -->
<
property
name
=
"maxIdle"
value
=
"20"
/>
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间 单位:毫秒
超过时间则抛出异常,如果设置为-1表示无限等待 -->
<
property
name
=
"maxWait"
value
=
"3000"
/>
</
bean
>
<!-- 使用第一种配置方式 -->
<
bean
name
=
"dataSource"
class
=
"oracle.jdbc.pool.OracleConnectionPoolDataSource"
>
<
property
name
=
"networkProtocol"
value
=
"tcp"
/>
<
property
name
=
"databaseName"
value
=
"XE"
/>
<
property
name
=
"driverType"
value
=
"thin"
/>
<
property
name
=
"portNumber"
value
=
"1521"
/>
<
property
name
=
"user"
value
=
"oracle"
/>
<
property
name
=
"serverName"
value
=
"127.0.0.1"
/>
<
property
name
=
"password"
value
=
"oracle"
/>
</
bean
>
</
beans
>
二.Spring与Mybatis整合(常用的方式)
mybatis-config.xml 配置
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<!
DOCTYPE
configuration
<
configuration
>
<
typeAliases
>
<
package
name
=
"com.briup.pojo"
/>
</
typeAliases
>
<
mappers
>
<
mapper
resource
=
"com/briup/mybatis/userMapper.xml"
/>
</
mappers
>
</
configuration
>
mapper映射文件配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<!-- com.briup.mappers.StudentMapper是我们定义接口的全限定名字 这样就可以使用接口调用映射的SQL语句了 这个名字一定要和接口对应上 -->
<
mapper
namespace
=
"com.briup.dao.IUserDao"
>
<
insert
id
=
"addUser"
parameterType
=
"user"
>
insert into a_user
values(#{id},#{name},#{age})
</
insert
>
<
select
id
=
"listUser"
resultType
=
"user"
>
select id,name,age from a_user
</
select
>
</
mapper
>
spring XML配置
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance"
;
xmlns:context
=
"
http://www.springframework.org/schema/context"
;
<!-- 配置dataSource数据源 -->
<!-- 第二种配置方式 -->
<
context:property-placeholder
location
=
"jdbc.properties"
/>
<
bean
id
=
"dataSource"
class
=
"org.apache.commons.dbcp.BasicDataSource"
>
<
property
name
=
"driverClassName"
value
=
"${jdbc.driverClassName}"
/>
<
property
name
=
"url"
value
=
"${jdbc.url}"
/>
<
property
name
=
"username"
value
=
"${jdbc.username}"
/>
<
property
name
=
"password"
value
=
"${jdbc.password}"
/>
<!-- 最大连接数 -->
<
property
name
=
"maxActive"
value
=
"80"
/>
<!-- 最大空闲连接数 -->
<
property
name
=
"maxIdle"
value
=
"20"
/>
<!-- 最大等待时间:当没有可用连接时,连接池等待连接被归还的最大时间 单位:毫秒
超过时间则抛出异常,如果设置为-1表示无限等待 -->
<
property
name
=
"maxWait"
value
=
"3000"
/>
</
bean
>
<!-- 配置factory -->
<
bean
id
=
"factory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
<!-- 指定
mybatis
-
config
总配置文件,订制的environment在spring容器中不在生效 -->
<
property
name
=
"configLocation"
value
=
"classpath:mybatis-config.xml"
/>
</
bean
>
<!-- 通过来扫描
mapper
接口所在的包名,当有多个包的时候,用半角逗号分隔即可
然后再次强调下,这种方式的配置,userMapper.xml必须和对应的接口放在同一个目录下面
-->
<
bean
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
<
property
name
=
"basePackage"
value
=
"com.briup.dao"
></
property
>
<
property
name
=
"sqlSessionFactory"
ref
=
"factory"
></
property
>
</
bean
>
</
beans
>