spring与JDBC集成操作

spring依赖注入jdbc:
在这里插入图片描述
jdbc配置通过硬编码方式实现
首先实例类
package com.offcn.pojo;
import java.io.Serializable;
public class ACount implements Serializable {
private int id;
public int getId() {
return id;
}
public String getSname() {
return sname;
}
public double getMoney() {
return money;
}
private String sname;
private double money;

public void setId(int id) {
    this.id = id;
}

public void setSname(String sname) {
    this.sname = sname;
}

public void setMoney(double money) {
    this.money = money;
}

}
dao接口:
package com.offcn.dao;

import com.offcn.pojo.ACount;

import java.util.List;

public interface CountDdao {

//根据id来查询
public ACount selectCount(int id);

//插寻所有
public List<ACount> selectAll();

//修改
public int updateAcount(ACount aCount);


//增加
public  int insertAcount(ACount aCount);

public void add();

}
dao实现:
package com.offcn.countdao.impl;

import com.offcn.dao.CountDdao;
import com.offcn.pojo.ACount;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class CountDaoImpl implements CountDdao {
//操作数据库的类
private JdbcTemplate jdbcTemplate;

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

/* @Override
public ACount selectCount(int id) {

    String sql="select * from count where id=?";
    Object[] objects={id};

    RowMapper<ACount> rowMapper= new RowMapper<ACount>() {
        @Override
        public ACount mapRow(ResultSet resultSet, int i) throws SQLException {
            System.out.println(i+"i");
            ACount count=new ACount();
            count.setId(resultSet.getInt("id"));
            count.setMoney(resultSet.getDouble("money"));
            return count;
        }
    };

    ACount count1 = jdbcTemplate.queryForObject(sql,Objects,rowMapper);
    return count1;
}*/


@Override
public ACount selectCount(int id) {
    String sql="select * from count where id=?";
    Object[] objects={id};

    RowMapper<ACount> rowMapper= new RowMapper<ACount>() {
        @Override
        public ACount mapRow(ResultSet resultSet, int i) throws SQLException {
            System.out.println(i+"i");
            ACount count=new ACount();
            count.setId(resultSet.getInt("id"));
            count.setMoney(resultSet.getDouble("money"));
            return count;
        }
    };

    ACount object = jdbcTemplate.queryForObject(sql,objects,rowMapper);
    return object;
}

@Override
public List<ACount> selectAll() {
    return null;
}

//@Override
/*public List<ACount> selectAll() {
    String sql="select * from count ";

    RowMapper rowMapper=new RowMapper() {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {
           ACount aCount=new ACount();
           aCount.setId(resultSet.getInt("id"));
            aCount.setMoney( resultSet.getDouble("money"));


            return aCount;
        }
    };*/

   /* List<ACount> list = jdbcTemplate.query(sql, rowMapper);
    return list;
}*/


//修改
@Override
public int updateAcount(ACount aCount) {

    Object[] objects={aCount.getMoney(),aCount.getSname(),aCount.getId()};
    String sql="update  Acount set money=? where id=?";
    /*RowMapper rowMapper=new RowMapper() {
        @Override
        public Object mapRow(ResultSet resultSet, int i) throws SQLException {


            ACount aCount=new ACount();
            aCount.setId(resultSet.getInt("id"));
            aCount.setMoney( resultSet.getDouble("money"));
            return  aCount;
        };
    };*/

    int update = jdbcTemplate.update(sql,objects);
    return  update;
}

@Override
public int insertAcount(ACount aCount) {
    Object[] objects={aCount.getSname(),aCount.getMoney()};
     String sql  = "insert into `Acount`(sname,money) values(?,?)";
    //String sql="insert into  count(sname,money) values(?,?)";
    int update = jdbcTemplate.update(sql, objects);
    return update;

}

@Override
public void add() {

}

}
xml文件配置容器

扫描二维码关注公众号,回复: 3648426 查看本文章
<?xml version="1.0" encoding="UTF-8"?>

<bean id="datasource" class="org.apache.commons.dbcp.BasicDataSource">
<!--连接数据库地址-->
<property name="url" value="jdbc:mysql://localhost:3306/count?useUnicode=true&amp;characterEncoding=utf-8"></property>
<!--为连接数据库加载驱动-->
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<!--通过xml文件容器属性获取数据库的用户名-->
<property name="username" value="root"></property>
    <!--通过xml文件容器属性获取数据库的用户名的密码-->
<property name="password" value="1234"></property>
    <property name="dataSource" ref="datasource"></property>
</bean>

<!--配置dao层,创建dao层对象-->
<bean id="countDao" class="com.offcn.countdao.impl.CountDaoImpl">

    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>

<!--配置service层,创建service层对象-->
<bean id="countService" class="com.offcn.countservice.impl.CountServiceImpl">
    
    <property name="countDdao" ref="countDao"></property>
</bean>
**测试:Text ** package com.offcn.test;

import com.offcn.countservice.CountService;
import com.offcn.pojo.ACount;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.sql.SQLOutput;
import java.util.List;

public class Text {

@Test
public  void test(){
    //查询一个
    /*ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

    CountService count1 = (CountService) app.getBean("countService");

    ACount count = count1.selectCount(1);

    System.out.println(count);*/

    //查询所有
    /*ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

    CountService count1 = (CountService) app.getBean("countService");

    List<ACount> list = count1.selectAll();
    for (ACount a:list
         ) {
        System.out.println(a);
    }*/
    //添加一个ACount对象
   /* ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");

    CountService count1 = (CountService) app.getBean("countService");
  ACount a=new ACount();
  a.setId(3);
  a.setMoney(200.0);
  a.setSname("dd");

    int i = count1.insertAcount(a);
    System.out.println(i);*/

    //修改一个ACount的信息
    ApplicationContext app=new ClassPathXmlApplicationContext("applicationContext.xml");
    CountService count1 = (CountService) app.getBean("countService");
    ACount a=new ACount();
    a.setMoney(3022.00);
    a.setId(1);
    a.setSname("张丹");
    int i = count1.updateAcount(a);
    System.out.println(i);

}

}

猜你喜欢

转载自blog.csdn.net/weixin_42772943/article/details/83002842