先创建一个接口,并写上实现的方法: public interface Some {
public void add();
public void ent();
}
创建接口实现类,并继承该接口,实现该接口中的方法:
public class SomeImpl implements Some {
public void add() {
System.out.println("--------add-------");
}
public void ent() {
System.out.println("--------ent-------");
}
}
在创建一个增强类;
public class MyAspect {
//前置增强
public void before(){
System.out.println("=======前置增强======");
}
public void before(JoinPoint jp){
System.out.println("前置通知方法before() jp="+jp);
}
//后置增强
public void afterReturing(){
System.out.println("=========后置曾强=======");
}
public void afterReturing(String result){
System.out.println("后置方法通知 result="+result);
}
//环绕增强通知
public Object around(ProceedingJoinPoint pjo) throws Throwable {
System.out.println("环绕通知方法,目标执行之前");
Object result=pjo.proceed();
System.out.println("环绕通知方法,目标方法执行之后");
return ((String)result).toUpperCase();
}
//异常通知
public void afterThrowing(){
System.out.println("异常通知方法");
}
public void afterThrowing(Exception ex){
System.out.println("异常通知方法 ex="+ex.getMessage());
}
//最终通知
public void after(){
System.out.println("最终通知方法");
}
}
public class SomeImpl implements Some { public void add() { System.out.println("--------add-------"); } public void ent() { System.out.println("--------ent-------"); } }
在创建一个增强类;
public class MyAspect { //前置增强 public void before(){ System.out.println("=======前置增强======"); } public void before(JoinPoint jp){ System.out.println("前置通知方法before() jp="+jp); } //后置增强 public void afterReturing(){ System.out.println("=========后置曾强======="); } public void afterReturing(String result){ System.out.println("后置方法通知 result="+result); } //环绕增强通知 public Object around(ProceedingJoinPoint pjo) throws Throwable { System.out.println("环绕通知方法,目标执行之前"); Object result=pjo.proceed(); System.out.println("环绕通知方法,目标方法执行之后"); return ((String)result).toUpperCase(); } //异常通知 public void afterThrowing(){ System.out.println("异常通知方法"); } public void afterThrowing(Exception ex){ System.out.println("异常通知方法 ex="+ex.getMessage()); } //最终通知 public void after(){ System.out.println("最终通知方法"); } }
第一步:导jar包 pom.xml
<!--引入spring-beans节点-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>4.2.3.RELEASE</version>
</dependency>
<!--引入Spring-context节点-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<!--引入Spring-JDBC节点-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>4.2.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.wisdom-framework</groupId>
<artifactId>MySQL-connector-Java</artifactId>
<version>5.1.34_1</version>
</dependency>
第二步:实体层
public class User {
private Integer bookid;
private String bookname;
private Integer bookprice;
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 Integer getBookprice() {
return bookprice;
}
public void setBookprice(Integer bookprice) {
this.bookprice= bookprice;
}
}
第三步:搭建分层
A:dao层
接口:
//01.查询素有图书
public List<User> findAll();
接口实现:需要实现接口,继承JdbcDaoSupport 植入jdbcTemplate:简化jdbc操作
public class userdaoimpextends JdbcDaoSupport implements userdao {
// 需要植入 jdbcTemplate
publicList<User> findAll() {
String sql="select * from book";
//接口的一个实现类 匿名内部类
List<User> list=this.getJdbcTemplate().query(sql,new RowMapper<User>() {
public User mapRow(ResultSet rs,int i) throwsSQLException {
User book=new User();
book.setBookid(rs.getInt("bookid"));
book.setBookname(rs.getString("bookname"));
book.setBookprice(rs.getInt("bookprice"));
return book;
}
});
return list;
}
}
第四步:applicationContext.xml
<?xml version="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
">
<!--00.识别jdbc.properties文件-->
<context:property-placeholderlocation="jdbc.properties"></context:property-placeholder>
<!--01.建立数据源 Spring 内置的一个数据源 DriverManager-->
<beanid="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<propertyname="driverClassName"value="${jdbc.driverClassName}"></property>
<propertyname="url"value="${jdbc.url}"></property>
<propertyname="username"value="${jdbc.user}"></property>
<propertyname="password"value="${jdbc.password}"></property>
</bean>
<!--02.jdbcTemplate 配置-->
<beanid="jdbcTemplate"class="org.springframework.jdbc.core.JdbcTemplate">
<propertyname="dataSource"ref="dataSource"></property>
</bean>
<!--03.dao配置-->
<beanid="bookDao"class="cn.dao.imp.userdaoimp">
<propertyname="jdbcTemplate"ref="jdbcTemplate"></property>
</bean>
<!--04.service bookService-->
<beanid="bookService"class="cn.service.impl.BookDAOImpl">
<propertyname="dao"ref="bookDao"></property>
</bean>
</beans>
第五步:service层
实现层:
public class BookDAOImplimplements IBookService {
//植入dao
privateuserdao dao;
public List<User> findAll() {
return dao.findAll();
}
public userdao getDao() {
return dao;
}
public void setDao(userdao dao) {
this.dao= dao;
}
}
接口层:
//01.查询素有图书
public List<User> findAll();
第六步:单测类
@Test
// 01.jdbctemplate
public void test02(){
ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
IBookService service = (IBookService) ctx.getBean("bookService");
List<User> list = service.findAll();
for (User item:list) {
System.out.println(item.getBookname());
}
}