Extract the spring database connection part to the Project

Source: Internet
Author: User

Using spring to manage the database of a project is often easier to manage than writing a connection by yourself. The procedure is also relatively simple. create the conf and lib directories under the root directory of the project, import the spring-related packages coop to lib, and create two jdbc files. properties, bean. xml jdbc. properties: [html] driverClassName = org. gjt. mm. mysql. driver url = jdbc: mysql: // localhost: 3306/test? UseUnicode = true & characterEncoding = UTF-8 username = root password = root initialSize = 1 maxActive = 300 maxIdle = 2 minIdle = 1 bean. xml: [html] <? 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" xmlns: aop = "http://www.springframework.org/schema/aop" xmlns: tx = "http://www.springframework.org/schema/tx" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-bean S-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd "> <! -- Read the jdbc. properties configuration file location = "classpath: jdbc. properties" --> <context: property-placeholder location = "classpath: jdbc. properties"/> <! -- Configure the data source --> <bean id = "dataSource" class = "org. apache. commons. dbcp. basicDataSource "destroy-method =" close "> <property name =" driverClassName "value =" $ {driverClassName} "/> <property name =" url "value =" $ {url} "/> <property name =" username "value =" $ {username} "/> <property name =" password "value =" $ {password} "/> <! -- Initial value when the connection pool starts --> <property name = "initialSize" value = "$ {initialSize}"/> <! -- Maximum value of the connection pool --> <property name = "maxActive" value = "$ {maxActive}"/> <! -- Maximum idle value. after a peak time, the connection pool can slowly release a portion of connections that are no longer in use, until maxIdle is reached --> <property name = "maxIdle" value = "$ {maxIdle}"/> <! -- Minimum idle value. when the number of idle connections is less than the threshold value, the connection pool will pre-apply for some connections, so that you do not have time to apply when the peak traffic arrives --> <property name = "minIdle" value = "$ {minIdle}"/> </bean> <! -- Use annotations to configure transactions. For the Transaction Manager of the data source, inject the defined data source into the attribute dataSource of the cetcetransactionmanager class --> <bean id = "txManager" class = "org. springframework. jdbc. datasource. dataSourceTransactionManager "> <property name =" dataSource "ref =" dataSource "/> </bean> <! -- Introduce namespace: 1. xmlns: tx = "http://www.springframework.org/schema/tx 2. http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd uses the @ Transaction annotation to use the transaction manager --> <tx: annotation-driven Transaction-manager = "txManager"/> <! -- Configure the business bean: PersonServiceBean --> <bean id = "playerService" class = "com. spring. db. PlayerServiceBean"> <! -- Inject data sources to the attribute dataSource --> <property name = "dataSource" ref = "dataSource"> </property> </bean> </beans> jdbc needless to say, bean. xml is the main configuration file. There are two main parts (1) <bean id = "dataSource"...> to configure the data source, from jdbc. properties. If there are multiple data sources, you only need to copy this part (2) <! -- Configure service bean: PersonServiceBean -->, which is divided into configuring your own service, for example, a <bean id = "playerService" class = "com. spring. db. playerServiceBean "> <! -- Inject a data source to the attribute dataSource --> <property name = "dataSource" ref = "dataSource"> </property> </bean> id is a unique identifier, and class is a class path, name = "dataSource" is the attribute in the class, and ref is the id of the data source to be used. after the configuration file is written, you can write the database read and write parts, mainly managed in DBServer. in java, [java] package com. spring. db; import org. springframework. context. applicationContext; import org. springframework. context. support. classPathXmlApplicationContext; public class DBServer {private PlayerService PlayerService; private static DBServer instance = null; public static DBServer getInstance () {if (instance = null) {instance = new DBServer ();} return instance;} public DBServer () {ApplicationContext act = new ClassPathXmlApplicationContext ("bean. xml "); playerService = (PlayerService) act. getBean ("playerService");} public PlayerService getPlayerService () {return playerService;} public voi D setPlayerService (PlayerService playerService) {this. playerService = playerService;} is initialized directly in the constructor for convenience, and the code is easy to understand playerService = (PlayerService) act. getBean ("playerService"); is the corresponding bean. <bean id = "dataSource"...> after adding multiple services, PlayerService and PlayerServiceBean are called interfaces and implementation classes respectively. You can define the required methods in the service and implement the corresponding implementation classes. 3. after completing the preceding steps, write a test class test and create a user (id, name) table in the database. The test code is as follows: [java] public class TestSpring {public static void main (String [] args) {Player p1 = new Player (1, "spring jdbc"); // add www.2cto.com DBServer. getInstance (). getPlayerService (). add (p1); // query Player p2 = DBServer. getInstance (). getPlayerService (). getPlayer (1); System. out. println ("id =" + p2.getId () + "name =" + p2.getName ();} run the following command: id = 1 name = spring jdbc

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.