[关闭]
@MrXiao 2017-06-17T13:25:39.000000Z 字数 6119 阅读 854

spring(4)——持久层封装

未分类


1. 持久层访问方式

数据访问方式

2. JDBC

3. JDBCTemplete

3.1 原始jdbcTemplete

  1. DriverManagerDataSource dataSource = new DriverManagerDataSource();
  2. dataSource.setDriverClassName("com.mysql.jdbc.Driver");
  3. dataSource.setUrl("jdbc:mysql://localhost:3306/springdb");
  4. dataSource.setUsername("root");
  5. dataSource.setPassword("***********");
  6. JdbcTemplate template = new JdbcTemplate(dataSource);
  7. template.execute("insert into student values(1,'张三',23)");

3.2 Spring开发jdbcTemplete

  1. 设置Bean

    1. <bean id="dao" class="com.topvision.daotest.Dao">
    2. <property name="jdbcTemplate" ref="jdbcTemplate" />
    3. </bean>
    4. <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    5. <property name="dataSource" ref="dataSource" />
    6. </bean>
    7. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    8. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    9. <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
    10. <property name="username" value="root" />
    11. <property name="password" value="xiaoyue*AZ2V65" />
    12. </bean>
  2. 编写dao层测试类

    1. @Test
    2. public void Demo1() {
    3. ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("jdbcTemplete.xml");
    4. JdbcTemplate jdbcTemplate = (JdbcTemplate) ctx.getBean("jdbcTemplate");
    5. jdbcTemplate.execute("insert into student values(5,'23423',24)");
    6. }

    或者注解式

    开启注解扫描

    1. <context:component-scan base-package="com.topvision.daotest"/>

    去掉dao层的bean,即如下配置

    1. <bean id="dao" class="com.topvision.daotest.Dao">
    2. <property name="jdbcTemplate" ref="jdbcTemplate" />
    3. </bean>

    编写测试类

    1. @RunWith(SpringJUnit4ClassRunner.class)
    2. @ContextConfiguration(locations="classpath:jdbcTemplete.xml")
    3. public class Dao {
    4. @Autowired
    5. private JdbcTemplate jdbcTemplate;
    6. public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
    7. this.jdbcTemplate = jdbcTemplate;
    8. }
    9. @Test
    10. public void Demo2() {
    11. jdbcTemplate.execute("insert into student values(8,'王五',25)");
    12. }
    13. }

3. jdbcDaoSupport

JdbcDaoSupport是JDBC数据访问对象的超类,其实它使用的还是jdbcTemplete,不过需要DI依赖注入dataSource或者jdbcTemplete.

3.1 xml方式

  1. xml配置文件

    1. <bean id="jdbcDaoSupportTest" class="com.topvision.daotest.JDBCDaoSupportTest">
    2. <bean id="jdbcDaoSupportTest" class="com.topvision.daotest.JDBCDaoSupportTest">
    3. <!-- 必须为继承JdbcDaoSupport的类注入JdbcTemplate,变量名为jdbcTemplate -->
    4. <!-- <property name="jdbcTemplate" ref="jdbcTemplate"/> -->
    5. <!-- 还可以注入DataSource对象,由JdbcDaoSupport自动创建JdbcTemplate对象 -->
    6. <property name="dataSource" ref="dataSource" />
    7. </bean>
    8. <bean id="dataSource"
    9. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    10. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    11. <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
    12. <property name="username" value="root" />
    13. <property name="password" value="*****" />
    14. </bean>
  2. 测试类

    1. public class JDBCDaoSupportTest extends JdbcDaoSupport{
    2. private static ClassPathXmlApplicationContext ctx;
    3. @Test
    4. public void Demo2() {
    5. ctx = new ClassPathXmlApplicationContext("jdbcDaoSupport.xml");
    6. JDBCDaoSupportTest bean = (JDBCDaoSupportTest) ctx.getBean("jdbcDaoSupportTest");
    7. JdbcTemplate template = bean.getJdbcTemplate();
    8. String sql = "select name from student where id = ?";
    9. String name = template.queryForObject(sql, String.class,1);
    10. System.out.println(name);
    11. }
    12. }

