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

    }

                

        

            

  • 相关阅读:
    数据库函数
    数据库查询习题整理(二)
    数据库查询习题整理(一)
    1202数据库查询
    1201新课程TSQL语句
    1123项目第五天
    1121项目第四天
    1120项目第三天
    14
    にちかん
  • 原文地址:https://www.cnblogs.com/xu06123/p/8567707.html
Copyright © 2011-2022 走看看