• 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());
    }
    }

    }

                

        

            

  • 相关阅读:
    关于 Delphi 中流的使用(2) 用 TFileStream(文件流) 读写
    今日工作心得:asp.net中使用javascript进行验证的注意点
    今日工作心得:能够引起FileUpload控件的PostedFile总是为null的一个原因
    完美解决office 2007安装失败(Office.ZhCN/...)
    关于FCKeditor,上传文件时提示invalid request
    今日学习:关于C#多线程之一——异步委托
    今日工作心得:关于C#读取Excel的一些资料
    c# webBrowser读取gb2312 中文变方框的问题
    C#中HashTable的用法
    今日工作心得——javascript结合jquery使图片适应窗口大小
  • 原文地址:https://www.cnblogs.com/xu06123/p/8567707.html
Copyright © 2020-2023  润新知