首页 > 其他分享 >spring+JDBC

spring+JDBC

时间:2022-11-08 06:22:36浏览次数:49  
标签:JDBC spring springframework id org import public

1.导入依赖

 <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-core</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context-support</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-expression</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>commons-logging</groupId>
      <artifactId>commons-logging</artifactId>
      <version>1.2</version>
    </dependency>

    <dependency>
      <groupId>mysql</groupId>
      <artifactId>mysql-connector-java</artifactId>
      <version>8.0.20</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-jdbc</artifactId>
      <version>5.3.8</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-tx</artifactId>
      <version>5.3.8</version>
    </dependency>

    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-aop</artifactId>
      <version>5.3.8</version>
    </dependency>

2.创建spring配置文件 配置数据源

<!-- 1.配置数据源  -->
    <bean class="org.springframework.jdbc.datasource.DriverManagerDataSource" id="dataSource">
        <!-- 1.1.数据库驱动 -->
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <!-- 1.2.连接数据库的url -->
        <property name="url" value="jdbc:mysql://localhost:3306/spring?characterEncoding=utf8&amp;serverTimezone=UTC"/>
        <!-- 1.3.连接数据库的用户名 -->
        <property name="username" value="root"></property>
        <!-- 1.4.连接数据库的密码 -->
        <property name="password" value="123456"></property>
    </bean>
    <!-- 2配置JDBC模板 -->
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        <!-- 默认必须使用数据源 -->
        <property name="dataSource" ref="dataSource"></property>
    </bean>

3.实体类

package com.springjdbc;

public class Person {
    private int id;

    public int getId() {
        return id;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getName() {
        return name;
    }

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

    private int age;
    private String name;

}

4.Dao层

package com.springjdbc;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
//用注解创建的对象就是类本身的类型,名字就是这个类的名字,首字母小写personDao
@Repository
public class PersonDao {
    //    1 实现依赖注入
//    2 生成成员变量get和set方法
//    3 实现自动注入 byType->byName (@Resource功能类似,只是该注解优先采用根据名称自动装配)
    @Autowired
    JdbcTemplate jdbcTemplate;

    public List<Person> findAllUser(){
        RowMapper<Person> rowMapper = new BeanPropertyRowMapper<>(Person.class);
        return jdbcTemplate.query("select * from person",rowMapper);
    }

    public void addUser(String name,int age){
        jdbcTemplate.update("insert into person values (null,?,?)",age,name);
    }//?为占位符

    public void updateUser(String name,int age,int id){
        jdbcTemplate.update("update person set name = ?,age = ? where id = ?",name,age,id);
    }

    public void deleteUser(int id){
        jdbcTemplate.update("delete from person where id = ?",id);
    }
}

5.测试

package com.springjdbc;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.List;

/**
 * Hello world!
 *
 */
public class App 
{
    public static void main( String[] args )
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");//加载spring容器,创建对象
        PersonDao personDao = (PersonDao) applicationContext.getBean("personDao");
        personDao.addUser("小花",18);
        List<Person> personList = personDao.findAllUser();
        for(Person person : personList){
            System.out.println(person.getName()+"  "+person.getAge());
        }
        System.out.println( "Hello World!" );
    }
}

 

标签:JDBC,spring,springframework,id,org,import,public
From: https://www.cnblogs.com/dzs894330350/p/16868063.html

相关文章

  • Spring Frameword参考手册
    目录OverviewCoreTestingDataAccessWebServletWebReactiveIntegrationLanguagesAppendixWikiOverviewhistory,designphilosophy,feedback,gettingstarted.Core......
  • SpringBoot参考手册
    目录LegalGettingHelpDocumentationOverviewGettingStartedUpgradingSpringBootApplicationsUsingSpringBootCoreFeaturesWebDataIOMessagingContainerImagesPro......
  • SpringBoot IO
    目录1、Caching1.1、SupportedCacheProviders2、Hazelcast3、QuartzScheduler4、SendingEmail5、Validation5.1、验证手机号是否有效示例5.1.1、定义验证注解类5.1.2、......
  • Spring的IoC容器之BeanFactory
    SpringIoC容器之BeanFactory首先,Spring提供了两种容器类型:BeanFactory和ApplicationContext。BeanFactory:基础容器类型,提供了完整的IoC服务支持。默认采用延迟......
  • springboot多环境配置
    springboot整合多个环境配置springboot默认读取配置文件名称application,且多个环境配置文件必须按照如下命名Application-dev.yml开发环境application-test.yml......
  • springboot整合nacos config详解
    springboot整合nacos详解@value:读取application文件@ConfigurationProperties:读取指定文件@PropertySource:自定义文件引入@PropertySource+@Value:读取自定义文件@Pro......
  • 事件10841解决oracle 9i中JDBC Thin Driver ORA-00600 [ttcgcshnd-1]的异常
    文档课题:事件10841解决oracle9i中JDBCThinDriverORA-00600ttcgcshnd-1的异常.用thindriver连接Oracle9i时,出现exception,如下所示java.sql.SQLException:ORA-00600:......
  • Spring 的jar包下载
    Spring官方Spring官网(https://spring.io/)Spring的下载进入Spring官网,选择Progects下的SpringFramework选项,进入详情页面Spring官网Springframework......
  • springboot整合多个环境配置
    springboot整合多个环境配置​ springboot默认读取配置文件名称application,且多个环境配置文件必须按照如下命名Application-dev.yml 开发环境application-test.y......
  • springboot 上传文件设置文件大小限制
     报错内容:org.springframework.web.multipart.MaxUploadSizeExceededException: Maximum upload size exceeded; nested exception is java.lang.IllegalStateExce......