SpringJDBC的实现

这是我的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"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans  
            http://www.springframework.org/schema/beans/spring-beans.xsd  
            http://www.springframework.org/schema/tx 
            http://www.springframework.org/schema/tx/spring-tx.xsd  
            http://www.springframework.org/schema/aop   
            http://www.springframework.org/schema/aop/spring-aop.xsd  
            http://www.springframework.org/schema/context  
            http://www.springframework.org/schema/context/spring-context.xsd">
    
    <!--配置数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
        <property name="username" value="root" />
        <property name="password" value="123" />
    </bean>
                    
                    
    <!-- SpringJDBC操作对象 -->                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
    <bean id="service" class="com.nan.service.UserServiceImpl">
        <property name="dao" ref="userDao"></property>
    </bean>
    
    <bean id="userDao" class="com.nan.dao.UserDaoImpl">
     <!--将spring jdbc操作对象注入dao层 -->   <property name="jdbcTemplate" ref="jdbcTemplate"></property> </bean> </beans>

 

 

在此开始分步骤说明:


 

  第一步:配置数据源

<!--配置数据源-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
    <property name="url" value="jdbc:mysql://localhost:3306/spring"></property>
    <property name="username" value="root" />
    <property name="password" value="123" />
</bean>    

 

  第二步:配置操作SpringJDBC的对象

  <!-- SpringJDBC操作对象 -->                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

 

  第三步:将jdbcTemplate bean配置到dao层直接使用即可

<bean id="userDao" class="com.nan.dao.UserDaoImpl">
   <!--将spring jdbc操作对象注入dao层 -->
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

 

  第四步:java使用spring jdbcpublic class UserDaoImpl implements Dao {

    
    JdbcTemplate jdbcTemplate;
    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public void add(String name) {
        System.out.println("进入增加");
        // TODO Auto-generated method stub
        jdbcTemplate.update("insert into user(username) values(?)",name);
    }

    @Override
    public void delete(String name) {
        jdbcTemplate.update("delete from user where username=?",name);

    }

    @Override
    public void update(int id,String name) {
        jdbcTemplate.update("update set name = ? where id = ?",name,id);

    }
    
  //查询
public void query() { RowMapper<UserVO> rowMapper = new BeanPropertyRowMapper(UserVO.class); List<UserVO> list = jdbcTemplate.query("select id,username name from user",rowMapper); for(UserVO l:list) { System.out.println(l); } } }

 

SpringJDBC的实现

上一篇:Go 语言的基本数据类型


下一篇:微信公众号支付H5调用支付详解