简单的 jdbcTemplate案例 + 切面 环绕通知
案例简介 :
使用jdbcTemplate 对 emp表 进行CRUD(增删改查)并且记录每次方法的执行时间并保存
1、切面类
用于记录每次方法的执行时间并保存
public class MyAspect {
EmpService empService = new EmpService();
public Object aroundHandler(ProceedingJoinPoint jointPoint) throws Throwable {
long start = System.currentTimeMillis();
System.out.println("开始执行"+jointPoint.getSignature().getName()+"方法");
Object name = jointPoint.proceed();
long end = System.currentTimeMillis()-start;
System.out.println("执行方法耗时"+end+"毫秒");
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
jdbcTemplate.update("insert into t_data set fm=?, tim=?",jointPoint.getSignature().getName(),end);
return name;
}
}
2、实体类
emp 的字段 和构造方法
package pojo;
public class T_Emp {
private Integer id;
private String name;
private String content;
private Integer did;
public T_Emp() {
}
public T_Emp(String name, String content, Integer did) {
this.name = name;
this.content = content;
this.did = did;
}
public T_Emp(Integer id, String name, String content, Integer did) {
this.id = id;
this.name = name;
this.content = content;
this.did = did;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Integer getDid() {
return did;
}
public void setDid(Integer did) {
this.did = did;
}
@Override
public String toString() {
return "T_Emp{" +
"id=" + id +
", name='" + name + '\'' +
", content='" + content + '\'' +
", did=" + did +
'}';
}
}
3、增删改查的方法
public class EmpService implements EmpServiceI {
//添加
@Override
public void ins(T_Emp t_emp) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
jdbcTemplate.update("insert into t_emp set name=?, content=?,did=?", t_emp.getName(), t_emp.getContent(), t_emp.getDid());
}
//删除
@Override
public void del(int id) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
jdbcTemplate.update("delete from t_emp where id =?", id);
}
//修改
@Override
public void upt(T_Emp t_emp) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
jdbcTemplate.update("update t_emp set name=?, content=?,did=? where id = ?", t_emp.getName(), t_emp.getContent(), t_emp.getDid(),t_emp.getId());
}
//查询列表
@Override
public List<T_Emp> sel() {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
List<T_Emp> tEmps = null;
tEmps = jdbcTemplate.query("select * from t_emp ", new RowMapper<T_Emp>() {
@Override
public T_Emp mapRow(ResultSet resultSet, int i) throws SQLException {
T_Emp t_emp = new T_Emp();
t_emp.setId(resultSet.getInt("id"));
t_emp.setName(resultSet.getString("name"));
t_emp.setContent(resultSet.getString("content"));
t_emp.setDid(resultSet.getInt("did"));
return t_emp;
}
});
return tEmps;
}
//查询单条数据
@Override
public T_Emp seldan(int i) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
JdbcTemplate jdbcTemplate = applicationContext.getBean(JdbcTemplate.class);
T_Emp tEmp = null;
tEmp = jdbcTemplate.queryForObject("select * from t_emp where id= ?", new RowMapper<T_Emp>() {
@Override
public T_Emp mapRow(ResultSet resultSet, int i) throws SQLException {
T_Emp t_emp = new T_Emp();
t_emp.setId(resultSet.getInt("id"));
t_emp.setName(resultSet.getString("name"));
t_emp.setContent(resultSet.getString("content"));
t_emp.setDid(resultSet.getInt("did"));
return t_emp;
}
},i);
return tEmp;
}
}
4、增删改查方法的接口
public interface EmpServiceI {
void ins(T_Emp t_emp);
void del(int id);
void upt(T_Emp t_emp);
List<T_Emp> sel();
T_Emp seldan(int i);
}
5、测试类
public class JdbcTemplateTest {
public static void main(String[] args) {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml");
EmpServiceI empServiceI = applicationContext.getBean(EmpServiceI.class);
// //添加
// T_Emp t_emp = new T_Emp("jake","very good",10);
// empServiceI.ins(t_emp);
// //删除
// empServiceI.del(53);
// //修改
// T_Emp t_emp2 = new T_Emp(54,"jakelove","very good",100);
// empServiceI.upt(t_emp2);
// 查询单条数据
T_Emp t_emp = empServiceI.seldan(54);
System.out.println(t_emp.toString());
//查询 列表
// List<T_Emp> t_emps = empServiceI.sel();
//
// for (T_Emp t_emp1:t_emps) {
// System.out.println(t_emp1.toString());
// }
}
}
6、xml配置
<?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:aop="http://www.springframework.org/schema/aop"
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/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">
<!--开启spring的自动代理-->
<aop:aspectj-autoproxy/>
<!--配置目标类-->
<bean id="deptService" class="service.EmpService"></bean>
<!--配置切面-->
<bean id="myAspect" class="aspect.MyAspect"></bean>
<aop:config>
<aop:aspect ref="myAspect">
<aop:pointcut id="poi1" expression="execution(* service.EmpService.*(..))"/>
<aop:around method="aroundHandler" pointcut-ref="poi1"></aop:around>
</aop:aspect>
</aop:config>
<!--jdbcTemplate 配置-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="url" value="${jdbc.url}"></property>
<property name="password" value="${jdbc.password}"></property>
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="username" value="${jdbc.username}"></property>
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
还有一个jdbc.properties文件
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/yuangong?characterEncoding=UTF-8
jdbc.username=root
jdbc.password=root
?characterEncoding=UTF-8 为了防止sql乱码 不加这语句 加入数据库的中文就会成 ???
=========
还有所需jar包就不跟大家发了,看一下图片吧