9.SpringJDBC template class

Source: Internet
Author: User

1. Spring框架中提供了很多持久层的模板类来简化编程,使用模板类编写程序会变的简单2. 提供了JDBC模板,Spring框架提供的    * JdbcTemplate类3. Spring框架可以整合Hibernate框架,也提供了模板类    * HibernateTemplate类
快速入门
1. 步骤一:创建数据库的表结构
CREATE database spring_day03;    Use spring_day03;    CREATE TABLE T_account (        int  primary key auto_increment,        name varchar),         double);
2.引入开发的JAR包    * 先引入IOC基本的6个jar包(4个核心+2个日志)    * 再引入Spring-aop的jar包(1个AOP)    * 最后引入JDBC模板需要的jar包        * MySQL数据库的驱动包        * Spring-jdbc.jar        * Spring-tx.jar
3. 在配置文件中配置连接池和模板类,并扫描包
<!--spring comes with a connection pool -     <BeanID= "DataSource"class= "Org.springframework.jdbc.datasource.DriverManagerDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>        < Propertyname= "url"value= "Jdbc:mysql:///spring_day03"/>        < Propertyname= "username"value= "root"/>        < Propertyname= "Password"value= "Toor"/>    </Bean>
    <!---    < id = "JdbcTemplate" class  = "org.springframework.jdbc.core.JdbcTemplate">        <  name = "DataSource" ref = "DataSource" />    </ Bean >
    <!---    <base-package = "Com.spring.demo1"  />

4. Testing

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (value= "Classpath:applicationContext.xml") Public classDemo1 {//template class@Resource (name= "JdbcTemplate")    PrivateJdbcTemplate JdbcTemplate; /*** Insert Operation*/@Test Public voidm01 () {jdbctemplate.update ("INSERT into t_account values (null,?,?)", "Test 3", 10000); }}

Selection of connection pools

1.DBCP Connection Pool

先引入DBCP的2个jar包        * com.springsource.org.apache.commons.dbcp-1.2.2.osgi.jar        * com.springsource.org.apache.commons.pool-1.5.3.jar
    <!--DBCP Connection Pool -    <BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource">        < Propertyname= "Driverclassname"value= "Com.mysql.jdbc.Driver"/>        < Propertyname= "url"value= "Jdbc:mysql:///spring_day03"/>        < Propertyname= "username"value= "root"/>        < Propertyname= "Password"value= "Toor"/>    </Bean> 

2.C3P0 Connection Pool

先引入C3P0的jar包        * com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
    <!--c3p0 Connection Pool -    <BeanID= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource">        < Propertyname= "Driverclass"value= "Com.mysql.jdbc.Driver"/>        < Propertyname= "Jdbcurl"value= "Jdbc:mysql:///spring_day03"/>        < Propertyname= "User"value= "root"/>        < Propertyname= "Password"value= "Toor"/>    </Bean>

Simple operation of the JDBC template class

@RunWith (Springjunit4classrunner.class) @ContextConfiguration (value= "Classpath:applicationContext.xml") Public classDemo1 {//template class@Resource (name= "JdbcTemplate")    PrivateJdbcTemplate JdbcTemplate; /*** Insert Operation*/@Test Public voidm01 () {jdbctemplate.update ("INSERT into t_account values (null,?,?)", "Test 3", 10000); }        /*** Update Operation*/@Test Public voidm02 () {jdbctemplate.update ("Update t_account set name =?" WHERE id =? "," Test 3 modified 10 ", 4); }        /*** Delete Operation*/@Test Public voidM03 () {jdbctemplate.update ("Delete from T_account where id=?", 3); }        /*** Query a record*/@Test Public voidm04 () { account account= Jdbctemplate.queryforobject ("select * from T_account where id=?",NewBeanmapper (), 1);    SYSTEM.OUT.PRINTLN (account); }        /*** Query multiple records*/@Test Public voidm05 () {List<Account> list = Jdbctemplate.query ("SELECT * from T_account",NewBeanmapper ());  for(account account:list) {System.out.println (account); }    }    }classBeanmapperImplementsRowmapper<account>{     PublicAccount Maprow (ResultSet RS,intARG1)throwsSQLException { account account=NewAccount (); Account.setid (Rs.getint ("id")); Account.setname (Rs.getstring ("Name")); Account.setmoney (Rs.getdouble ("Money")); returnAccount ; }}

9.SpringJDBC template class

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.