SpringBoot JDBC/AOP
JDBC
工程结构:
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>boot-jdbc</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot-jdbc</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> <dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency> <dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
实体类:
package com.example.bootjdbc.domain; import java.math.BigDecimal;
import java.util.Date; public class Book {
private Integer bookId;
private String bookName;
private String bookAuthor;
private BigDecimal bookPrice;
private Date bookDate; public Integer getBookId() {
return bookId;
} public void setBookId(Integer bookId) {
this.bookId = bookId;
} public String getBookName() {
return bookName;
} public void setBookName(String bookName) {
this.bookName = bookName;
} public String getBookAuthor() {
return bookAuthor;
} public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
} public BigDecimal getBookPrice() {
return bookPrice;
} public void setBookPrice(BigDecimal bookPrice) {
this.bookPrice = bookPrice;
} public Date getBookDate() {
return bookDate;
} public void setBookDate(Date bookDate) {
this.bookDate = bookDate;
} @Override
public String toString() {
return "Book{" +
"bookId=" + bookId +
", bookName='" + bookName + '\'' +
", bookAuthor='" + bookAuthor + '\'' +
", bookPrice=" + bookPrice +
", bookDate=" + bookDate +
'}';
}
}
userDaoiml
package com.example.bootjdbc; import com.example.bootjdbc.domain.Book;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository; import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.List; @Repository
public class UserDaoImpl {
@Autowired
private JdbcTemplate jdbcTemplate; class BookRowMapper implements RowMapper<Book> {
@Override
public Book mapRow(ResultSet rs, int rowNum) throws SQLException {
Book book = new Book();
book.setBookAuthor(rs.getString("book_author"));
book.setBookDate(new Date(rs.getDate("book_date").getTime()));
book.setBookId(rs.getInt("book_id"));
book.setBookName(rs.getString("book_name"));
book.setBookPrice(rs.getBigDecimal("book_price"));
return book;
}
} public void add() {
String sql = "INSERT INTO `testdb`.`t_book`(`book_name`, `book_author`, `book_price`, `book_date`) VALUES ('222', '333', 444, '2018-07-11');\n";
jdbcTemplate.execute(sql);
} public Book selectOne() {
String sql = "select * from t_book where book_id=?";
Book book = jdbcTemplate.queryForObject(sql, new BookRowMapper(), 2); return book;
} public List<Book> selectList() {
String sql = "select * from t_book";
return jdbcTemplate.query(sql,new BookRowMapper());
} public void update() {
String sql = "UPDATE `testdb`.`t_book` SET `book_name` = '333', `book_author` = '555', `book_price` = 444, `book_date` = '2018-07-11' WHERE `book_id` = 2;\n";
jdbcTemplate.update(sql);
} public void delete() {
String sql = "delete from t_book where book_id=?";
jdbcTemplate.update(sql,3);
}
}
测试类:
package com.example.bootjdbc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate; import javax.sql.DataSource; @SpringBootApplication
public class BootJdbcApplication { public static void main(String[] args) throws Exception {
ConfigurableApplicationContext
context = SpringApplication.run(BootJdbcApplication.class, args);
System.out.println(context.getBean(DataSource.class).getClass());
DataSource dataSource = context.getBean(DataSource.class);
System.out.println(dataSource.getConnection().getCatalog());
System.out.println(context.getBean(JdbcTemplate.class));
System.out.println("--------------------------");
UserDaoImpl userDao = context.getBean(UserDaoImpl.class);
System.out.println(userDao.selectOne());
context.close();
}
}
application.properties(默认配置HikariCP连接池)
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql:///testdb?useSSL=true
spring.datasource.username=root
spring.datasource.password=123
# 通过type指定连接池类型
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
AOP
工程结构:
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId>
<artifactId>boot-aop</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>boot-aop</name>
<description>Demo project for Spring Boot</description> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
application.properties
#spring.aop.auto = true
##默认为false,根据有无接口来选择使用的代理方式,如果设置为true代表强制使用cglib代理方式
##spring.aop.proxy-target-class=true
接口
package com.example.bootaop.dao; public interface BookDao {
void addBook(String name, String author);
}
package com.example.bootaop.dao; import org.springframework.stereotype.Repository; @Repository
public class BookDaoImpl implements BookDao { @Override
public void addBook(String name, String author) {
System.out.println("bookName:"+name+", bookAuthor:"+author);
}
}
package com.example.bootaop.dao; import org.springframework.stereotype.Repository; @Repository
public class BookDaoImpl1 implements BookDao {
@Override
public void addBook(String name, String author) { }
}
切入类
package com.example.bootaop.aop; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component; import java.util.Arrays; @Component
@Aspect
public class BookAop {
// 定义切入点
public static final String POINT_CUT = "execution(* com.example.bootaop.dao..*.*(..))"; @Before(POINT_CUT)
public void before() {
System.out.println("添加图书方法校验前.....");
} @After(POINT_CUT)
public void after(JoinPoint jp) {
/// System.out.println(AopContext.currentProxy());
System.out.println(jp.getTarget().getClass());
System.out.println(Arrays.asList(jp.getArgs()));
System.out.println("添加图书成功后.....");
}
}
测试类:
package com.example.bootaop; import com.example.bootaop.dao.BookDao;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.EnableAspectJAutoProxy; @SpringBootApplication
@EnableAspectJAutoProxy(proxyTargetClass = false, exposeProxy = false)
public class BootAopApplication { public static void main(String[] args) {
ConfigurableApplicationContext
context = SpringApplication.run(BootAopApplication.class, args);
/*BookDaoImpl bookDao = context.getBean(BookDaoImpl.class);
System.out.println(bookDao.getClass());
bookDao.addBook("三国演义", "罗贯中");*/
BookDao bookDao = context.getBean("bookDaoImpl1",BookDao.class);
System.out.println(bookDao.getClass());
bookDao.addBook("三国演义", "罗贯中");
context.close();
}
}
SpringBoot JDBC/AOP的更多相关文章
- Springboot的日志管理&Springboot整合Junit测试&Springboot中AOP的使用
==============Springboot的日志管理============= springboot无需引入日志的包,springboot默认已经依赖了slf4j.logback.log4j等日 ...
- SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数
SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...
- SpringBoot切面Aop的demo简单讲解
前言 本篇文章主要介绍的是SpringBoot切面Aop的demo简单讲解. SpringBoot Aop 说明:如果想直接获取工程那么可以直接跳到底部,通过链接下载工程代码. 切面(Aop) 一.概 ...
- Spring全家桶——SpringBoot之AOP详解
Spring全家桶--SpringBoot之AOP详解 面向方面编程(AOP)通过提供另一种思考程序结构的方式来补充面向对象编程(OOP). OOP中模块化的关键单元是类,而在AOP中,模块化单元是方 ...
- 读写分离很难吗?springboot结合aop简单就实现了
目录 前言 环境部署 开始项目 注意 參考: 前言 入职新公司到现在也有一个月了,完成了手头的工作,前几天终于有时间研究下公司旧项目的代码.在研究代码的过程中,发现项目里用到了Spring Aop来实 ...
- SpringBoot CGLIB AOP解决Spring事务,对象调用自己方法事务失效.
对于像我这种喜欢滥用AOP的程序员,遇到坑也是习惯了,不仅仅是事务,其实只要脱离了Spring容器管理的所有对象,对于SpringAOP的注解都会失效,因为他们不是Spring容器的代理类,Sprin ...
- (办公)springboot配置aop处理请求.
最近项目用到springboot,就是需要配置一些东西.比如用aop处理请求.方法前通知获取url,method,ip,类方法,参数,方法后通知,返回参数,而且还可以记录一下日志.下面是操作的代码. ...
- SpringBoot系列——aop 面向切面
前言 项目中我们经常会用到aop切面,比如日志记录:这里简单记录一下springboot是如何使用aop spring对aop的配置,来自springboot参考手册,Common applicati ...
- SpringBoot使用AOP
本文介绍SpringBoot中使用Spring AOP. 简介 AOP简介 AOP可能对于广大开发者耳熟能详,它是Aspect Oriented Programming的缩写,翻译成中文就是:面向切面 ...
随机推荐
- springBoot Swagger2 接口文档生成
// 生成配置类 package com.irm.jd.config.swagger; import org.springframework.context.annotation.Bean; impo ...
- Sql Server 2008R2中使用CET进行递归查询
在使用数据库的过程中,我们经常会遇到递归的查询.比如传入一个分类ID,要查出这个分类下的所有子分类,以及子分类的子分类.或者说传入一个部门ID,要查出这个部门下所有子部门的人员:在Or ...
- TraceHelper
public class TraceHelper { private static TraceHelper _traceHelper; private TraceHelper() { } public ...
- String、StringBuffer、StringBuilder的区别和解析
1.三个类之间的关系 他们都是通过字符数组来实现的,继承关系 String:字符串常量,不可变类 StringBuffer:字符串变量,可变类,线程安全 StringBuilder:字符串变量,可变类 ...
- cf#516A. Make a triangle!(三角形)
http://codeforces.com/contest/1064/problem/A 题意:给出三角形的三条边,问要让他组成三角形需要增加多少长度 题解:规律:如果给出的三条边不能组成三角形,那答 ...
- OSG-OSG中的observer_ptr指针
看array大神的CookBook后一些感想,在代码上添加了一些注释,也对源码做了一些研读,记录下学习的过程. CookBook中第一个例子就是observer_ptr指针,这个指针和它的名字一样,就 ...
- Qt-QML-Popup,弹层界面编写
随着接触Qt的时间的增加,也逐渐的发现了Qt 的一些不人信话的一些地方,不由的想起一句话,也不知道是在哪里看到的了“一切变成语言都是垃圾,就C++还可以凑合用”大致意思是这样.最近项目的祝界面框架都基 ...
- CentOS 7.2-编译安装zabbix 3.4
起因: 前面已经使用yum安装了zabbix 3.4了,准备去交差了,交差时老大明确要求必须使用编译安装,统一放在/usr/local目录下.... 重来吧!! 一.环境说明 本次安装使用CentOS ...
- Objective-C 类和对象
面向对象 面向对象(Object-Oriented)是基于面向过程(procedure-oriented)而言的 面向对象 强调对象<指挥者> OC, Java语言就是面向对象 面向过程 ...
- Java开发工程师(Web方向) - 03.数据库开发 - 第3章.SQL注入与防范
第3章--SQL注入与防范 SQL注入与防范 经常遇到的问题:数据安全问题,尤其是sql注入导致的数据库的安全漏洞 国内著名漏洞曝光平台:WooYun.org 数据库泄露的风险:用户信息.交易信息的泄 ...