zoukankan      html  css  js  c++  java
  • jdbcTemplate

    JDBC:用来进行数据访问的技术    使用前需要先加载驱动

    jdbcTemplate:

    先导入jar包:

        <!--spring JDBC-->

    <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-jdbc</artifactId>
    <version>4.0.2.RELEASE</version>
    </dependency>

       

    在数据库中创建数据表

    创建测试类:

     

              public class Student {

             private Integer sid;

    private String sname;

    public Integer getSid() {
    return sid;
    }

    public void setSid(Integer sid) {
    this.sid = sid;
    }

    public String getSname() {
    return sname;
    }

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

    创建dao层:

         public interface StudentDao{

    public List<Student> getAllStudents();
    }

         

            public class StudentDaoimpl extends JdbcDaoSupport implements StudentDao {

    public List<Student> getAllStudents() {
    final String sql="select * from student";
    List<Student> list = this.getJdbcTemplate().query(sql, new RowMapper<Student>() {
    public Student mapRow(ResultSet resultSet, int i) throws SQLException {
    Student student = new Student();
    student.setSid(resultSet.getInt("sid"));
    student.setSname(resultSet.getString("sname"));
    return student;
    }
    });
    return list;
    }
    }

    创建service层:

         

    public interface StudentService {
    public List<Student> getAllStudents();
    }

    public class StudentServiceimpl implements StudentService {
    private StudentDao studentDao;

    public StudentDao getStudentDao() {
    return studentDao;
    }

    public void setStudentDao(StudentDao studentDao) {
    this.studentDao = studentDao;
    }

    public List<Student> getAllStudents() {
    return studentDao.getAllStudents();
    }
    }

    编写配置文件:

                

    jdbc.driver=com.mysql.jdbc.Driver
    jdbc.url=jdbc:mysql:///y2167
    jdbc.username=root
    jdbc.password=

              

              

    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
    <property name="driverClassName" value="${jdbc.driver}"></property>
    <property name="url" value="${jdbc.url}"></property>
    <property name="username" value="${jdbc.username}"></property>
    <property name="password" value="${jdbc.password}"></property>
    </bean>
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
    <property name="dataSource" ref="dataSource"></property>
    </bean>
    <bean id="studentdao" class="cn.day06jdbc.dao.StudentDaoimpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>
    <bean id="service" class="cn.day06jdbc.service.StudentServiceimpl">
    <property name="studentDao" ref="studentdao"></property>
    </bean>

    测试类:

               

    public class test6 {
    @Test
    public void t1(){
    ApplicationContext context=new ClassPathXmlApplicationContext("applicationContextday06jdbc.xml");
    StudentService service = (StudentService)context.getBean("service");
    List<Student> allStudents = service.getAllStudents();
    for (Student item:allStudents) {
    System.out.println(item.getSid()+" "+item.getSname());
    }
    }

    }

                

        

            

  • 相关阅读:
    HBase with MapReduce (MultiTable Read)
    HBase with MapReduce (SummaryToFile)
    HBase with MapReduce (Summary)
    HBase with MapReduce (Read and Write)
    HBase with MapReduce (Only Read)
    Hbase中的BloomFilter(布隆过滤器)
    HBase的快照技术
    How To Use Hbase Bulk Loading
    Cloudera-Manager修改集群的IP
    Java中的HashSet和TreeSet
  • 原文地址:https://www.cnblogs.com/xu06123/p/8567707.html
Copyright © 2011-2022 走看看