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

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","zs@163.com",100.0,1});
        empList.add(new Object[]{"lisi","lisi@163.com",100.0,2});
        empList.add(new Object[]{"wangwu","wangwu@163.com",100.0,2});
        empList.add(new Object[]{"zhaoliu","zhaoliu@163.com",100.0,1});
        empList.add(new Object[]{"qianqi","qianqi@163.com",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);
        }
复制代码

 

posted @   努力是一种常态  阅读(205)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示