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

  本篇文章为你整理了学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API()的详细内容,包含有 学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API,希望能帮助你了解 学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API。

  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"

   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"

   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);

   }

 

  

  以上就是学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API()的详细内容,想要了解更多 学习笔记——定义切面优先级 ;Spring中的JdbcTemplate;JdbcTemplate的常用API的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: