[Liu Yang java]_spring support for DAO _ 10th

Source: Internet
Author: User

The good spring framework is that when MVC is developed, it can support JDBC as well as the mainstream ORM framework (Hibernate, MyBatis) technology once it needs to operate on the underlying database.

The emphasis is first on the spring-to-JDBC support. A template JdbcTemplateis provided in spring for JDBC support, which encapsulates many of the methods for JDBC operations and eliminates the vulnerability caused by ignoring resource releases

1. The jar that spring needs to import for JDBC Operations (development environment configuration)

    • Common-dbcp.jar: Third-party packages that provide data sources
    • Common-pool.jar:common-dbcp.jar's Dependency Package
    • Spring-jdbc-4.0.3.jar: Completing the JDBC operation
    • SPRING-TX-4.0.3.JAR:JDBC transaction exception-related classes
    • Msyql-connector.jar:mysql Driver Pack

2. Let's take a look at a program that uses spring JDBC to write steps

    • Configure a data source, the configuration of a data source spring relies on third-party two data source implementation packages: APACHE-DBCP and C3P0
<id= "DataSource"  class= "Org.apache.commons.dbcp.BasicDataSource "    destroy-method=" Close "     p:driverclassname=" Com.mysql.jdbc.Driver "    p:url=" Jdbc:mysql://localhost:3306/sampledb "      P:username = "root"     p:password= "1234"/>

Note: Basicdatasource provides a close method to close the data source, so we can use the Destory-method property to perform a close operation

    • DBCP parameter Description
      • InitialSize: Initializes the connection, which is the number of initial connections created when the connection pool starts. Default is 0
      • Maxactive: The maximum active connection, that is, the maximum number of active connections The connection pool can allocate at the same time, and no limit if set to negative. Default is 8
      • Maxidle: The maximum idle connection, that is, the maximum number of connections that the connection pool allows to remain idle, the idle connection that is exceeded is freed, and is not limited if set to negative numbers. Default is 8
      • Minidle: Minimum idle connection, that is, the minimum number of connections that the connection pool allows to remain idle, below which a new connection is created and not created if set to 0. Default is 0
      • Maxwait: Maximum wait time, that is, when there are no available connections, the maximum time (in milliseconds) that the connection pool waits for a connection to be returned, and throws an exception when the time is exceeded. If set to-1 means no limit. Default is-1
    • Configuring the data source with a property file
      • Create a jdbc.properites file first
Jdbc.driverclassname=oracle.jdbc.driver.oracledriverjdbc.url=jdbc:oracle:thin: @localhost: 1521:XEjdbc.username= Team3jdbc.password=team3
      • The following code can be written in the spring configuration file
<Context:property-placeholder Location= "Classpath:jdbc.properties"><BeanID= "DataSource"class= "Org.apache.commons.dbcp.BasicDataSource"Destroy-method= "Close">    < Propertyname= "Driverclassname"value= "${jdbc.driverclassname}"/>    < Propertyname= "url"value= "${jdbc.url}"/>    < Propertyname= "username"value= "${jdbc.username}"/>    < Propertyname= "Password"value= "${jdbc.password}"/></Bean>
    • JdbcTemplate template application, in general, we use jdbctemplate,jdbctemplate in the DAO class after the XML configuration file is configured and then injected directly into the DAO
<id= "template"  class= " Org.springframework.jdbc.core.JdbcTemplate "  p:datasource-ref=" DataSource "></  Bean>
    • DAO Layer Code implementation
 PackageCom.gxaedu.dao;Importjava.util.List;ImportJava.util.Map;Importorg.springframework.jdbc.core.JdbcTemplate; @Repository//declaring a DAO Public classUsersdao {@Autowire//inject JdbcTemplatejdbctemplate template;  Public voidQuery () {//User's queryString sql = "SELECT * from Users"; List<Map> list =template.queryforlist (SQL); System.out.println ("List =" +list); }         PublicMap Query (string username, string userpwd) {//How to get user information or use spring's JDBCString sql = "SELECT * from Users where user_name=? anduser_pwd =?"; object[] obj={username, userpwd}; returntemplate.queryformap (sql, obj); }}

[Liu Yang java]_spring support for DAO _ 10th

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.