发表于: 2017-10-18 18:54:39
1 698
今日完成:
完成spring集成mybatis,阅读aop概念。
明日计划:
还不知道切面是什么,在有个大概概念后把切面加入任务二中。
遇到的困难:
对代码错误地址的定位还是不够准确,今天的在配置集成的时候由于字母打错了,但是不理解异常的原因,最主要也是因为对集成的流程不了解,自己对着找了很久都找不到,这个很久是真的很久,实在不行求助师兄,然后发现是字母错了,希望以后能改掉粗心这个缺点,尽量少犯这种没意义的错误。
以下我说一下对集成操作数据库后的流程理解:
private static ApplicationContext ctx=new ClassPathXmlApplicationContext("spring.xml");
PersonDao perDao = (PersonDao) ctx.getBean("personMapper");
这一步是提取配置文件和直接拿出操作类,这个操作类是接口,是没有方法本体的,是通过另一个配置文件注入完成的,但流程在这两处代码无法看出来,都是在配置文件进行转化注入的。
public interface PersonDao {
Person takeById(int id);
List<Person> takeAll();
void addper(Person per);
void deleById(int id);
void updatePer(Person per);
Person takeByName(String name);
}
以下就是spring配置文件的数据了:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations" > <!-- PropertyPlaceholderConfigurer类中有个locations属性,接收的是一个数组,即我们可以在下面配好多个properties文件 -->
<list>
<value>classpath:conpool.properties</value>
</list>
</property>
</bean>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
<property name="driverClassName" value="${conpool.driver}"/>
<property name="url" value="${conpool.url}"/>
<property name="username" value="${conpool.username}"/>
<property name="password" value="${conpool.password}"/>
<property name="maxActive" value="${conpool.maxActive}"/>
<property name="maxIdle" value="${conpool.maxIdle}"/>
<property name="maxWait" value="${conpool.maxWait}"/>
<property name="initialSize" value="${conpool.initialSize}"/>
<property name="defaultAutoCommit" value="${conpool.defaultAutoCommit}"/>
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!--dataSource属性指定要用到的连接池-->
<property name="dataSource" ref="dataSource" />
<!--configLocation属性指定mybatis的核心配置文件-->
<property name="configLocation" value="classpath:mybatis.xml" />
</bean>
<bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="dao.PersonDao" />
<!--必须与接口映射配置文件相同-->
</bean>
这些连接数据库的操作大多都和之前的一样,其中在factory里加了configlocation属性来连接spring和mybatis,这个配置也很少,主要就是连接接口和进行数据操作的mapper
<typeAliases>
<typeAlias alias="Person" type="model.Person" />
</typeAliases>
<mappers>
<mapper resource="personMapper.xml" />
</mappers>
其中personMapper.xml和以前的一样,就是select id=什么 下面是sql语句那些,spring集合mybatis就是省略了以下的代码操作,就是省略了service,这里只列出其中一个方法
String resource= "config.xml";
InputStream input= MybatisAct.class.getClassLoader().getResourceAsStream(resource);
SqlSessionFactory sessionFactory=new SqlSessionFactoryBuilder().build(input);SqlSession session = sessionFactory.openSession();
public void takebyid(int n){
Person per1=session.selectOne("studentMapper.takebyid",n);
System.out.println(per1);
}
<bean id="personMapper" class="org.mybatis.spring.mapper.MapperFactoryBean">
<!--sqlSessionFactory属性指定要用到的SqlSessionFactory实例-->
<property name="sqlSessionFactory" ref="sqlSessionFactory" />
<!--mapperInterface属性指定映射器接口,用于实现此接口并生成映射器对象-->
<property name="mapperInterface" value="dao.PersonDao" />
<!--必须与接口映射配置文件相同-->
</bean>
我们操作时提取的bean就是上面的这个,这个接口在上面的配置中已经和操作数据库的配置文件连接了,之后直接用这个bean的实例就能对数据库操作了。
进度:
任务二装修,已完成mybatis整合,准备进行aop装修
任务开始时间:2017.10.7
预计demo时间:2017.10.15
是否有延期风险:
预计10.20交。
禅道:http://task.ptteng.com/zentao/my-task.html
评论