[Reserved] Spring JdbcTemplate Comments

About JdbcTemplate

  Spring in the operation of the database jdbc did a deep package, using the spring injection feature, you can register the DataSource into JdbcTemplate.

  JdbcTemplate located in. Its fully qualified name org.springframework.jdbc.core.JdbcTemplate. To use JdbcTemlate need a package that contains a bit Affairs and exception control

  

JdbcTemplate method provides the following five categories:

  • execute method: it can be used to execute any SQL statement, typically used to execute DDL statements;

  • update method and batchUpdate method: update methods for performing add, modify and delete statements; batchUpdate methods for performing batch related statements;

  • query methods and queryForXXX method: to perform a query related statements;

  • Method call: for executing stored procedures, functions related statements.

 

The following cases were analyzed

 

In the src attribute below to create a new configuration file

1 jdbc.user=root
2 jdbc.password=123456
3 jdbc.driverClass=com.mysql.jdbc.Driver
4 jdbc.jdbcUrl=jdbc\:mysql\:///test

  We usually configuration information into a single database file, so also in order to facilitate later maintenance

 

Spring configuration profiles applicationContext.xml

Copy the code
 1 <context:property-placeholder location="classpath:db.properties"/>
 2 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
 3     <property name="user" value="${jdbc.user}"></property>
 4     <property name="password" value="${jdbc.password}"></property>
 5     <property name="driverClass" value="${jdbc.driverClass}"></property>
 6     <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
 7 </bean>
 8 
 9 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
10     <property name="dataSource" ref="dataSource"></property>
11 </bean>
Copy the code

  The first line of code: db.properties to read the data file.

  Second line: to configure a data source, where the data from the implementation class property of a class of C3P0. Wherein the value of the attribute is derived from db.properties

  Ninth line of code: Configure a JdbcTemplate example, data source and inject a dataSource

 

Test code

 

1, update () method

 

a, by inserting data update

Copy the code
1 //启动IoC容器
2 ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext.xml");
3 //获取IoC容器中JdbcTemplate实例
4 JdbcTemplate jdbcTemplate=(JdbcTemplate) ctx.getBean("jdbcTemplate");
5 String sql="insert into user (name,deptid) values (?,?)";
6 int count= jdbcTemplate.update(sql, new Object[]{"caoyc",3});
7 System.out.println(count);
Copy the code

  Here update method, the second parameter may be a variable parameter. Can be seen in the database, the data to be inserted correctly

 

b, modify the data update

1 String sql="update user set name=?,deptid=? where id=?";
2 jdbcTemplate.update(sql,new Object[]{"zhh",5,51});

 

 c, delete data update

1 String sql="delete from user where id=?";
2 jdbcTemplate.update(sql,51);

 

2, batchUpdate () bulk insert, update and delete methods

a, bulk insert

Copy the code
1 String sql="insert into user (name,deptid) values (?,?)";
2 
3 List<Object[]> batchArgs=new ArrayList<Object[]>();
4 batchArgs.add(new Object[]{"caoyc",6});
5 batchArgs.add(new Object[]{"zhh",8});
6 batchArgs.add(new Object[]{"cjx",8});
7 
8 jdbcTemplate.batchUpdate(sql, batchArgs);
Copy the code

  The second parameter is a method batchUpdate elements Object [] List of the collection array type

 

3, to read data from the data entity object

  First set a User entity class

 

Copy the code
 1 package com.proc;
 2 
 3 public class User {
 4     private Integer id;
 5     private String name;
 6     private Integer deptid;
 7     public Integer getId() {
 8         return id;
 9     }
10     public void setId(Integer id) {
11         this.id = id;
12     }
13     public String getName() {
14         return name;
15     }
16     public void setName(String name) {
17         this.name = name;
18     }
19     public Integer getDeptid() {
20         return deptid;
21     }
22     public void setDeptid(Integer deptid) {
23         this.deptid = deptid;
24     }
25 
26     public String toString() {
27         return "User [id=" + id + ", name=" + name + ", deptid=" + deptid + "]";
28     }
29 }
Copy the code

 

 

a, reading a single object

1 String sql="select id,name,deptid from user where id=?";
2 
3 RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
4 User user= jdbcTemplate.queryForObject(sql, rowMapper,52);
5 System.out.println(user);

  Output:

  User [id=52, name=caoyc, deptid=6]

 

[Note]: 1, BeanProperytRowMapper requirements sql query data out of the column and entity attributes required correspondence. If not specify the property name and data in sql statements need to re-use as an alias

     2, the object can not be obtained using JdbcTemplate associated objects

 

b, reading the plurality of objects

Copy the code
1 String sql="select id,name,deptid from user";
2 
3 RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
4 List<User> users= jdbcTemplate.query(sql, rowMapper);
5 for (User user : users) {
6     System.out.println(user);
7 }
Copy the code

 

Output

...

User [id=49, name=姓名49, deptid=5]
User [id=50, name=姓名50, deptid=8]
User [id=52, name=caoyc, deptid=6]
User [id=53, name=zhh, deptid=8]
User [id=54, name=cjx, deptid=8]

---

 

c, obtaining a record or a column count, avg, sum and other unique function return value

1 String sql="select count(*) from user";
2 
3 int count= jdbcTemplate.queryForObject(sql, Integer.class);
4 System.out.println(count);

 

 

In the actual development of how you can use

UserDao.java

Copy the code
 1 package com.proc;
 2 
 3 import org.springframework.beans.factory.annotation.Autowired;
 4 import org.springframework.jdbc.core.BeanPropertyRowMapper;
 5 import org.springframework.jdbc.core.JdbcTemplate;
 6 import org.springframework.jdbc.core.RowMapper;
 7 import org.springframework.stereotype.Repository;
 8 
 9 @Repository
10 public class UserDao {
11 
12     @Autowired
13     private JdbcTemplate jdbcTemplate;
14     
15     public User get(int id){
16         String sql="select id,name,deptid from user where id=?";
17         RowMapper<User> rowMapper=new BeanPropertyRowMapper<User>(User.class);
18         return jdbcTemplate.queryForObject(sql, rowMapper,id);
19     }
20 }
Copy the code

 

xml configuration:

Copy the code
 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xmlns:context="http://www.springframework.org/schema/context"
 6     xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
 7         http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
 9 
10 <context:component-scan base-package="com.proc"></context:component-scan>
11 <context:property-placeholder location="classpath:db.properties"/>
12 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
13     <property name="user" value="${jdbc.user}"></property>
14     <property name="password" value="${jdbc.password}"></property>
15     <property name="driverClass" value="${jdbc.driverClass}"></property>
16     <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
17 </bean>
18 
19 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
20     <property name="dataSource" ref="dataSource"></property>
21 </bean>
22 </beans>
Copy the code

 

Code Testing

1 UserDao userDao=(UserDao) ctx.getBean("userDao");
2 System.out.println(userDao.get(53));

 original:

https://www.cnblogs.com/caoyc/p/5630622.html

Guess you like

Origin www.cnblogs.com/appium/p/11301885.html