SSM——Spring中JdbcTemplate基本使用

一. JdbcTemplate概述

它是spring框架中提供的一个对象,是对原始繁琐的Jdbc API对象的简单封装。spring框架为我们提供了很多的操作模板类。

  • 例如:操作关系型数据的JdbcTemplate和HibernateTemplate,操作nosql数据库的RedisTemplate,操作消息队列的JmsTemplate等等。

二. JdbcTemplate开发步骤

在这里插入图片描述

1. 导入坐标

<!--导入spring的jdbc坐标-->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-jdbc</artifactId>
	<version>5.0.5.RELEASE</version>
</dependency>

<!--导入spring的tx坐标-->
<dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-tx</artifactId>
	<version>5.0.5.RELEASE</version>
</dependency>

2. 创建accout表和Accout实体

在这里插入图片描述

3. 创建JdbcTemplate对象、执行数据库操作

@Test
    //测试JdbcTemplate开发步骤
    public void test1() throws PropertyVetoException {
        //1. 创建数据源对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/test");
        dataSource.setUser("root");
        dataSource.setPassword("root");
        //2、创建JdbcTemplate对象
        JdbcTemplate jdbcTemplate = new JdbcTemplate();
        //3. 设置数据源对象  设置数据源给JdbcTemplate
        jdbcTemplate.setDataSource(dataSource);
        //4. 执行操作
        int row = jdbcTemplate.update("insert into test.account values(?,?)", "tom", 5000);
        System.out.println(row);

    }

在这里插入图片描述

三. Spring产生JdbcTemplate对象

我们可以将JdbcTemplate的创建权交给Spring,将数据源DataSource的创建权也交给Spring,在Spring容器内部将数据源DataSource注入到JdbcTemplate模版对象中,配置如下:

<?xml version="1.0" encoding="UTF-8"?>
<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"
       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">

    <!--加载jdbc.properties-->
    <context:property-placeholder location="classpath:jdbc.properties"/>

    <!--数据源对象-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="${jdbc.driver}"/>
        <property name="jdbcUrl" value="${jdbc.url}"/>
        <property name="user" value="${jdbc.username}"/>
        <property name="password" value="${jdbc.password}"/>
    </bean>

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

</beans>

在这里插入图片描述

1. 从容器中获得JdbcTemplate进行添加操作

在这里插入图片描述

2. 修改操作

在这里插入图片描述

package com.itheima.test;

import com.itheima.domain.Account;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class JdbcTemplateCRUDTest {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Test
    public void testQueryCount(){
    	//简单类型不需要javabean进行封装
        Long count = jdbcTemplate.queryForObject("select count(*) from test.account", Long.class);
        System.out.println(count);
    }

    @Test
    public void testQueryOne(){
        Account account = jdbcTemplate.queryForObject("select * from test.account where name=?", new BeanPropertyRowMapper<Account>(Account.class), "tom");
        System.out.println(account);
    }

	
    @Test
    public void testQueryAll(){
    	//返回一个Javabean需要进行映射封装
        List<Account> accountList = jdbcTemplate.query("select * from test.account", new BeanPropertyRowMapper<Account>(Account.class));
        System.out.println(accountList);
    }

    @Test
    public void testUpdate(){
        jdbcTemplate.update("update test.account set money=? where name=?",10000,"tom");
    }

    @Test
    public void testDelete(){
        jdbcTemplate.update("delete from test.account where name=?","tom");
    }

}

3. 删除和查询全部操作

在这里插入图片描述

4. 查询单个数据操作操作

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSMSpring+Spring MVC+MyBatis)项目使用Spring Security可以提供身份验证和授权功能,以保护应用程序的安全性。下面是在SSM项目使用Spring Security的一般步骤: 1. 添加依赖:在项目的pom.xml文件添加Spring Security的依赖。例如: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>5.4.6</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> <version>5.4.6</version> </dependency> ``` 2. 配置Spring Security:在Spring的配置文件(通常是applicationContext.xml)配置Spring Security。可以使用XML配置或Java配置。 - XML配置示例: ```xml <import resource="classpath:security-config.xml" /> ``` - Java配置示例: ```java @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { // 配置身份验证和授权规则 @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .antMatchers("/admin/**").hasRole("ADMIN") .antMatchers("/user/**").hasAnyRole("USER", "ADMIN") .anyRequest().authenticated() .and() .formLogin() .loginPage("/login") .defaultSuccessUrl("/home") .permitAll() .and() .logout() .logoutUrl("/logout") .logoutSuccessUrl("/login?logout") .permitAll(); } // 配置用户认证的数据源 @Override protected void configure(AuthenticationManagerBuilder auth) throws Exception { auth .inMemoryAuthentication() .withUser("user").password("{noop}password").roles("USER") .and() .withUser("admin").password("{noop}password").roles("ADMIN"); } } ``` 3. 创建登录和注销页面:创建登录页面(例如login.jsp)和注销页面(例如logout.jsp)。 4. 对需要保护的资源进行配置:根据应用程序的需求,对需要保护的URL进行配置,以限制访问只有经过身份验证和授权的用户才能访问。 以上是在SSM项目使用Spring Security的基本步骤,具体的配置和使用方式可以根据项目的需求进行调整。另外,还可以使用数据库等其他方式来进行用户认证和授权。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值