Spring Framework provides for database application development in the JDBCTemplate class, which is the core of the Spring JDBC support, which provides support for all database operations function.
Spring JDBC framework provides support consists of four packets, namely the Core (core package), Object (Object package), the dataSource (packet data source) and a support (Support Package), org.springframework.jdbc.core.JdbcTemplate class it is included in the core package. Spring JDBC the core, JdbcTemplate class contains methods substantially all database operations.
JdbcTemplate class inherits from the abstract class JdbcAccessor, while achieving a JdbcOperations interface. Its direct parent JdbcAccessor provides some common attributes used to access the database for subclasses are detailed below.
1)DataSource
Its main function is to obtain a database connection, the specific implementation, can have introduced support for database connection pool and distributed transactions, it can be used as a standard interface to access database resources.
2)SQLExceptionTranslator
org.springframework.jdbc.support.SQLExceptionTranslator interface is responsible for the conduct SQLException translation work. Or a method of setting necessary by the acquisition SQLExceptionTranslator can be made when needed to process SQLException JdbcTemplate, entrusted SQLExceptionTranslator implementation class to complete work related to the translation.
JdbcOperations interface defines a set of operations that can be used in the JdbcTemplate including add, modify, query and delete operations.
Spring JDBC information is done in the configuration file in the Spring, the configuration template is shown below:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http:/www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! - Configure Data Source -> <bean id="dataSource" class="org.springframework.jdbc.dataSource.DriverManagerDataSource"> <-! Database-driven -> <property name="driverClassName" value="com.mysql.jdbc.Driver" /> <-! Connection to the database url -> <property name= "url" value="jdbc:mysql://localhost/spring" /> <! - username connection to the database -> <property name="username" value="root" /> <! - password to connect to the database -> <property name="password" value="root" /> </bean> <! - Configure JDBC Templates -> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.jdbcTemplate"> <! - default must use a data source -> <property name="dataSource" ref="dataSource"/> </bean> <! - injection type configuration -> <bean id="xxx" class="xxx"> <property name="jdbcTemplate" ref="jdbcTemplate"/> </bean> ... </beans>
In the above code, defines three Bean, are Bean dataSource, jdbcTemplate classes and be injected. Wherein dataSource corresponds DriverManagerDataSource class configuration for the data source; JdbcTemplate corresponding to the jdbcTemplate class that defines the configuration of JdbcTemplate.
In dataSource, the four properties defined in connection to the database, as shown in Table 1.
Property name | meaning |
---|---|
driverClassName | The driver name is used, the corresponding Driver class driver JAR package |
url | Address where the data source |
username | Username to access the database |
password | Password to access the database |
Table 1 attribute values need to be set accordingly depending on the database machine type or configuration. If a different type of database, you need to change the name of the drive. If the database is not local, it is necessary to replace the corresponding host localhost IP.
When defining jdbcTemplate, dataSource jdbcTemplate injected in need. In other classes jdbcTemplate to be used, the need to use implanted jdbcTemplate class (class typically injected dao).
In JdbcTemplate class, there is provided a method of updating a large number of queries and databases, such as query (), update () and the like.
1.1.1 Import jar package
1.1.2Ctiy entity
public class City implements Serializable { private Integer cid; private String cname; private Integer pid; public Integer getCid() { return cid; } public void setCid(Integer cid) { this.cid = cid; } public String getCname() { return cname; } public void setCname(String cname) { this.cname = cname; } public Integer getPid() { return pid; } public void setPid(Integer pid) { this.pid = pid; } }
Dao layer
public interface AccountDao { // remittance public void OUT (outUser String, Integer Money); // receivables public void in (inUser String, Integer Money); // query method public List <City> City (); // delete method public Integer pleasure (String out); // method of increasing public Integer add(City city); }
Dao layer implementation class
@Repository public class AccountDaoImpl implements AccountDao { @Resource private JdbcTemplate jdbcTemplate; //汇款的实现方法 @Override public void out(String outUser, Integer money) { this.jdbcTemplate.update("update city set pid=pid-? where cid=?",money,outUser); } // implementation receivables @Override public void in (inUser String, Integer Money) { the this .jdbcTemplate.update ( " Update City the SET pid = pid + the WHERE cid =?? " , Money, inUser); } RowMapper<City> rowMapper=new BeanPropertyRowMapper<>(City.class); @Override public List<City> city() { return jdbcTemplate.query("select * from city",rowMapper); } @Override public Integer pleasure (String from) { return jdbcTemplate.update("delete from city where cid=?",de); } @Override public Integer add(City city){ String xml="insert INTO city(cid,cname,pid) values (?,?,?)"; Object[] objects={city.getCid(),city.getCname(),city.getPid()}; return jdbcTemplate.update(xml,objects); } }
Service Layer
public interface the AccountService { // transfer public void transfet (outUser String, String inUser, Money Integer); // query method public List <City> City (); // delete method public Integer pleasure (String out); // method of increasing public Integer add(City city); }
Service implementation class
@Service("accountService") public class AccountServiceImpl implements AccountService { @Resource private AccountDao accountDao; public void setAccountDao(AccountDao accountDao) { this.accountDao = accountDao; } @Override public void transfet(String outUser, String inUser, Integer money) { this.accountDao.out(outUser,money); this.accountDao.in(inUser, money); } @Override public List<City> city() { return accountDao.city(); } @Override public Integer pleasure (String from) { return accountDao.delect(de); } @Override public Integer add(City city) { return accountDao.add(city); } }
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <! - is the root node of our beans -> <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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <! - scan package -> <context:component-scan base-package="com.mengma"/> <! - Load properties file -> <context:property-placeholder location="classpath:c3p0-db.properties"/> <! - configuration data sources. Reads the properties file information -> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <property name="driverClass" value="${jdbc.driverClass}" /> <property name="jdbcUrl" value="${jdbc.jdbcUrl}" /> <property name="user" value="${jdbc.user}" /> <property name="password" value="${jdbc.password}" /> </bean> <! - Configuration dao -> <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource" ref="dataSource" /> </bean> </beans>
c3p0-db.properties
jdbc.driverClass =com.mysql.jdbc.Driver jdbc.jdbcUrl = jdbc:mysql://localhost:3306/test?serverTimezone=UTC jdbc.user = root jdbc.password = root
test
@Test public void shouldAnswerWithTrue() { // get Spring container, and the operation String XMLPath = " the applicationContext.xml " ; ApplicationContext applicationContext = new ClassPathXmlApplicationContext(xmlPath); AccountService accountService = (AccountService) applicationContext.getBean("accountService"); //修改操作 accountService.transfet("130100", "130400", 100); //查询操作 List<City> cityList=accountService.city(); for (City city:cityList){ System.out.println(city.getCname()); } // delete accountService.delect ( 130000 ); // add an action City City = new new City (); city.setCid(130000); city.setCname("超哥"); city.setPid(12314); Integer add = accountService.add(city); }
The above code is the way to achieve comment
structure
I hope you find help, thank you