Spring的DAO模块数据库操作实例

来源:互联网 发布:java中获取当前时间 编辑:程序博客网 时间:2024/05/21 22:42

Spring的DAO模块提供了对了JDBC、Hibernate、JDO等DAO层支持。DAO模块依赖于
这里写图片描述,MyEclipse自带的Spring DAO类库没有这两个类库,需要自己添加。
下面以保存实体Person为例作介绍:
传统的JDBC编程,总免不了与Connection、Statement、PreparedStatement、ResultSet、SQLException等打交道,还要注意打开连接后要释放连接等琐碎的问题。
Spring框架对JDBC进行了封装,完全抛弃了JDBC API。数据库连接、事务等也交给了Spring打点,开发者只需要使用封装好的JdbcTemplate执行SQL语句,然后得到需要的结果。

实体类Person:

本类的POJO实体类为Person类。本例将使用Spring提供的JdbcTemplate将Person持久化到数据库中,或者将Person从数据库中读取出来。Person类的代码如下:
这里写图片描述
Person.java

package com.helloweenvsfei.spring.dao;import java.util.Date;public class Person {    private Integer id;    private String name;    private String sex;    private int age;    private Date birthday;    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 String getSex() {        return sex;    }    public void setSex(String sex) {        this.sex = sex;    }    public int getAge() {        return age;    }    public void setAge(int age) {        this.age = age;    }    public Date getBirthday() {        return birthday;    }    public void setBirthday(Date birthday) {        this.birthday = birthday;    }}

DAO层接口

DAO层接口定义了操作Person实体类的方法。IPerson接口定义了4个方法:
这里写图片描述
分别查询某人员姓名、添加某人员信息、查询记录数、获取所有人员信息等。
IPersonDao接口代码没有与Spring的DAO模块耦合。代码如下:
IPersonDao.java

package com.helloweenvsfei.spring.dao;import java.util.List;public interface IPersonDao {    public String getPersonName(Integer id);    public void addPerson(Person person);    public int getPersonCount();    public List<Person> listPersons();}

继承JdbcDaoSupport
JdbcDaoSupport类来自于这里写图片描述。PersonDaoImpl实现了接口IPersonDao接口,并继承Spring的DAO模块中的JdbcDaoSupport类。JdbcDaoSupport提供JdbcTemplate对象,封装了常用的JDBC操作。PersonDaoImpl中还定义了一个初始化方法,用于初始化表结构(如果不存在则创建)。
由于代码直接继承了Spring提供的JdbcDaoSupport,因此没有使用Connection、Statement等JDBC API,也不用关闭这些资源。JdbcDaoSupport会自动维护这些资源。代码中用Spring封装好的JdbcTemplate来执行SQL、查询Person列表、查询单个Person属性值、查询Person的总数。查询Person列表时返回的是一个List

package com.helloweenvsfei.spring.dao;import java.sql.Date;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.springframework.jdbc.core.support.JdbcDaoSupport;public class PersonDaoImpl extends JdbcDaoSupport implements IPersonDao {    public void initDatabase()    {        String sql="create table if not exists tb_person "                +"(id int auto_increment, "+"name varchar(255), "+"sex varchar(10) , age int ,birthday timestamp, primary key(id))";        this.getJdbcTemplate().execute(sql);    }    @Override    public String getPersonName(Integer id) {        String sql="select name from tb_person where id="+id;        return (String)this.getJdbcTemplate().queryForObject(sql, String.class);    }    @Override    public void addPerson(Person person) {        String sql="insert into tb_person(name,sex,age,birthday) values(?,?,?,?)";        this.getJdbcTemplate().update(sql, new Object[]{                person.getName(),person.getSex(),person.getAge(),person.getBirthday(),        });    }    @Override    public int getPersonCount() {        String sql="select count(*) from tb_person";        return this.getJdbcTemplate().queryForInt(sql);    }    @Override    public List<Person> listPersons() {        String sql="select id,name,sex,age,birthday for tb_person";        @SuppressWarnings("unchecked")        List<Map<String,Object>> list=this.getJdbcTemplate().queryForList(sql);        List<Person> personList=new ArrayList<Person>();        for(Map<String,Object> row:list)        {            Person person=new Person();            person.setId((Integer)row.get("id"));            person.setName(row.get("name").toString());            person.setSex(row.get("sex").toString());            person.setAge((Integer)row.get("age"));            person.setBirthday((Date)row.get("birthday"));            personList.add(person);        }        return personList;    }}

