Configuration through the configuration file XML method
You can use a very concise service class
The UserService class code is as follows:
Package Com.swift; Public class userservice { private Userdao Userdao; Public void Setuserdao (Userdao userdao) { this. Userdao = Userdao; } Public Boolean Add () { return userdao.add (); }}
UserService to call the method Add () that connects to the database in Userdao, you need a Userdao object that injects the object through the configuration file XML and requires Setuserdao () to match
The same Userdao class is also very concise
PackageCom.swift;Importorg.springframework.jdbc.core.JdbcTemplate; Public classUserdao {PrivateJdbcTemplate JdbcTemplate; Public voidsetjdbctemplate (JdbcTemplate jdbctemplate) { This. JdbcTemplate =JdbcTemplate; } Public BooleanAdd () {//combopooleddatasource datasource=new Combopooleddatasource ();//Datasource.setdriverclass ("Com.mysql.jdbc.Driver");//Datasource.setjdbcurl ("Jdbc:mysql://localhost:3306/sw_database ");//datasource.setuser ("root");//Datasource.setpassword ("root");String SQL= "INSERT into Sw_user (Username,password) VALUES (?,?)"; intCount=jdbctemplate.update (SQL, "Bullet", "battle-Man song"); if(count==1) { return true; } return false; }}
To use the JdbcTemplate object in Userdao, this object is also injected through the config file XML, which needs to be setjdbctemplate () to match
The JdbcTemplate object, in turn, needs to inject datasource objects into the configuration via XML.
DataSource objects are obtained through the C3P0 connection pool class and need to import the jar package support provided by the previous essay
XML configuration File Code:
<?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"xsi:schemalocation="http//Www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp//Www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp//WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOPhttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--c3p0 Connection pool Get dataSource--<bean id= "DataSource"class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "driverclass" value= "Com.mysql.jdbc.Driver" ></property> <property name= "Jdbcurl" value= "Jdbc:mysql://localhost:3306/sw_database" ></ property> <property name= "user" value= "root" ></property> <property name= "password" value= "root" ;</property> </bean> <bean id= "JdbcTemplate"class= "Org.springframework.jdbc.core.JdbcTemplate" > <property name= "dataSource" ref= "DataSource" ></ property> </bean> <bean id= "Userdao"class= "Com.swift.UserDao" > <property name= "jdbctemplate" ref= "JdbcTemplate" ></property> </bean> <bean id= "UserService"class= "Com.swift.UserService" > <property name= "Userdao" ref= "Userdao" ></property> </bean> </be Ans>
In the test class, you can get all the XML configuration file objects you want.
The code is as follows:
PackageCom.swift;Importjava.io.IOException;Importjavax.servlet.ServletException;ImportJavax.servlet.annotation.WebServlet;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.context.ApplicationContext;ImportOrg.springframework.context.support.ClassPathXmlApplicationContext; @WebServlet ("/test") Public classServlettestextendsHttpServlet {Private Static Final LongSerialversionuid = 1L; Publicservlettest () {Super(); } protected voidDoget (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {response.setcharacterencoding ("Utf-8"); Response.setcontenttype ("Text/html;charset=utf-8"); Response.getwriter (). Append ("Served at:"). Append (Request.getcontextpath ()); //queryForObject method for using JdbctemplatApplicationContext context=NewClasspathxmlapplicationcontext ("C3p0.xml"); UserService UserService= (UserService) context.getbean ("UserService"); if(Userservice.add ()) {Response.getwriter (). Append ("Added successfully! "); }Else{response.getwriter (). Append ("Add failed! "); } } protected voidDoPost (HttpServletRequest request, httpservletresponse response)throwsservletexception, IOException {doget (request, response); }}
Results in Browser:
Results in database:
The configuration of C3P0 connection pool in spring and the use of jdbctemplate injection of various required objects through an XML configuration file to complete the database Add () method