spring-query by ID using JdbcTemplate implementation

Source: Internet
Author: User

First, create a spring project
Project Name: spring101306
Ii. adding a jar package to the project
1. Create a Lib directory in your project
/lib
2. Add spring support under the Lib directory
Commons-logging.jar
Junit-4.10.jar
Log4j.jar
Mysql-connector-java-5.1.18-bin.jar
Spring-beans-3.2.0.release.jar
Spring-context-3.2.0.release.jar
Spring-core-3.2.0.release.jar
Spring-expression-3.2.0.release.jar
Spring-jdbc-3.2.0.release.jar
Spring-tx-3.2.0.release.jar
Com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
Iii. adding a profile and properties file to a project
1. Create the Conf directory in your project
2. Add a property file under the Conf directory
Property file name: Jdbc.properties
Properties File Contents:
Jdbc.url=jdbc:mysql://localhost:3306/spring
Jdbc.driver=com.mysql.jdbc.driver
Jdbc.username=root
Jdbc.password=root
2. Add the Spring core configuration file under the Conf directory
Configuration file name: Applicationcontext.xml
Configuration file Contents:
<?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: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 " >

<!--load Properties file--
<context:property-placeholder location= "Classpath:jdbc.properties"/>

<!--1. Configure the database connection pool--
<bean id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" >
<property name= "Jdbcurl" value= "${jdbc.url}" ></property>
<property name= "Driverclass" value= "${jdbc.driver}" ></property>
<property name= "user" value= "${jdbc.username}" ></property>
<property name= "Password" value= "${jdbc.password}" ></property>
</bean>
</beans>
Four, implement bean design
1. Create a package for the entity bean in the SRC directory
Package Name: Cn.jbit.spring101306.domain
2. Create an entity bean under a package
public class Temp {
Private Integer tempid;
Private String tempname;
Omit Get and set
}
V. Design the DAO layer
1. Create a DAO layer package in the SRC directory
Package Name: Cn.jbit.spring101306.dao
2. Create the DAO layer's interface and implementation class under the package
1) interface Design
Interface Name: Itempdao.java
Interface content:
Public interface Itempdao {
Public Temp FindByID (int id);
}
2) interface Implementation class design
Implementation class Name: Tempdaoimpl.java
Implementing the class content:
public class Tempdaoimpl extends Jdbcdaosupport implements Itempdao {
@Override
Public Temp FindByID (int id) {
Temp temp = This.getjdbctemplate (). queryForObject ("SELECT * from temp where tid =?", new Rowmapper<temp> () {
@Override
Public Temp Maprow (ResultSet rs, int arg1) throws SQLException {
Temp TEMP = new temp ();
Temp.settempid (Rs.getint ("Tid"));
Temp.settempname (rs.getstring ("Tname"));
return temp;
}
},ID);
return temp;
}
}
Vi. Configuring DAO in the core configuration file
<!--Configuring DAO--
<bean id= "Tempdao" class= "Cn.jbit.spring101306.dao.TempDaoImpl" >
<!--injected with the JdbcTemplate-
<property name= "DataSource" ref= "DataSource" ></property>
</bean>
Seven, testing
1. Create the test directory on the project
/test
2. Creating a test package under the test directory
Package Name: Cn.jbit.spring101301.dao
3. Create a test class under a test package
Test class Name: Jdbctemplatedemo.java
Test the contents of the class:
public class Jdbctemplatedemo {
/**
* Use spring JdbcTemplate to query by ID
*/
@Test
public void Testjdbctemplatefindbyid () {
ApplicationContext context = new Classpathxmlapplicationcontext ("Classpath:applicationContext.xml");
Itempdao Tempdao = (Itempdao) context.getbean ("Tempdao");
Temp TEMP = Tempdao.findbyid (3);
System.out.println (Temp.gettempid () + "------->" +temp.gettempname ());
}
}

This article is from the "Yan" blog, please be sure to keep this source http://suyanzhu.blog.51cto.com/8050189/1563390

spring-query by ID using JdbcTemplate implementation

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.