Spring——JdbcTemplate

A .JdbcTemplate description:

  In order to make easier to use JDBC, the Spring JDBCAPI defined on an abstraction layer, in order to establish a framework for access JDBC , Spring Boot Spring Data-JPA.

  SpringJDBC core framework, JDBC template is designed to provide a method templates for different types of JDBC operations . Each template methods can control the entire process, and allows a specific task in the process.

In this way, while retaining flexibility as possible, to minimize the workload of database access.

Two, JdbcTemplate method introduced

  • execute method: it can be used to perform any SQL statement is generally used to perform DDL statements; Execute, executeQuery, executeUpdate
  • update method and batchUpdate method: update methods for performing add, modify and delete statements; batchUpdate method for performing a batch of related statements SQL SERVCER (GO SQL statements GO);
  • query methods and queryForXXX method: to perform a query related statements;
  • call method: stored for performing a procedure, a function related statements.

Third, the use JdbcTemplate way to achieve database programming

  (A), arrangement

  1. dependence introduction

<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.1.5.RELEASE</version>
</dependency>
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.32</version>
</dependency>

  2. entity class

public class StuInfo {
    private Integer id;
    private String name;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "StuInfo{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }

  3. Create a database connection profile

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/stutest?useUniCode=true&characterEncoding=utf-8
jdbc.username=root
jdbc.password=123

  4. build a layered architecture

    Dao layer implementation class:

public  class StuDao the extends JdbcDaoSupport { // JdbcDaoSupport class inheritance, which encapsulates the template JdbcTemplate
      / * new new the RowMapper <StuInfo> () { 
        @Override 
        public StuInfo the mapRow (the ResultSet the resultSet, I int) throws SQLException { 
            StuInfo new new StuInfo STU = (); 
            stu.setId (ResultSet.getInt ( "ID")); 
            stu.setName (resultSet.getString ( "name")); 
            return STU; 
        } 
    } * / 

        // Search information 
        public List <StuInfo> getStu () { / / automatic mapping mode are employed result set, the above mode is manual annotation associated 
            List <StuInfo> = Lists the this.getJdbcTemplate().query("select * from stu_info",  new BeanPropertyRowMapper(StuInfo.class));
            return  lists;
        }
}

       Source analysis:

  

 

 

  5.Spring profile

<!--导入jdbc配置文件-->
    <context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
   <!-- 数据源配置-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driver}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value= "$ {jdbc.username}"  /> 
        < Property name = "password" value = "$ {jdbc.password}"  /> 
    </ the bean > 

    < the bean ID = "stuDao" class = "cn.spring.jdbcTemplate. mapper.StuDao " > 
<-! configuration DAO 
        * DAO inherit JdbcDaoSupport, after only need to inject a data source, the bottom will automatically create the template 
-> < Property name =" dataSource " ref =" dataSource " > </ Property > </ the bean > <-! package scanner -> <context:component-scan base-package="cn.spring.jdbcTemplate"></context:component-scan>

  Because the JdbcTemplate inherited, so in this case the data source is injected directly in the Dao layer, otherwise:

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
 <bean id="stuDao" class="cn.spring.jdbcTemplate.mapper.StuDao">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

  6. Test

ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
        StuService stuServiceImpl = (StuService) context.getBean("StuService");
        List<StuInfo> stu = stuServiceImpl.getStu();
        //输出数据
        for (StuInfo stuInfo : stu) {
            System.out.println(stuInfo.getId() + "====" + stuInfo.getName());

        }

  (Two), notes the way

    Here only two differences:

  Dao layer implementation class:

 

@Repository
public class UserDaoImpl implements UserDao{

    @Resource//使用注解方式注入JdbcTemplate
    private JdbcTemplate jdbcTemplate;

    @Override
    public int addMoney(double money,Integer id) {
        int update = jdbcTemplate.update("update  user set money=money+? where id=?", money, id);
        return update;
    }

 

  Profiles:

 <!--设置JdbcTemplate模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

 

 

 

Guess you like

Origin www.cnblogs.com/xiao-ran/p/11782419.html