首页 > 其他分享 >学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API

学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API

时间:2023-01-18 17:58:01浏览次数:45  
标签:String Spring dept API JdbcTemplate sql employee jdbcTemplate

2023-01-18

一、定义切面优先级 

 1、语法:@Order(value=index)

①index是int类型,默认值是int可存储的最大值

②数值越小,优先级越高

二、Spring中的JdbcTemplate

1、JdbcTemplate简介

(1)Spring提供的JdbcTemplate是一个小型持久化层框架,简称Jdbc代码

Mybatis是一个半自动化的ORM持久化层框架

2、JdbcTemplate基本使用

(1)导入Jar包

    <dependencies>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>5.3.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>5.3.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/com.alibaba/druid -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.10</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.26</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/junit/junit -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

(2)编写配置文件

db.properties:设置连接数据库属性

applicationContext.xml(spring配置文件)

    加载外部属性文件、装配数据源(DataSources)、装配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 https://www.springframework.org/schema/context/spring-context.xsd">


<!--    加载外部属性文件-->
    <context:property-placeholder location="classpath:db.properties"></context:property-placeholder>

<!--    装配数据源(DataSources)-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="driverClassName" value="${db.driverClassName}"></property>
        <property name="url" value="${db.url}"></property>
        <property name="username" value="${db.username}"></property>
        <property name="password" value="${db.password}"></property>
    </bean>

    <!--    装配JdbcTemplate-->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    
</beans>

(3)使用核心类库

三、JdbcTemplate的常用API

1、jdbcTemplate.update(String sql,Object... args):通用的增删改查方法

 //创建容器对象
        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext_jdbcTemplate.xml");
        //获取JdbcTemplate对象
        JdbcTemplate jdbcTemplate = context.getBean("jdbcTemplate", JdbcTemplate.class);

        //增
//        String sql = "insert into tbl_dept(dept_name) values(?)";
//        jdbcTemplate.update(sql,"采购部门");

        //删
//        String sql = "delete from tbl_dept where dept_id = ?";
//        jdbcTemplate.update(sql,4);

        //改
        String sql = "update tbl_dept set dept_name=? where dept_id=?";
       jdbcTemplate.update(sql,"人事2部",3);

2、jdbcTemplate.batchUpdate(String,List<Object[ ]>):通用批处理增删改查方法

//批量增
        String sql = "insert into tbl_employee(last_name,email,salary,dept_id) values(?,?,?,?)";
        List<Object[]> empList = new ArrayList<>();
        empList.add(new Object[]{"zs","[email protected]",100.0,1});
        empList.add(new Object[]{"lisi","[email protected]",100.0,2});
        empList.add(new Object[]{"wangwu","[email protected]",100.0,2});
        empList.add(new Object[]{"zhaoliu","[email protected]",100.0,1});
        empList.add(new Object[]{"qianqi","[email protected]",100.0,1});

        jdbcTemplate.batchUpdate(sql,empList);

 

3、jdbcTemplate.queryForObject(String sql,Class clazz,Object... args):查询单个数值

String sql = "select count(1) from tbl_employee";
Integer count = jdbcTemplate.queryForObject(sql, Integer.class);
System.out.println("员工数量为:" + count);

4、jdbcTemplate.queryForObject(String sql,RowMapper<T> rm,Object... args):查询单个对象

//查询单个对象
        String sql = "select id,last_name,email,salary,dept_id from tbl_employee where id=?";
//        创建RowMapper
        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
        Employee employee = jdbcTemplate.queryForObject(sql, rowMapper, 1);
        System.out.println("employee = " + employee);

5、jdbcTemplate.query(String sql,RowMapper<T> rm,Object... args):查询多个对象

//查询多个对象
         String sql = "select id,last_name,email,salary,dept_id from tbl_employee";
        //创建RowMapper<T>
        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper<>(Employee.class);
        //测试
        List<Employee> query = jdbcTemplate.query(sql, rowMapper);
        for (Employee employee : query) {
            System.out.println("employee = " + employee);
        }

 

标签:String,Spring,dept,API,JdbcTemplate,sql,employee,jdbcTemplate
From: https://www.cnblogs.com/isDaHua/p/17059859.html

相关文章

  • Springboot整合swagger3
    引入依赖<dependency><groupId>io.springfox</groupId><artifactId>springfox-boot-starter</artifactId><version>3.0.0</ve......
  • 【案例实战】SpringBoot整合GRPC微服务远程通信
    文章目录​​1.什么是GRPC​​​​2.GRPC特性以及应用场景​​​​3.GRPC大致请求流程​​​​4.GRPC的优点和缺点​​​​5.SpringBoot整合GRPC环境准备​​​​6.SpringBo......
  • springboot项目集成xxl-job
    一、xxl-job简介xxl-job是一个开源的分布式定时任务框架,它可以与其他微服务组件一起构成微服务集群。它的调度中心(xxl-job)和执行器(自己的springboot项目中有@XxlJob("......
  • springday2_热部署
    目录热部署意义:仅仅加载开发者自定义的资源,不加载jar资源手工启动热部署自动启动热部署启动条件:修改代码后,idea失去焦点5秒后自动构建打开方法:热部署配置范围......
  • Centos7 部署NetCore6.0 Web Api
    一、安装dotnet环境第一步sudorpm-Uvhhttps://packages.microsoft.com/config/centos/7/packages-microsoft-prod.rpm第二步sudoyuminstalldotnet-sdk-6.0......
  • 学习笔记——Spring中的AOP(AspectJ);Spring中AOP概述;Spring中AOP相关术语;AspectJ中切
    2023-01-18一、Spring中的AOP1、AspectJ(1)简介Java社区里最完整最流行的AOP框架在Spring2.0以上版本中,可以使用AspectJ注解或基于XML配置的AOP(2)使用AspectJ步骤①在s......
  • 一文搞定SpringCloud Alibaba全部知识点!
    文章目录1.分布式架构简介1.1.分布式架构1.2.常见的微服务架构解决方案1.3.分布式系统核心组件图2.AlibabaCloud架构环境准备2.1.创建maven聚合项目2.2.配置MyBa......
  • 【集成-Jedis】SpringBoot集成Jedis
    将jedis的依赖放进Maven<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.7.......
  • Springboot:aop注解实现
    概念Aop原指面向切面编程。但在spring中对aop的实现指的是:对方法执行前后加入其它逻辑代码,达到增强方法的目的spring-aop的底层实现一般实现aop,有两种方案:JDK动态......
  • SpringShell使用说明
    一、添加依耐(pom.xml)<dependency><groupId>org.springframework.shell</groupId><artifactId>spring-shell-starter</artifactId><version>2.1.4</version><......