3.2 注解方式

  1. xml配置

    1. <bean id="dataSource"
    2. class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    3. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    4. <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
    5. <property name="username" value="root" />
    6. <property name="password" value="*****" />
    7. </bean>
  2. 测试类

    1. public class JDBCDaoSupportTest extends JdbcDaoSupport{
    2. @Autowired
    3. private DataSource dataSource;
    4. @PostConstruct
    5. private void init() {
    6. setDataSource(dataSource);
    7. }
    8. @Test
    9. public void Demo1() {
    10. JdbcTemplate template = this.getJdbcTemplate();
    11. String sql = "select name from student where id = ?";
    12. String name = template.queryForObject(sql, String.class,1);
    13. System.out.println(name);
    14. }
    15. }
  3. 注入dataSource注意事项
    查看jdbcDaoSupport源码,可以注入dataSource或者注入jdbcTemplete
    注入方法有两种:

    构造方法中注入:

    1. @Autowired
    2. UserVODaoImpl(DataSource dataSource) {
    3. setDataSource(dataSource);
    4. }

    实例化前注入

    1. @Autowired
    2. private DataSource dataSource;
    3. @PostConstruct
    4. private void init() {
    5. setDataSource(dataSource);
    6. }

4. Hibernate

5. Mybatis

6. DataSource

6.1 Spring内置DriverManagerDataSource

  1. 资源包

    1. <dependency>
    2. <groupId>org.springframework</groupId>
    3. <artifactId>spring-jdbc</artifactId>
    4. </dependency>
  2. 配置项

    1. <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    2. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    3. <property name="url" value="jdbc:mysql://localhost:3306/springdb"/>
    4. <property name="username" value="root" />
    5. <property name="password" value="****" />
    6. </bean>

6.2 DBCP

  1. 资源包

    1. <dependency>
    2. <groupId>org.apache.commons</groupId>
    3. <artifactId>com.springsource.org.apache.commons.dbcp</artifactId>
    4. <version>1.2.2.osgi</version>
    5. </dependency>
    6. <dependency>
    7. <groupId>org.apache.commons</groupId>
    8. <artifactId>com.springsource.org.apache.commons.pool</artifactId>
    9. <version>1.5.3</version>
    10. </dependency>
  2. 配置项

    1. <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
    2. <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    3. <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
    4. <property name="username" value="root" />
    5. <property name="password" value="*****" />
    6. </bean>

6.3 C3P0

  1. 资源包

    1. <dependency>
    2. <groupId>com.mchange.c3p0</groupId>
    3. <artifactId>com.springsource.com.mchange.v2.c3p0</artifactId>
    4. <version>0.9.1.2</version>
    5. </dependency>
  2. 配置项

    1. <bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    2. <property name="driverClass" value="${driverClass}" />
    3. <property name="jdbcUrl" value="${url}" />
    4. <property name="user" value="${jdbc_username}" />
    5. <property name="password" value="${password}" />
    6. </bean>

6.4 外部properties输入参数

  1. 编写properties文件(jdbc.properties)

    1. driverClass=com.mysql.jdbc.Driver
    2. url=jdbc:mysql://localhost:3306/springdb
    3. jdbc_username=root
    4. password=*****
  2. 在spring配置文件中扫描properties

    方式一:可用*匹配加载多个

    1. <context:property-placeholder location="classpath:jdbc.properties"/>

    方式二:一个property对应一个文件

    1. <bean class="org.springframework.beans.factory.config.PropertyPlaceholdeConfigurer">
    2. <property name="location" value="classpath:jdbc.properties" />
    3. <property name="location" value="classpath:dsafgusd" />
    4. </bean>
  3. 用&{key}的方式引入
  1. <bean id="dataSource3" class="com.mchange.v2.c3p0.ComboPooledDataSource">
  2. <property name="driverClass" value="${driverClass}" />
  3. <property name="jdbcUrl" value="${url}" />
  4. <property name="user" value="${jdbc_username}" />
  5. <property name="password" value="${password}" />
  6. </bean>
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注