Configuration through the configuration file XML method
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 invoke the method Add () for connecting 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
Package com.swift;
Import org.springframework.jdbc.core.JdbcTemplate;
public class Userdao {
private jdbctemplate jdbctemplate;
public void Setjdbctemplate (JdbcTemplate jdbctemplate) {
this.jdbctemplate = jdbctemplate;
}
public boolean Add () {
// 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 (?,?)";
int count=jdbctemplate.update (SQL, "Bullet", "War hero Song");
if (count==1) {return
true;
}
return false;
}
}
The object in Userdao to use JdbcTemplate, which is also injected through the configuration file XML, requires Setjdbctemplate () to match
The JdbcTemplate object also needs to inject datasource objects into the configuration via XML
The DataSource object is obtained through the C3P0 connection pool class and needs to be imported into the jar bundle support provided by the previous essay
XML configuration file code:
<?xml version= "1.0" encoding= "UTF-8"?> <beans "xmlns=" 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/sche Ma/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 http://www.springframework.org/schema/a Op http://www.springframework.org/schema/aop/spring-aop.xsd "> <!--c3p0 connection pool gets datasource--> <bean Id= "DataSource" class= "Com.mchange.v2.c3p0.ComboPooledDataSource" > <property name= "Driverclass" 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" > & Lt;property name= "JdbcTemplate" ref= "JdbcTemplate" ></property> </bean> <bean id= "Userservic"
E "class=" Com.swift.UserService "> <property name=" Userdao "ref=" Userdao "></property> </bean> </beans>
In the test class, you can get the XML configuration file object to handle all the
The code is as follows:
Package com.swift;
Import java.io.IOException;
Import javax.servlet.ServletException;
Import Javax.servlet.annotation.WebServlet;
Import Javax.servlet.http.HttpServlet;
Import Javax.servlet.http.HttpServletRequest;
Import Javax.servlet.http.HttpServletResponse;
Import Org.springframework.context.ApplicationContext;
Import Org.springframework.context.support.ClassPathXmlApplicationContext;
@WebServlet ("/test") public class Servlettest extends HttpServlet {private static final long serialversionuid = 1L;
Public Servlettest () {super (); } protected void Doget (HttpServletRequest request, httpservletresponse response) throws Servletexception, IOException
{response.setcharacterencoding ("utf-8");
Response.setcontenttype ("Text/html;charset=utf-8");
Response.getwriter (). Append ("served at:"). Append (Request.getcontextpath ()); queryForObject method using Jdbctemplat applicationcontext context=new ClasspathxmlapplicatiOncontext ("C3p0.xml");
UserService userservice= (UserService) Context.getbean ("UserService"); if (Userservice.add ()) {Response.getwriter (). Append ("Add success.")
"); }else {response.getwriter (). Append ("Add failed.
"); } protected void DoPost (HttpServletRequest request, httpservletresponse response) throws Servletexception, Ioex
ception {doget (request, response); }
}
Results in Browser:
Results in database: