发表于: 2017-12-02 22:22:54
2 769
今天完成的事情
今天按照师兄给的实例去写spring MVC,代码就不贴了,看下目录吧
东西比较多,理解的时候各层级不好理解,然后自己按照昨天的代码写了一个方法,查询ID为19的那一行
运行结果
但是我表内的数据不是为0啊
这是我的配置文件
<?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"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="buluo"/>
<bean id="placeholderConfig"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:jdbc.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<!--数据库驱动-->
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<!--数据库连接的URL-->
<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/fyc"/>
<!--数据库连接的用户名-->
<property name="user" value="root"/>
<!--数据库连接的密码-->
<property name="password" value="123456"/>
<!-- data source -->
<!--当连接池中的连接耗尽的时候c3p0一次同时获取的连接数。Default: 3 -->
<property name="acquireIncrement" value="3"/>
<!--初始化时获取三个连接,取值应在minPoolSize与maxPoolSize之间。Default: 3 -->
<property name="initialPoolSize" value="3"/>
<property name="minPoolSize" value="3"/>
<property name="maxPoolSize" value="20"/>
<!--最大空闲时间,60秒内未使用则连接被丢弃。若为0则永不丢弃。Default: 0 -->
<property name="maxIdleTime" value="60"/>
<!--每60秒检查所有连接池中的空闲连接。Default: 0 -->
<property name="idleConnectionTestPeriod" value="0"/>
<!-- JDBC的标准参数,用以控制数据源内加载的PreparedStatements数量。但由于预缓存的statements
属于单个connection而不是整个连接池。所以设置这个参数需要考虑到多方面的因素。
如果maxStatements与maxStatementsPerConnection均为0,则缓存被关闭。Default: 0-->
<property name="maxStatements" value="3"/>
<!-- c3p0是异步操作的,缓慢的JDBC操作通过帮助进程完成。扩展这些操作可以有效的提升性能 通过
多线程实现多个操作同时被执行。Default: 3-->
<property name="numHelperThreads" value="3"/>
</bean>
<!-- 二、创建mybatis会话工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="mapperLocations" value="classpath:StudentMapper.xml" />
</bean>
<!--sql语句和dao的映射关系类-->
<bean id="studentMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<property name="mapperInterface" value="buluo.dao.StudentDao"/>
<property name="sqlSessionFactory" ref="sqlSessionFactory"/>
</bean>
<!--执行通知的类-->
<bean id="LogAdvice" class="buluo.utils.LogAdvice"></bean>
<aop:config>
<!--ref为通知使用的对象-->
<aop:aspect id = "myAspect" ref="LogAdvice">
<aop:pointcut id="aspect" expression="execution(* buluo.service.StudentService.*Impl*(..))"/>
<!--确定类中的方法(有method确定)进行通知-->
<!--before前置通知-->
<aop:before method="before" pointcut-ref="aspect"/>
<!--after为后置通知-->
<aop:after method="after" pointcut-ref="aspect"/>
<!--后置异常通知-->
<aop:after-returning method="afterAdvice" pointcut-ref="aspect"/>
<!--出现异常通知-->
<aop:after-throwing method="afterException" pointcut-ref="aspect"/>
<!--环绕通知-->
<aop:around method="around" pointcut-ref="aspect"/>
</aop:aspect>
</aop:config>
</beans>
package buluo.utils;
import org.apache.log4j.Logger;
import org.aspectj.lang.ProceedingJoinPoint;
import java.text.SimpleDateFormat;
import java.util.Date;
public class LogAdvice {
private static Logger loggerAdvice = Logger.getLogger(LogAdvice.class);
public void before(){
System.out.println("这是前置通知");
}
public void after(){
System.out.println("这是后置通知");
}
public Object around(ProceedingJoinPoint pip)throws Throwable{
//获取组件类名
String className = pip.getTarget().getClass().getName();
// 获取调用方法名
String method = pip.getSignature().getName();
//取得数据库连接前时间
long begin = System.currentTimeMillis();
// 当前系统时间
String date = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss") .format(new Date());
Object obj = pip.proceed();
//取得数据库连接后时间+
long end = System.currentTimeMillis();
//sqlTime为数据库响应时间
int sqlTime=(int)(end - begin);
String msg = date + ",执行了" + className + "." + method + "()";
loggerAdvice.info(msg + "\t数据库响应时间:" + sqlTime);
return obj;
}
public void afterException(){
System.out.println("这是异常通知");
}
public void afterAdvice(){
System.out.println("这是一个后置异常通知");
}
}
明天的计划
跑通jetty:run,把禅道按规范重新拆一下
遇到的问题
Could not autowire. There is more than one bean of 'StudentDao' type.
Beans:
studentDao (StudentDao.java) studentMapper (ApplicationContext.xml)
less... (Ctrl+F1)
Checks autowiring problems in a bean class.
原因是缺少一个@Service注释,在实现里面注释完后可以运行
今天的收获
按照任务一的service写了springMVC,但是还没有完全实现
任务进度:任务2步骤4
任务开始时间:11月22日
任务结束时间:11月30日
预计延期至12月3日
禅道:http://task.ptteng.com/zentao/project-task-399.html
评论