Spring配置

applicationContext.xml中需要配置一个数据源,并将该数据源设置到personDao中。配置代码如下:
注意使用com.mysql.jdbc.Driver驱动需要添加下面的库:
这里写图片描述
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:p="http://www.springframework.org/schema/p"    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd"><bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">    <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>    <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=UTF-8">    </property>    <property name="username" value="root"></property>    <property name="password" value="123"></property></bean><bean id="personDao" class="com.helloweenvsfei.spring.dao.PersonDaoImpl" depends-on="dataSource" init-method="initDatabase">    <property name="dataSource" ref="dataSource"></property></bean></beans>

运行代码

运行程序前不需要创建表,PersonDaoImpl的initDatabase()会自动创建表结构。但是需要创建数据库spring,因此initDatabase()无法创建数据库。创建数据库的DDL语句:

create database spring character set utf8;

下面的代码中先加载applicationContext.xml、创建BeanFactory并从中获取DAO对象,然后实例化一个Person对象,并用DAO保存进数据,最后输出记录总数、所有的Person对象。运行代码如下:
DaoRun.java

package com.helloweenvsfei.spring.dao;import java.util.ArrayList;import java.util.Date;import java.util.List;import org.springframework.beans.factory.xml.XmlBeanFactory;import org.springframework.core.io.ClassPathResource;public class DaoRun {    public static void main(String[] args) {        XmlBeanFactory factory=new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));        IPersonDao personDao=(IPersonDao)factory.getBean("personDao");        Person person=new Person();        person.setName("Helloween");        person.setAge(30);        person.setSex("男");        person.setBirthday(new Date());        personDao.addPerson(person);        System.out.println("Count:"+personDao.getPersonCount());        System.out.println(personDao.getPersonName(1));        List<Person> personList=new ArrayList<Person>();        for(Person p:personList)        {            System.out.println("Name:"+p.getName());        }    }}

运行效果如下:

