笔者列举了常见的srping操作数据库的问题,列举了两种操作事务的方式(XML配置的方式和注解的方式)
希望能给学习spring框架或开发中的小伙伴一点点帮助。谢谢
一.spring与JDBC整合
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"
;
xsi:schemaLocation
=
" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
;
>
<
bean
name
=
"dao"
class
=
"com.briup.jdbc.UserDaoImplByJDBC"
>
<
property
name
=
"dateSource"
ref
=
"dataSource2"
>
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"
/>
<
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
PUBLIC
"-// mybatis.org//DTD
Config 3.0//EN"
" http://mybatis.org/dtd/mybatis-3-config.dtd"
;
>
<
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"
?>
DOCTYPE
mapper
PUBLIC
"-// mybatis.org//DTD
Mapper 3.0//EN"
" http://mybatis.org/dtd/mybatis-3-mapper.dtd"
;
>
<
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"
?>
<
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"
;
xsi:schemaLocation
=
" http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd"
;
>
<
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"
/>
<
property
name
=
"maxWait"
value
=
"3000"
/>
bean
>
<
bean
id
=
"factory"
class
=
"org.mybatis.spring.SqlSessionFactoryBean"
>
<
property
name
=
"dataSource"
ref
=
"dataSource"
/>
<
property
name
=
"configLocation"
value
=
"classpath:mybatis-config.xml"
/>
bean
>
<
bean
class
=
"org.mybatis.spring.mapper.MapperScannerConfigurer"
>
<
property
name
=
"basePackage"
value
=
"com.briup.dao"
>
property
>
<
property
name
=
"sqlSessionFactory"
ref
=
"factory"
>
property
>
bean
>
beans
>