【Spring】Spring JDBC配置与使用

本文目录

  • Spring JDBC
  • Spring JDBC 示例
    • 第一步:创建maven工程和数据库表
    • 第二步:创建dao层
    • 第三步:添加Spring配置文件
    • 第四步:测试代码和运行结果

Spring JDBC

Spring JDBC 框架负责所有的低层细节,从开始打开连接,准备和执行 SQL 语句,处理异常,处理事务,到最后关闭连接。大大简化了开发人员对数据库的操作,使得开发人员可以从繁琐的数据库操作中解脱出来,从而将更多的精力投入到编写业务逻辑中。
Spring框架提供了JdbcTemplate类,该类是Spring框架数据抽象层的基础这是管理所有数据库通信和异常处理的中央框架类。

Spring JDBC 示例

第一步:创建maven工程和数据库表

打开mysql,创建一张user

CREATE TABLE user(
   ID   INT NOT NULL AUTO_INCREMENT,
   NAME VARCHAR(20) NOT NULL,
   AGE  INT NOT NULL,
   PRIMARY KEY (ID)
);

创建一个maven工程【Spring】Spring入门案例

pom.xml中加入Springmysql相关的包

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
  </dependency>

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

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

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

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context-support</artifactId>
    <version>5.2.7.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-expression</artifactId>
    <version>5.2.7.RELEASE</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.2.7.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>5.2.7.RELEASE</version>
  </dependency>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>5.2.7.RELEASE</version>
  </dependency>
</dependencies>

代码结构如下:
【Spring】Spring JDBC配置与使用_第1张图片

第二步:创建dao层

public interface UserDao {
    public int update(String sql,Object[] param);
    public List<User> query(String sql, Object[]  param);
}

@Repository("userDao")
public class UserDaoImpl implements UserDao{
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int update(String sql,Object[] param){
        return  jdbcTemplate.update(sql,param);
    }

    public List<User> query(String sql, Object[]  param){
        RowMapper<User> rowMapper = new BeanPropertyRowMapper<>(User.class);
        return  jdbcTemplate.query(sql,rowMapper,param);
    }
}

第三步:添加Spring配置文件


<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 https://www.springframework.org/schema/context/spring-context.xsd">
    <context:component-scan base-package="com.lucas"/>
    
    <bean id="dataSource"
          class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        
        <property name="driverClassName"
                  value="com.mysql.cj.jdbc.Driver">property>
        
        <property name="url"
                  value="jdbc:mysql://localhost:3306/springtest?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true">property>
        
        <property name="username" value="root">property>
        
        <property name="password" value="123456">property>
    bean>

    
    <bean id="jdbcTemplate"
          class="org.springframework.jdbc.core.JdbcTemplate">
        
        <property name="dataSource" ref="dataSource">property>
    bean>
beans>

第四步:测试代码和运行结果

添加测试代码

public class App {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserDao userDao = (UserDao) applicationContext.getBean("userDao");

        Object[] para = {"张三", 21};
        String sql = "insert into user values (null,?,?)";
        userDao.update(sql, para);

        String sql1 ="select * from user";
        List<User> users =userDao.query(sql1,null);
        for(User user : users){
            System.out.println(user);
        }
    }
}

【Spring】Spring JDBC配置与使用_第2张图片

你可能感兴趣的:(JavaEE,#,Spring,#,Java)