An example analysis of Spring Connection database method for Java development _java

Source: Internet
Author: User
Tags aop stub

This example describes the Java Development Spring Connection database method. Share to everyone for your reference, specific as follows:
Interface:

Package cn.com.service; 
Import java.util.List; 
Import Cn.com.bean.PersonBean; 
Public interface Personservice { 
 //Save the public 
 void Save (Personbean); 
 Update public 
 void update (Personbean person); 
 Gets the person public 
 Personbean Getperson (int id); 
 Public list<personbean> Getpersonbean (); 
 Delete Record public 
 void Delete (int personid); 
}

Person Bean class:

Package Cn.com.bean; 
public class Personbean { 
 private int id; 
 private String name; 
 Public Personbean (String name) { 
  This.name=name 
 } 
 public int getId () {return 
  ID; 
 } 
 public void setId (int id) { 
  this.id = ID; 
 } 
 Public String GetName () {return 
  name; 
 } 
 public void SetName (String name) { 
  this.name = name; 
 } 
}

Interface implementation:

Package Cn.com.service.impl; 
Import java.util.List; 
Import Javax.sql.DataSource; 
Import Org.springframework.jdbc.core.JdbcTemplate; 
Import Cn.com.bean.PersonBean; 
Import Cn.com.service.PersonService; 
 public class Personserviceimpl implements Personservice {private JdbcTemplate jdbctemplate; 
 public void Setdatasource (DataSource DataSource) {this.jdbctemplate = new JdbcTemplate (DataSource);  @Override public void Save (Personbean person) {//TODO auto-generated the method stub jdbctemplate.update ("Insert 
 into person (name) values (?), New Object[]{person.getname ()}, new Int[]{java.sql.types.varchar}); @Override public void Update (Personbean person) {//TODO auto-generated the method stub jdbctemplate.update ("Upda Te person set name=? Where id=? ", New Object[]{person.getname (), Person.getid ()}, new Int[]{java.sql.types.varchar,java.sql.types.integer 
 }); 
@Override public Personbean getperson (int id) {//TODO auto-generated method stub  Return (Personbean) jdbctemplate.queryforobject ("select * from person where id=?", New Object[]{id}, new Int[]{java. Sql. 
 Types.integer},new Personrowmapper ());  @SuppressWarnings ("unchecked") @Override public list<personbean> Getpersonbean () {//TODO auto-generated 
 Method stub return (list<personbean>) jdbctemplate.query ("SELECT * To Person", new Personrowmapper ()); @Override public void Delete (int personid) {//TODO auto-generated Method stub jdbctemplate.update ("Delete f 
 Rom person where id=? ", New Object[]{personid}, New Int[]{java.sql.types.integer});

 } 
}

RowMapper:

Package Cn.com.service.impl; 
Import Java.sql.ResultSet; 
Import java.sql.SQLException; 
Import Org.springframework.jdbc.core.RowMapper; 
Import Cn.com.bean.PersonBean; 
public class Personrowmapper implements RowMapper { 
 @Override 
 the public Object Maprow (ResultSet rs, int index) throw s SQLException { 
  //TODO auto-generated method stub 
  Personbean person =new personbean ("name"); C10/>person.setid (Rs.getint ("id")); 
  return person; 
 } 


Beans.xml Configuration

<?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" xmlns:tx= "Http://www.springframework.org/schema/tx" xsi: schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/ Spring-beans-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 "> <!--<context:property-placeholder location=" classpath:jdbc.properties "/>--&G 
    T <bean id= "DataSource" class= "Org.apache.commons.dbcp.BasicDataSource" Destroy-method= "Close" > <property name= "driverclassname" value= "Com.mysql.jdbc.Driver"/> <property-name= "u RL "value=" Jdbc:mysql://localhost:3306/wy "/> <property name=" username "value=" root "/> <!--Property Pool Initial value at startup--> <property name= "password" value= "123"/> <!--connection name= "InitialSize" value= "${initialsize" } "/>--> <property name= initialsize value=" 1 "/> <!--connection pool maximum--> <property name=" M  Axactive "value="/> <!--maximum idle value. After a peak time, the connection pool can slowly release a portion of the connection that has not been used, and has been reduced to maxidle until--> <property Name= "Maxidle" value= "2"/> <!--minimum idle value. When the number of idle connections is less than the threshold, the connection pool will advance to some connections to avoid the flood peak when it is too late to request--> <property name= " Minidle "value=" 1 "/> </bean> <bean id=" Txmanager "class=" Org.springframework.jdbc.datasource.DataSou Rcetransactionmanager "> <property name=" dataSource "ref=" DataSource "/> </bean> <tx:annota Tion-driven transaction-manager= "Txmanager"/> <bean id= "Personservice" class= "Cn.com.service.impl.PersonServiceImpl" > <prope 

 Rty name= "DataSource" ref= "DataSource" ></property> </bean> </beans>

Test class:

Package junit.test; 
Import static org.junit.assert.*; 
Import Org.junit.BeforeClass; 
Import Org.junit.Test; 
Import Org.springframework.context.ApplicationContext; 
Import Org.springframework.context.support.ClassPathXmlApplicationContext; 
Import Cn.com.bean.PersonBean; 
Import Cn.com.service.PersonService; 
 public class PersonTest2 {private static Personservice personservice; @BeforeClass public static void Setupbeforeclass () throws Exception {ApplicationContext act=new Classpathxmlapplicati 
 Oncontext ("Beans.xml"); 
 Personservice= (Personservice) Act.getbean ("Personservice"); 
 @Test public void Save () {Personservice.save (New Personbean ("Wyy")); 
 @Test public void Update () {Personbean Person=personservice.getperson (1); 
 Person.setname ("WY"); 
 Personservice.update (person); 
 @Test public void Getperson () {Personbean Person=personservice.getperson (1); 
 System.out.println (Person.getname ()); @Test public void Delete () {PERSONSERVICE.DElete (1);

 } 
}

Database:

CREATE TABLE 
CREATE table ' person ' ( 
 ' id ' int (one) not null auto_increment, 
 ' name ' varchar (ten) NOT NULL, 
 P Rimary KEY (' id ') 
) engine=innodb auto_increment=2 DEFAULT Charset=utf8

I hope this article will help you with Java programming.

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.