2017-01-04 02:50:26,635 [org.springframework.core.env.MutablePropertySources]-[DEBUG] Adding [systemProperties] PropertySource with lowest search precedence2017-01-04 02:50:26,647 [org.springframework.core.env.MutablePropertySources]-[DEBUG] Adding [systemEnvironment] PropertySource with lowest search precedence2017-01-04 02:50:26,648 [org.springframework.core.env.AbstractEnvironment]-[DEBUG] Initialized StandardEnvironment with PropertySources [systemProperties,systemEnvironment]2017-01-04 02:50:26,658 [org.springframework.beans.factory.xml.XmlBeanDefinitionReader]-[INFO] Loading XML bean definitions from class path resource [applicationContext.xml]2017-01-04 02:50:26,698 [org.springframework.beans.factory.xml.DefaultDocumentLoader]-[DEBUG] Using JAXP provider [com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl]2017-01-04 02:50:26,771 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] Trying to resolve XML entity with public id [null] and system id [http://www.springframework.org/schema/beans/spring-beans-4.1.xsd]2017-01-04 02:50:26,773 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] Loading schema mappings from [META-INF/spring.schemas]2017-01-04 02:50:26,791 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] Loaded schema mappings: {http://www.springframework.org/schema/aop/spring-aop-4.1.xsd=org/springframework/aop/config/spring-aop-4.1.xsd, http://www.springframework.org/schema/context/spring-context-3.1.xsd=org/springframework/context/config/spring-context-3.1.xsd, http://www.springframework.org/schema/jms/spring-jms-2.5.xsd=org/springframework/jms/config/spring-jms-2.5.xsd, http://www.springframework.org/schema/util/spring-util-3.0.xsd=org/springframework/beans/factory/xml/spring-util-3.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool.xsd=org/springframework/beans/factory/xml/spring-tool-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-3.2.xsd=org/springframework/aop/config/spring-aop-3.2.xsd, http://www.springframework.org/schema/lang/spring-lang-4.1.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd, http://www.springframework.org/schema/context/spring-context-4.0.xsd=org/springframework/context/config/spring-context-4.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd=org/springframework/web/servlet/config/spring-mvc-3.2.xsd, http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd=org/springframework/oxm/config/spring-oxm-3.0.xsd, http://www.springframework.org/schema/tool/spring-tool-4.1.xsd=org/springframework/beans/factory/xml/spring-tool-4.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.2.xsd=org/springframework/scripting/config/spring-lang-3.2.xsd, http://www.springframework.org/schema/cache/spring-cache-3.2.xsd=org/springframework/cache/config/spring-cache-3.2.xsd, http://www.springframework.org/schema/jee/spring-jee-4.1.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd, http://www.springframework.org/schema/util/spring-util-2.0.xsd=org/springframework/beans/factory/xml/spring-util-2.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.2.xsd=org/springframework/beans/factory/xml/spring-tool-3.2.xsd, http://www.springframework.org/schema/context/spring-context.xsd=org/springframework/context/config/spring-context-4.1.xsd, http://www.springframework.org/schema/cache/spring-cache-4.1.xsd=org/springframework/cache/config/spring-cache-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-4.0.xsd=org/springframework/aop/config/spring-aop-4.0.xsd, http://www.springframework.org/schema/jee/spring-jee-3.2.xsd=org/springframework/ejb/config/spring-jee-3.2.xsd, http://www.springframework.org/schema/context/spring-context-3.0.xsd=org/springframework/context/config/spring-context-3.0.xsd, http://www.springframework.org/schema/util/spring-util-2.5.xsd=org/springframework/beans/factory/xml/spring-util-2.5.xsd, http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd=org/springframework/web/servlet/config/spring-mvc-4.0.xsd, http://www.springframework.org/schema/beans/spring-beans-3.2.xsd=org/springframework/beans/factory/xml/spring-beans-3.2.xsd, http://www.springframework.org/schema/aop/spring-aop-3.1.xsd=org/springframework/aop/config/spring-aop-3.1.xsd, http://www.springframework.org/schema/lang/spring-lang-4.0.xsd=org/springframework/scripting/config/spring-lang-4.0.xsd, http://www.springframework.org/schema/mvc/spring-mvc.xsd=org/springframework/web/servlet/config/spring-mvc-4.1.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd=org/springframework/web/servlet/config/spring-mvc-3.1.xsd, http://www.springframework.org/schema/beans/spring-beans-4.1.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool-4.0.xsd=org/springframework/beans/factory/xml/spring-tool-4.0.xsd, http://www.springframework.org/schema/lang/spring-lang-3.1.xsd=org/springframework/scripting/config/spring-lang-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-3.1.xsd=org/springframework/cache/config/spring-cache-3.1.xsd, http://www.springframework.org/schema/jee/spring-jee-4.0.xsd=org/springframework/ejb/config/spring-jee-4.0.xsd, http://www.springframework.org/schema/task/spring-task-4.1.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool-3.1.xsd=org/springframework/beans/factory/xml/spring-tool-3.1.xsd, http://www.springframework.org/schema/cache/spring-cache-4.0.xsd=org/springframework/cache/config/spring-cache-4.0.xsd, http://www.springframework.org/schema/jee/spring-jee-3.1.xsd=org/springframework/ejb/config/spring-jee-3.1.xsd, http://www.springframework.org/schema/task/spring-task-3.2.xsd=org/springframework/scheduling/config/spring-task-3.2.xsd, http://www.springframework.org/schema/beans/spring-beans-3.1.xsd=org/springframework/beans/factory/xml/spring-beans-3.1.xsd, http://www.springframework.org/schema/util/spring-util.xsd=org/springframework/beans/factory/xml/spring-util-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-3.0.xsd=org/springframework/aop/config/spring-aop-3.0.xsd, http://www.springframework.org/schema/jms/spring-jms-3.2.xsd=org/springframework/jms/config/spring-jms-3.2.xsd, http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd=org/springframework/web/servlet/config/spring-mvc-3.0.xsd, http://www.springframework.org/schema/beans/spring-beans-4.0.xsd=org/springframework/beans/factory/xml/spring-beans-4.0.xsd, http://www.springframework.org/schema/beans/spring-beans.xsd=org/springframework/beans/factory/xml/spring-beans-4.1.xsd, http://www.springframework.org/schema/lang/spring-lang-3.0.xsd=org/springframework/scripting/config/spring-lang-3.0.xsd, http://www.springframework.org/schema/context/spring-context-2.5.xsd=org/springframework/context/config/spring-context-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms-4.1.xsd=org/springframework/jms/config/spring-jms-4.1.xsd, http://www.springframework.org/schema/task/spring-task-4.0.xsd=org/springframework/scheduling/config/spring-task-4.0.xsd, http://www.springframework.org/schema/tool/spring-tool-3.0.xsd=org/springframework/beans/factory/xml/spring-tool-3.0.xsd, http://www.springframework.org/schema/aop/spring-aop-2.0.xsd=org/springframework/aop/config/spring-aop-2.0.xsd, http://www.springframework.org/schema/jee/spring-jee-3.0.xsd=org/springframework/ejb/config/spring-jee-3.0.xsd, http://www.springframework.org/schema/task/spring-task-3.1.xsd=org/springframework/scheduling/config/spring-task-3.1.xsd, http://www.springframework.org/schema/util/spring-util-4.1.xsd=org/springframework/beans/factory/xml/spring-util-4.1.xsd, http://www.springframework.org/schema/beans/spring-beans-3.0.xsd=org/springframework/beans/factory/xml/spring-beans-3.0.xsd, http://www.springframework.org/schema/jee/spring-jee.xsd=org/springframework/ejb/config/spring-jee-4.1.xsd, http://www.springframework.org/schema/aop/spring-aop-2.5.xsd=org/springframework/aop/config/spring-aop-2.5.xsd, http://www.springframework.org/schema/jms/spring-jms.xsd=org/springframework/jms/config/spring-jms-4.1.xsd, http://www.springframework.org/schema/lang/spring-lang-2.0.xsd=org/springframework/scripting/config/spring-lang-2.0.xsd, http://www.springframework.org/schema/jms/spring-jms-3.1.xsd=org/springframework/jms/config/spring-jms-3.1.xsd, http://www.springframework.org/schema/oxm/spring-oxm.xsd=org/springframework/oxm/config/spring-oxm-4.1.xsd, http://www.springframework.org/schema/util/spring-util-3.2.xsd=org/springframework/beans/factory/xml/spring-util-3.2.xsd, http://www.springframework.org/schema/oxm/spring-oxm-4.1.xsd=org/springframework/oxm/config/spring-oxm-4.1.xsd, http://www.springframework.org/schema/task/spring-task.xsd=org/springframework/scheduling/config/spring-task-4.1.xsd, http://www.springframework.org/schema/tool/spring-tool-2.0.xsd=org/springframework/beans/factory/xml/spring-tool-2.0.xsd, http://www.springframework.org/schema/lang/spring-lang-2.5.xsd=org/springframework/scripting/config/spring-lang-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-2.0.xsd=org/springframework/ejb/config/spring-jee-2.0.xsd, http://www.springframework.org/schema/jms/spring-jms-4.0.xsd=org/springframework/jms/config/spring-jms-4.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd=org/springframework/oxm/config/spring-oxm-3.2.xsd, http://www.springframework.org/schema/tool/spring-tool-2.5.xsd=org/springframework/beans/factory/xml/spring-tool-2.5.xsd, http://www.springframework.org/schema/jee/spring-jee-2.5.xsd=org/springframework/ejb/config/spring-jee-2.5.xsd, http://www.springframework.org/schema/task/spring-task-3.0.xsd=org/springframework/scheduling/config/spring-task-3.0.xsd, http://www.springframework.org/schema/util/spring-util-4.0.xsd=org/springframework/beans/factory/xml/spring-util-4.0.xsd, http://www.springframework.org/schema/lang/spring-lang.xsd=org/springframework/scripting/config/spring-lang-4.1.xsd, http://www.springframework.org/schema/context/spring-context-3.2.xsd=org/springframework/context/config/spring-context-3.2.xsd, http://www.springframework.org/schema/jms/spring-jms-3.0.xsd=org/springframework/jms/config/spring-jms-3.0.xsd, http://www.springframework.org/schema/util/spring-util-3.1.xsd=org/springframework/beans/factory/xml/spring-util-3.1.xsd, http://www.springframework.org/schema/beans/spring-beans-2.0.xsd=org/springframework/beans/factory/xml/spring-beans-2.0.xsd, http://www.springframework.org/schema/oxm/spring-oxm-4.0.xsd=org/springframework/oxm/config/spring-oxm-4.0.xsd, http://www.springframework.org/schema/cache/spring-cache.xsd=org/springframework/cache/config/spring-cache-4.1.xsd, http://www.springframework.org/schema/context/spring-context-4.1.xsd=org/springframework/context/config/spring-context-4.1.xsd, http://www.springframework.org/schema/beans/spring-beans-2.5.xsd=org/springframework/beans/factory/xml/spring-beans-2.5.xsd, http://www.springframework.org/schema/oxm/spring-oxm-3.1.xsd=org/springframework/oxm/config/spring-oxm-3.1.xsd, http://www.springframework.org/schema/aop/spring-aop.xsd=org/springframework/aop/config/spring-aop-4.1.xsd}2017-01-04 02:50:26,798 [org.springframework.beans.factory.xml.PluggableSchemaResolver]-[DEBUG] Found XML schema [http://www.springframework.org/schema/beans/spring-beans-4.1.xsd] in classpath: org/springframework/beans/factory/xml/spring-beans-4.1.xsd2017-01-04 02:50:26,954 [org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader]-[DEBUG] Loading bean definitions2017-01-04 02:50:27,005 [org.springframework.beans.factory.xml.BeanDefinitionParserDelegate]-[DEBUG] Neither XML 'id' nor 'name' specified - using generated bean name [com.helloweenvsfei.spring.aop.MethodBeforeInterceptor#4715c34e]2017-01-04 02:50:27,009 [org.springframework.beans.factory.xml.BeanDefinitionParserDelegate]-[DEBUG] Neither XML 'id' nor 'name' specified - using generated bean name [com.helloweenvsfei.spring.aop.MethodAfterInterceptor#64a39f6]2017-01-04 02:50:27,012 [org.springframework.beans.factory.xml.BeanDefinitionParserDelegate]-[DEBUG] Neither XML 'id' nor 'name' specified - using generated bean name [com.helloweenvsfei.spring.aop.ThrowsInterceptor#117a1ad3]2017-01-04 02:50:27,016 [org.springframework.beans.factory.xml.BeanDefinitionParserDelegate]-[DEBUG] Neither XML 'id' nor 'name' specified - using generated bean name [com.helloweenvsfei.spring.aop.AopServiceImpl#591c486a]2017-01-04 02:50:27,027 [org.springframework.beans.factory.support.DefaultSingletonBeanRegistry]-[DEBUG] Creating shared instance of singleton bean 'dataSource'2017-01-04 02:50:27,028 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Creating instance of bean 'dataSource'2017-01-04 02:50:27,072 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Eagerly caching bean 'dataSource' to allow for resolving potential circular references2017-01-04 02:50:27,096 [org.springframework.core.io.support.SpringFactoriesLoader]-[DEBUG] Loaded [org.springframework.beans.BeanInfoFactory] names: [org.springframework.beans.ExtendedBeanInfoFactory]2017-01-04 02:50:27,105 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Getting BeanInfo for class [org.apache.commons.dbcp.BasicDataSource]2017-01-04 02:50:27,122 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Caching PropertyDescriptors for class [org.apache.commons.dbcp.BasicDataSource]2017-01-04 02:50:27,123 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'accessToUnderlyingConnectionAllowed' of type [boolean]2017-01-04 02:50:27,132 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'class' of type [java.lang.Class]2017-01-04 02:50:27,136 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'connection' of type [java.sql.Connection]2017-01-04 02:50:27,137 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'defaultAutoCommit' of type [boolean]2017-01-04 02:50:27,138 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'defaultCatalog' of type [java.lang.String]2017-01-04 02:50:27,139 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'defaultReadOnly' of type [boolean]2017-01-04 02:50:27,140 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'defaultTransactionIsolation' of type [int]2017-01-04 02:50:27,141 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'driverClassName' of type [java.lang.String]2017-01-04 02:50:27,143 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'initialSize' of type [int]2017-01-04 02:50:27,147 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'logAbandoned' of type [boolean]2017-01-04 02:50:27,148 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'logWriter' of type [java.io.PrintWriter]2017-01-04 02:50:27,149 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'loginTimeout' of type [int]2017-01-04 02:50:27,150 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'maxActive' of type [int]2017-01-04 02:50:27,151 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'maxIdle' of type [int]2017-01-04 02:50:27,151 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'maxOpenPreparedStatements' of type [int]2017-01-04 02:50:27,152 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'maxWait' of type [long]2017-01-04 02:50:27,153 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'minEvictableIdleTimeMillis' of type [long]2017-01-04 02:50:27,154 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'minIdle' of type [int]2017-01-04 02:50:27,154 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'numActive' of type [int]2017-01-04 02:50:27,155 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'numIdle' of type [int]2017-01-04 02:50:27,156 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'numTestsPerEvictionRun' of type [int]2017-01-04 02:50:27,157 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'password' of type [java.lang.String]2017-01-04 02:50:27,158 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'poolPreparedStatements' of type [boolean]2017-01-04 02:50:27,159 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'removeAbandoned' of type [boolean]2017-01-04 02:50:27,159 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'removeAbandonedTimeout' of type [int]2017-01-04 02:50:27,161 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'testOnBorrow' of type [boolean]2017-01-04 02:50:27,162 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'testOnReturn' of type [boolean]2017-01-04 02:50:27,163 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'testWhileIdle' of type [boolean]2017-01-04 02:50:27,164 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'timeBetweenEvictionRunsMillis' of type [long]2017-01-04 02:50:27,164 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'url' of type [java.lang.String]2017-01-04 02:50:27,165 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'username' of type [java.lang.String]2017-01-04 02:50:27,165 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'validationQuery' of type [java.lang.String]2017-01-04 02:50:27,174 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Finished creating instance of bean 'dataSource'2017-01-04 02:50:27,176 [org.springframework.beans.factory.support.DefaultSingletonBeanRegistry]-[DEBUG] Creating shared instance of singleton bean 'personDao'2017-01-04 02:50:27,177 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Creating instance of bean 'personDao'2017-01-04 02:50:27,185 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Eagerly caching bean 'personDao' to allow for resolving potential circular references2017-01-04 02:50:27,185 [org.springframework.beans.factory.support.AbstractBeanFactory]-[DEBUG] Returning cached instance of singleton bean 'dataSource'2017-01-04 02:50:27,186 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Getting BeanInfo for class [com.helloweenvsfei.spring.dao.PersonDaoImpl]2017-01-04 02:50:27,200 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Caching PropertyDescriptors for class [com.helloweenvsfei.spring.dao.PersonDaoImpl]2017-01-04 02:50:27,201 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'class' of type [java.lang.Class]2017-01-04 02:50:27,202 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'dataSource' of type [javax.sql.DataSource]2017-01-04 02:50:27,203 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'jdbcTemplate' of type [org.springframework.jdbc.core.JdbcTemplate]2017-01-04 02:50:27,204 [org.springframework.beans.CachedIntrospectionResults]-[DEBUG] Found bean property 'personCount' of type [int]2017-01-04 02:50:27,226 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Invoking afterPropertiesSet() on bean with name 'personDao'2017-01-04 02:50:27,227 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Invoking init method  'initDatabase' on bean with name 'personDao'2017-01-04 02:50:27,227 [org.springframework.jdbc.core.JdbcTemplate]-[DEBUG] Executing SQL statement [create table if not exists tb_person (id int auto_increment, name varchar(255), sex varchar(10) , age int ,birthday timestamp, primary key(id))]2017-01-04 02:50:27,234 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource2017-01-04 02:50:27,667 [org.springframework.jdbc.core.JdbcTemplate]-[WARN] SQLWarning ignored: java.sql.SQLWarning: Table 'tb_person' already exists2017-01-04 02:50:27,672 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource2017-01-04 02:50:27,673 [org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory]-[DEBUG] Finished creating instance of bean 'personDao'2017-01-04 02:50:27,677 [org.springframework.jdbc.core.JdbcTemplate]-[DEBUG] Executing SQL update [insert into tb_person(name,sex,age,birthday) values(?,?,?,?)]2017-01-04 02:50:27,679 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource2017-01-04 02:50:27,696 [org.springframework.jdbc.core.StatementCreatorUtils]-[DEBUG] Setting SQL statement parameter value: column index 1, parameter value [Helloween], value class [java.lang.String], SQL type unknown2017-01-04 02:50:27,697 [org.springframework.jdbc.core.StatementCreatorUtils]-[DEBUG] Setting SQL statement parameter value: column index 2, parameter value [男], value class [java.lang.String], SQL type unknown2017-01-04 02:50:27,698 [org.springframework.jdbc.core.StatementCreatorUtils]-[DEBUG] Setting SQL statement parameter value: column index 3, parameter value [30], value class [java.lang.Integer], SQL type unknown2017-01-04 02:50:27,703 [org.springframework.jdbc.core.StatementCreatorUtils]-[DEBUG] Setting SQL statement parameter value: column index 4, parameter value [Wed Jan 04 02:50:27 CST 2017], value class [java.util.Date], SQL type unknown2017-01-04 02:50:27,721 [org.springframework.jdbc.core.JdbcTemplate$2]-[DEBUG] SQL update affected 1 rows2017-01-04 02:50:27,723 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSource2017-01-04 02:50:27,731 [org.springframework.jdbc.core.JdbcTemplate]-[DEBUG] Executing SQL query [select count(*) from tb_person]2017-01-04 02:50:27,732 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource2017-01-04 02:50:27,742 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSourceCount:12017-01-04 02:50:27,745 [org.springframework.jdbc.core.JdbcTemplate]-[DEBUG] Executing SQL query [select name from tb_person where id=1]2017-01-04 02:50:27,745 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Fetching JDBC Connection from DataSource2017-01-04 02:50:27,747 [org.springframework.jdbc.datasource.DataSourceUtils]-[DEBUG] Returning JDBC Connection to DataSourceHelloween

数据中显示如下:
这里写图片描述
至此所有的知识点 讲完了,希望对初学Spring的人有所帮助!!

1 0
原创粉丝点击