码迷,mamicode.com
首页 > 数据库 > 详细

Spring JDBC

时间:2017-10-18 00:24:57      阅读:234      评论:0      收藏:0      [点我收藏+]

标签:res   http   style   lin   list   ora   contex   实体类   oca   

Spring jdbc的三种连接方式

分别引入不同的jar包。

实体类

import java.util.Date;

public class Category {
    private  Integer id;
    private  String name;
    private Date createDate;

    public Category() {
    }


    public Category(Integer id, String name, Date createDate) {
        this.id = id;
        this.name = name;
        this.createDate = createDate;
    }
    @Override
    public String toString() {
        return "Category{" +
                "id=" + id +
                ", name=‘" + name + ‘\‘‘ +
                ", createDate=" + createDate +
                ‘}‘;
    }

    public Integer getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }
}

-dao层

public interface ICategoryDao {
    public List<Category>  findAll();
}

impl

public class ICategoryDaoImpl extends JdbcDaoSupport  implements ICategoryDao {
    public List<Category> findAll() {
        String sql="select * from news_category";
        List<Category> list=getJdbcTemplate().query(sql, new RowMapper<Category>() {
            /**
             *
             * @param resultSet 结果集
             * @param i  索引
             * @return
             * @throws SQLException
             */
            public Category mapRow(ResultSet rs, int i) throws SQLException {
                Category category=new Category();
                category.setId(rs.getInt("id"));
                category.setName(rs.getString("name"));
                category.setCreateDate(rs.getDate("createDate"));
                return category;
            }
        });
        return list;
    }
}

service

public interface ICategoryService {
    public List<Category> findAll();
}

serviceimpl

public class CategoryServiceImpl implements ICategoryService {
    private ICategoryDao dao;

    public ICategoryDao getDao() {
        return dao;
    }

    public void setDao(ICategoryDao dao) {
        this.dao = dao;
    }

    public List<Category> findAll() {
        return dao.findAll();
    }
}

ApplicationContext.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:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
      http://www.springframework.org/schema/beans/spring-beans.xsd
      http://www.springframework.org/schema/context
      http://www.springframework.org/schema/context/spring-context.xsd
     http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<!--DataSource 数据源 提供链接,配置数据库的四大属性-->
    <!--   <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
       <property name="url" value="jdbc:mysql:///news"></property>
       <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
          <property name="username" value="root"></property>
       <property name="password" value=""></property>
   </bean>-->
    <!--c3p0-->
   <!-- <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="jdbcUrl" value="jdbc:mysql:///news"></property>
        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>
        <property name="user" value="root"></property>
        <property name="password" value=" "></property>
    </bean> -->

    <!--阿里-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
     <property name="url" value="jdbc:mysql:///news"></property>
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
     <property name="username" value="root"></property>
     <property name="password" value=" "></property>
 </bean>
<!--jdbcTemplate-->
    <bean id="jdbcTemplate" class ="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
   <!--Dao-->
<bean id="categoryDao" class="cn.kitty.dao.impl.ICategoryDaoImpl">
    <property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean>
    <!--service-->
<bean id="CategoryService" class="cn.kitty.service.impl.CategoryServiceImpl">
    <property name="dao" ref="categoryDao"></property>
</bean>
</beans>

test

public class Test20171017 {
    @Test
    public void jdbc(){
        ApplicationContext context=new ClassPathXmlApplicationContext("ApplicationContext.xml");
        ICategoryService service = (ICategoryService) context.getBean("CategoryService");
        List<Category> list = service.findAll();
        for ( Category c: list) {
            System.out.println(list);
        }
    }
}

 

Spring JDBC

标签:res   http   style   lin   list   ora   contex   实体类   oca   

原文地址:http://www.cnblogs.com/cuixiaomeng/p/7684483.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!