Spring中使用JDBCTemplate

Spring 中使用JdbcTemplate

1,新建maven工程,导入所需依赖

	<!--spring依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>
    <!--AOP AspectJ依赖-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>

    <!--JdbcTemplate依赖-->
    <!-- mysql依赖 -->
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>5.1.46</version>
    </dependency>
    <!--Druid数据库连接池依赖-->
    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>druid</artifactId>
        <version>1.0.9</version>
    </dependency>

    <!-- 2.Spring dao依赖 -->
    <!-- spring-jdbc包括了一些如jdbcTemplate的工具类 -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.2.6.RELEASE</version>
    </dependency>

2,引入数据库连接池>>druid

在resources资源目录中创建jdbcdruid.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=dbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=123456
max=20

在resources资源目录中的spring配置文件中加入连接池配置信息

<!--引入连接池配置文件-->
    <context:property-placeholder location="classpath:jdbcdruid.properties"/>
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}"/>
        <property name="username" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="maxActive" value="${max}"/>
    </bean>

3,配置JDBCTemplate对象,注入DataSource

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

4,创建dao和service,在dao中注入jdbcTemplate对象

dao

@Repository
public class BookDaoImpl implements BookDao {
    //注入jdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
}

service

@Service
public class BookService {
    //注入dao
    @Autowired
    private BookDao bookDao;
}

5,使用jdbcTemplate进行增删改查操作

(1)创建表和实体类

public class User {
    private String userId;
    private String username;
    private String ustatus;
}

(2)编写service和dao

@Repository
public class BookDaoImpl implements BookDao {
    //注入jdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void add(Book book) {

        String sql="insert into t_book values (?,?,?)";
        int update = jdbcTemplate.update(sql, book.getUserId(), book.getUsername(), book.getUstatus());
        System.out.println(update);
    }
    @Override
    public void delete(Integer id) {
        String sql = "delete from t_book where user_id=?";
        int update = jdbcTemplate.update(sql, id);
        System.out.println(update);
    }

    @Override
    public void update(Book book) {
        String sql = "update t_book set username=?,ustatus=? where user_id=?";
        int update = jdbcTemplate.update(sql, book.getUsername(), book.getUstatus(), book.getUserId());
        System.out.println(update);
    }

    @Override
    public int selectCount() {
        //返回记录数
        String sql = "select count(*) from t_book";
        Integer i = jdbcTemplate.queryForObject(sql, Integer.class);
        return i;
    }

    @Override
    public Book findBookInfo(Integer id) {
        String sql = "select * from t_book where user_id=?";
        Book book = jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper<Book>(Book.class), id);
        return book;
    }

    @Override
    public List<Book> findAll() {
        String sql = "select * from t_book";
        List<Book> books = jdbcTemplate.query(sql, new BeanPropertyRowMapper<Book>(Book.class));
        return books;
    }
}
@Service
public class BookService {
    //注入dao
    @Autowired
    private BookDao bookDao;
    // 添加的方法
    public void addBook(Book book){
        bookDao.add(book);
    }
    public void updateBook(Book book){
        bookDao.update(book);
    }
    //删除
    public void deleteBook(Integer id){
        bookDao.delete(id);
    }
    //查询表中的记录数
    public int findCount(){
        return bookDao.selectCount();
    }
    //查询返回对象
    public Book queryBook(Integer id){
        return bookDao.findBookInfo(id);
    }
    //返回集合
    public List<Book> queryAllBook(){
        return bookDao.findAll();
    }
}
@Test
    public void addBook() {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        bookService.addBook(new Book(null,"《假如给我三天光明》","y"));
        bookService.updateBook(new Book("1","《假如给我三天光明》","n"));
        bookService.deleteBook(1);
        int count = bookService.findCount();
        Book book = bookService.queryBook(2);
        List<Book> books = bookService.queryAllBook();
    }

6,使用JdbcTemplate模板对数据库进行批量操作

(1)批量操作

batchUpdate(String sql,List<Object[ ] batchArgs >)
参数1:sql语句,参数2:List集合,添加多条记录

 @Override
    public void batchAdd(List<Object[]> books) {
        String sql="insert into t_book values (?,?,?)";
        int[] ints = jdbcTemplate.batchUpdate(sql, books);
        System.out.println(Arrays.toString(ints));
    }
@Test
    public void batchAdd() {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        List<Object[]> list = new ArrayList<>();
        Object[] o1={null,"java","n"};
        Object[] o2={null,"C++","n"};
        Object[] o3={null,"Python","n"};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        bookService.batchAdd(list);
    }

(2)批量修改

    @Override
    public void batchUpdate(List<Object[]> books) {
        String sql = "update t_book set username=?,ustatus=? where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, books);
        System.out.println(Arrays.toString(ints));
    }
    @Test
    public void batchUpdate() {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        List<Object[]> list = new ArrayList<>();
        //注意占位符顺序
        Object[] o1={"《习近平谈治国理政第一卷》","y",10};
        Object[] o2={"《习近平谈治国理政第二卷》","y",11};
        Object[] o3={"《习近平谈治国理政第三卷》","y",12};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        bookService.batchUpdate(list);
    }

(3)批量删除

    @Override
    public void batchDelete(List<Object[]> books) {
        String sql = "delete from t_book where user_id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, books);
        System.out.println(Arrays.toString(ints));
    }
    @Test
    public void batchDelete() {
        ApplicationContext context = new ClassPathXmlApplicationContext("ApplicationContext.xml");
        BookService bookService = context.getBean("bookService", BookService.class);
        List<Object[]> list = new ArrayList<>();
        //注意占位符顺序
        Object[] o1={10};
        Object[] o2={11};
        Object[] o3={12};
        list.add(o1);
        list.add(o2);
        list.add(o3);
        bookService.batchDelete(list);
a
    }
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 1024 设计师:白松林 返回首页