2023-01-18 一、定義切麵優先順序 1、語法:@Order(value=index) ①index是int類型,預設值是int可存儲的最大值 ②數值越小,優先順序越高 二、Spring中的JdbcTemplate 1、JdbcTemplate簡介 (1)Spring提供的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); }