Spring5 framework [JdbcTemplate]

JdbcTemplate (concept and preparation)

1. What is JdbcTemplate

(1) The Spring framework encapsulates JDBC and uses JdbcTemplate to facilitate database operations

2. Preparations
(1) Import related jar packages
(2) Configure database connection pool in spring configuration file

 <! -- 数据库连接池 -- > 
 <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"  destroy-method="close">   
 <property name="url" value="jdbc:mysql:///user_db" />  
 <property name="username" value="root" /> 
 <property name="password" value="root" />  
 <property name="driverClassName" value="com.mysql.jdbc.Driver" /> </bean> 

(3) Configure JdbcTemplate object and inject DataSource


 <! -- JdbcTemplate 对象 -- >
 <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
 <! -- 注入 dataSource -- >  
 <property name="dataSource" ref="dataSource"></property> </bean> 

(4) Create service class, create dao class, and inject jdbcTemplate object in dao* configuration file
<! – component scanning –>

<context:component-scan base-package="com.atguigu">
</context:component-scan> 
-Service
@Service
public class BookService {
    
       
 // 注入 dao
      @Autowired  
         private BookDao bookDao; 
         } 
-dao
@Repository
public class BookDaoImpl implements  BookDao {
    
       
  // 注入 JdbcTemplate  
     @Autowired   
       private JdbcTemplate jdbcTemplate; 
       } 


JdbcTemplate operation database (add)

1. Create entity classes corresponding to the database

public class Book{
    
    
   private String userId;
   private String username;
   private String ustatus;

   public String getUserId() {
    
    
       return userId;
   }

   public void setUserId(String userId) {
    
    
       this.userId = userId;
   }

   public String getUsername() {
    
    
       return username;
   }

   public void setUsername(String username) {
    
    
       this.username = username;
   }

   public String getUstatus() {
    
    
       return ustatus;
   }

   public void setUstatus(String ustatus) {
    
    
       this.ustatus = ustatus;
   }
}

2. Write service and dao
(1) add database in dao
(2) call update method in JdbcTemplate object to add
service and dao (1) add database in dao (2) call update method in JdbcTemplate object to add operating

update(String sql, Object ...  args)
  • There are two parameters
  • The first parameter: sql statement
  • The second parameter: variable parameters, set the value of the sql statement
@Repository
public class BookDaoImpl implements BookDao{
    
    

    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    //添加的方法
    @Override
    public void add(Book book) {
    
    
        //1.创建sql语句
        String sql = "insert into t_book values(?,?,?)";
        //2.调用方法实现
        Object[] args = {
    
     book.getUserId(), book.getUsername(), book.getUstatus()};
        int update = jdbcTemplate.update(sql,args);//(返回影响行数,成功添加了几个数据)
        System.out.println(update);

    }
}

Continually updated…

Guess you like

Origin blog.csdn.net/SwaeLeeUknow/article/details/109365601
Recommended