Spring框架学习10——JDBC Template 实现数据库操作

为了简化持久化操作,Spring在JDBC API之上提供了JDBC Template组件。

1、添加依赖

添加Spring核心依赖,MySQL驱动

<!--Spring核心基础依赖-->
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-core</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-context</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-beans</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-expression</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<!-- AOP联盟依赖 -->
<dependency>
  <groupId>aopalliance</groupId>
  <artifactId>aopalliance</artifactId>
  <version>1.0</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-aop</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<!-- Mysql驱动 -->
<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <version>5.1.38</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-jdbc</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>
<dependency>
  <groupId>org.springframework</groupId>
  <artifactId>spring-tx</artifactId>
  <version>5.0.2.RELEASE</version>
</dependency>

2、配置数据源

在applicationContext.xml文件中配置数据源

<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: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.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">

    <!--配置数据源-->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <!--MySQL数据库驱动-->
        <property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
        <!--连接数据库的URL-->
        <property name="url" value="jdbc:mysql://localhost:3306/library?useUnicode=true&amp;characterEncoding=utf8"></property>
        <!--连接数据库的用户名-->
        <property name="username" value="root"></property>
        <!--连接数据库的密码-->
        <property name="password" value="root"></property>
    </bean>
    <!--配置JDBC模板-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>

</beans>

3、update方法

使用update方法对数据进行增删改操作
获取JdbcTemplate对象

private JdbcTemplate jdbcTemplate;

{
    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    jdbcTemplate = (JdbcTemplate) app.getBean("jdbcTemplate");
}

添加数据

String sql = "insert into book(name,author,price) values(?,?,?)";
jdbcTemplate.update(sql,new Object[]{"西游记","吴承恩",90.8});

修改数据

String sql = "update book set name=?,author=?,price=? where id=?";
jdbcTemplate.update(sql,"红楼梦","曹雪芹",100,1);

删除数据

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

批量操作数据,批量执行多SQL语句

String[] sqls = {
        "insert into book(name,author,price) values('Java基础','张三',90)",
        "insert into book(name,author,price) values('C语言','李四',80)",
        "insert into book(name,author,price) values('Web前端','王五',95)",
        "update book set name='水浒传',author='施耐庵' where id=1",
};
jdbcTemplate.batchUpdate(sqls);

批量操作数据,执行同SQL语句

String sql = "insert into book(name,author,price) values(?,?,?)";
List<Object[]> list = new ArrayList<Object[]>();
list.add(new Object[]{"HTML","Tom",90.8});
list.add(new Object[]{"CSS","Jack",88});
list.add(new Object[]{"JavaScript","Lily",89});
jdbcTemplate.batchUpdate(sql,list);

4、查询简单类型

查询单个数据

String sql = "select count(*) from book";
int count = jdbcTemplate.queryForObject(sql,Integer.class);
System.out.println(count);

查询多个数据

String sql = "select name from book where price=?";
List<String> list = jdbcTemplate.queryForList(sql,String.class,90);
System.out.println(list);

5、查询复杂类型(封装为Map)

查询单个对象

String sql = "select * from book where id=?";
Map<String,Object> map = jdbcTemplate.queryForMap(sql,2);
System.out.println(map);

查询多个对象

String sql = "select * from book";
List<Map<String,Object>> list =jdbcTemplate.queryForList(sql);
System.out.println(list);

6、查询复杂类型(封装为实体对象)

创建实体类

public class Book {
    private int id;
    private String name;
    private String author;
    private double price;
    //getter、setter、toString方法
}

查询单个对象

String sql = "select * from book where id=?";
Book book = jdbcTemplate.queryForObject(sql,new BeanPropertyRowMapper<Book>(Book.class),2);
System.out.println(book);

查询多个对象

String sql = "select * from book";
List<Book> list = jdbcTemplate.query(sql,new BeanPropertyRowMapper<Book>(Book.class));
System.out.println(list);

 

posted @ 2019-03-26 14:52  Web1024  阅读(582)  评论(1编辑  收藏  举报