When I do this, I found it's very easy to integrate Spring and iBatis, but when I integrate them with portlet. I met some interesting problems.
Let me show you my way to do all these thing. I wrote another blog about liferay+iBatis. It has some same steps like person.xml, Person.java. I won't mention here again.
1. We need to change our SqlMapConfig.xml, because our database connection configuration will be put in our springconfiguration file.
SqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//iBATIS.com//DTD SQL Map Config 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-config-2.dtd">
<sqlMapConfig>
<sqlMap resource="com/rujuan/model/person.xml" />
</sqlMapConfig>
2. spring-ibatis.xml. Our core file, we put our configuration here.
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<context:annotation-config />
<context:component-scan base-package="com.rujuan.dao" />
<context:component-scan base-package="com.rujuan.service" />
<context:component-scan base-package="com.rujuan.controller" />
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean class="org.springframework.web.portlet.mvc.annotation.AnnotationMethodHandlerExceptionResolver" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.UrlBasedViewResolver">
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
</bean>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/liferay" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="sqlMapClient" class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="configLocation" value="classpath:com/rujuan/model/SqlMapConfig.xml" />
<property name="dataSource" ref="dataSource" />
</bean>
<!-- When we don't use annotation to wire them together, we can use this. but there's problem. -->
<!-- <bean id="personDaoImpl" class="com.rujuan.dao.PersonDaoImpl">
<property name="sqlMapClient" ref="sqlMapClient" />
</bean>
<bean id="peopleManager" class="com.rujuan.service.PeopleManager">
<property name="personDao" ref="personDaoImpl"></property>
</bean>
<bean id="pmcontroller" class="com.rujuan.controller.PeopleController">
<property name="peopleManager" ref="peopleManager"></property>
</bean>
<bean id="messageSource"
class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basenames">
<list>
<value>content.Language</value>
</list>
</property>
</bean> -->
</beans>
3. In order to ask web container read the spring-ibatis.xml, we need to add the file in our portlet.xml
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/spring-ibatis.xml</value>
</init-param>
4. Now. it's time to wire them together.
1) PersonDaoImpl.java
@Repository(value="personDaoImpl")
public class PersonDaoImpl implements PersonDao {
@Autowired
private SqlMapClient sqlMapClient;
... Others are the same as previous blog
2) PeopleManager.java
@Service(value = "peopleManager")
public class PeopleManager {
@Autowired
@Qualifier("personDaoImpl")
private PersonDao personDao;
...Others are the same as previous blog
5. This time, our controller doesn't need to extends MVCPortlet. Our controller is different now
@RequestMapping("VIEW")
@Controller
public class PeopleController {
private static Logger log = Logger.getLogger(PeopleController.class);
@Autowired
@Qualifier("peopleManager")
private PeopleManager peopleManager;
//When we use annotation, this is useless. All beans will be wired together at runtime.
// public PeopleManager getPeopleManager() {
// return peopleManager;
// }
//
// public void setPeopleManager(PeopleManager peopleManager) {
// this.peopleManager = peopleManager;
// System.out.println(this.peopleManager+"#############");
// }
@RenderMapping
public String renderPeopleList(RenderRequest request, RenderResponse response) {
List<Person> people = null;
log.info(this.peopleManager + "====");
try {
people = peopleManager.getAllPeople();
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("people", people);
return "index";
}
}
6. In our portlet.xml, we need to confirm our portlet class be Spring's DispatcherPortlet.
<portlet>
<portlet-name>RujuanPerson</portlet-name>
<display-name>RujuanPerson</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>view-jsp</name>
<value>/jsp/index.jsp</value>
</init-param>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/spring-ibatis.xml</value>
</init-param>
...more
All are done! It works fine!
Note:
1) The first problem I met is when I don't use annotation to wire all these beans together, I wire beans in spring-ibatis.xml. When I deploy the project, all beans will be installed. But when I render the page, I mean in the browser I access the portlet, in the render method, the peopleManager will become null again. I don't know why.
2) The second problem is Transaction Manager. In my spring-ibatis.xml. I didn't use transaction manager. If we use Hibernate, we have to use that. What's a good way to use them?
本文详细介绍了如何在portlet中整合Spring和iBatis,包括配置SqlMapConfig.xml、spring-ibatis.xml文件,以及如何在portlet.xml中引入配置文件。同时,文章还讨论了在不使用注解进行组件装配时遇到的问题,并提供了解决方案。

1356

被折叠的 条评论
为什么被折叠?



