Liferay MVCPortlet + iBatis

It's such a long time not using iBatis. I've forgotten how to use it, but with the great Internet, it's convenient to get an example and catch it soon.

My dev environment is:

1) Liferay development studio

2) Liferay sdk plugins 6.1.20

3) Liferay EE 6.1.20

4) JDK 1.6 or 1.7

5) Maven

Now, let's start!

1. Create a maven project with liferay-portlet-archtype

2. Create table in your database

3. Create Person Bean

public class Person {
private int id;
private String firstName;
private String lastName;
private Date birthDate;

... all getters and setters

4. Create ibatis mapping file person.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE sqlMap PUBLIC "-//iBATIS.com//DTD SQL Map 2.0//EN"
"http://www.ibatis.com/dtd/sql-map-2.dtd">

<sqlMap>
<typeAlias type="com.rujuan.model.Person" alias="person" />

<resultMap class="person" id="result">
<result property="id" column="id" />
<result property="firstName" column="firstName" />
<result property="lastName" column="lastName" />
<result property="birthDate" column="birthDate" />
</resultMap>

<select id="getPeople" resultMap="result">
select * from person
</select>

<select id="getPersonById" resultMap="result" parameterClass="int">
select * from person where id = #value#
</select>

<insert id="savePerson" parameterClass="person">
insert into person (id, firstName, lastName, birthDate) values (#id#,
#firstName#, #lastName#, #birthDate#)
</insert>

<update id="updatePerson" parameterClass="person">
update person set firstName = #firstName#, lastName = #lastName#, birthDate = #birthDate#
where id = #id#
</update>

<delete id="deletePerson" parameterClass="int">
delete from person where id = #value#
</delete>

</sqlMap>

5. create SqlMapConfig.xml. In this file, we put our database connection stuff.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMapConfig
PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-config-2.dtd">
<!-- Always ensure to use the correct XML header as above! -->
<sqlMapConfig>
<settings cacheModelsEnabled="true" enhancementEnabled="true"
lazyLoadingEnabled="true" maxRequests="32" maxSessions="10"
maxTransactions="5" useStatementNamespaces="false" />
<!-- Configure a datasource to use with this SQL Map using SimpleDataSource. 
Notice the use of the properties from the above resource -->
<transactionManager type="JDBC">
<dataSource type="SIMPLE">
<property name="JDBC.Driver" value="com.mysql.jdbc.Driver" />
<property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/liferay" />
<property name="JDBC.Username" value="root" />
<property name="JDBC.Password" value="root" />
</dataSource>
</transactionManager>


<!-- Identify all SQL Map XML files to be loaded by this SQL map. Notice 
the paths are relative to the classpath. For now, we only have one… -->
<sqlMap resource="com/rujuan/model/person.xml" />
</sqlMapConfig>


6. Write your util to get SqlMapClient

public class MyAppSqlConfig {
private static final SqlMapClient sqlMap;
static {
try {
String resource = "com/rujuan/model/SqlMapConfig.xml";
Reader reader = Resources.getResourceAsReader(resource);
sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException("Error initializing MyAppSqlConfig class. Cause: " + e);
}
}


public static SqlMapClient getSqlMapInstance() {
return sqlMap;
}
}


7. Write your PersonDao and PersonDaoImpl

public class PersonDaoImpl implements PersonDao {


private SqlMapClient sqlMapClient = MyAppSqlConfig.getSqlMapInstance();


public List<Person> getPeople() throws SQLException {
return (List<Person>) sqlMapClient.queryForList("getPeople");
}


public Person getPersonById(int id) throws SQLException {
Object object = sqlMapClient.queryForObject("getPersonById", id);
return object instanceof Person ? ((Person) object) : null;
}


public void savePerson(Person person) throws SQLException {
sqlMapClient.insert("savePerson", person);
}


public void updatePerson(Person person) throws SQLException {
sqlMapClient.update("updatePerson", person);
}


public void deletePerson(int id) throws SQLException {
sqlMapClient.delete("deletePerson", id);
}
}


8. Write your PeopleManager acts like a Facade

public class PeopleManager {

private PersonDao personDao = new PersonDaoImpl();


public List<Person> getAllPeople() throws SQLException{
return personDao.getPeople();
}

public Person getPersonById(int id) throws SQLException{
return personDao.getPersonById(id);
}

public void insertPerson(Person person) throws SQLException{
personDao.savePerson(person);
}


public void updatePerson(Person person) throws SQLException{
personDao.updatePerson(person);
}

public void deletePerson(int id) throws SQLException{
personDao.deletePerson(id);
}
}

9. In your controller, we do CRUD.

public class PeopleController extends MVCPortlet {
private Logger log = Logger.getLogger(PeopleController.class);
private PeopleManager pm = new PeopleManager();


public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
List<Person> people = null;
try {
people = pm.getAllPeople();
} catch (SQLException e) {
e.printStackTrace();
}
renderRequest.setAttribute("people", people);
super.doView(renderRequest, renderResponse);
}


public void addPerson(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException, SQLException {
String id = actionRequest.getParameter("id");
String firstName = actionRequest.getParameter("firstName");
String lastName = actionRequest.getParameter("lastName");
String birthDate = actionRequest.getParameter("birthDate");
log.info("birthDate: " + birthDate +"--"+id +"==" + firstName +"***" + lastName);
Person person = new Person(Integer.parseInt(id), firstName, lastName, new Date(birthDate));
pm.insertPerson(person);
}

public void deletePerson(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException, SQLException {
String id = actionRequest.getParameter("personId");
pm.deletePerson(Integer.parseInt(id));
}


public void updatePerson(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException, SQLException{
String id = actionRequest.getParameter("personId");
Person person = pm.getPersonById(Integer.parseInt(id));
actionRequest.setAttribute("person", person);
actionResponse.setRenderParameter("jspPage", "/update.jsp");
}

public void renewPerson(ActionRequest actionRequest, ActionResponse actionResponse)
throws IOException, PortletException, SQLException{
String id = actionRequest.getParameter("id");
String firstName = actionRequest.getParameter("firstName");
String lastName = actionRequest.getParameter("lastName");
String birthDate = actionRequest.getParameter("birthDate");
Person person = new Person(Integer.parseInt(id), firstName, lastName, new Date(birthDate));
pm.updatePerson(person);
}

}

10. Show our jsps

1) view.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>


<portlet:defineObjects />


<table border="1">
<tr id="theader">
<td>ID</td>
<td>First Name</td>
<td>Last Name</td>
<td>BirthDate</td>
<td>Operation</td>
</tr>
<c:forEach var="person" items="${people}">
<tr>
<td>
<portlet:actionURL name="updatePerson" var="updatePersonURL">
<portlet:param name="personId" value="${person.id}"/>
</portlet:actionURL>
<a href="<%= updatePersonURL %>">${person.id}</a>
</td>
<td>${person.firstName}</td>
<td>${person.lastName}</td>
<td>${person.birthDate}</td>
<td>
<portlet:actionURL name="deletePerson" var="deletePersonURL">
<portlet:param name="personId" value="${person.id}"/>
</portlet:actionURL>
<a href="<%= deletePersonURL %>">DELETE</a>
</td>
</tr>
</c:forEach>
</table>


<portlet:renderURL var="addPerson">
<portlet:param name="jspPage" value="/add.jsp" />
</portlet:renderURL>


<p>
<a href="<%=addPerson%>">Add Person</a>
</p>


2) Add.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>


<portlet:defineObjects />




<portlet:actionURL name="addPerson" var="addPersonURL">
</portlet:actionURL>


<form action="<%= addPersonURL %>" method="post">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="<portlet:namespace/>id" id="id" /></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="<portlet:namespace/>firstName" id="firstName" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="<portlet:namespace/>lastName" id="lastName" /></td>
</tr>
<tr>
<td>Birthday</td>
<td><input type="text" name="<portlet:namespace/>birthDate" id="birthDate" /></td>
</tr>
<tr>
<td><input type="submit" value="submit" /></td>
</tr>
</table>
</form>


3) update.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui"%>


<portlet:defineObjects />




<portlet:actionURL name="renewPerson" var="renewPersonURL">
</portlet:actionURL>


<form action="<%=renewPersonURL%>" method="post">
<table>
<tr>
<td>ID</td>
<td><input type="text" name="<portlet:namespace/>id" id="id"
value="${person.id}" readonly="readonly"/></td>
</tr>
<tr>
<td>First Name</td>
<td><input type="text" name="<portlet:namespace/>firstName"
id="firstName" value="${person.firstName}" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><input type="text" name="<portlet:namespace/>lastName"
id="lastName" value="${person.lastName}" /></td>
</tr>
<tr>
<td>BirthDate</td>
<td><input type="text" name="<portlet:namespace/>birthDate"
id="birthDate" value="${person.birthDate}" /></td>
</tr>
<tr>
<td><input type="submit" value="submit" /></td>
</tr>
</table>
</form>

Remember: Change MVCPortlet to our own controller in portlet.xml. 

All things are done!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值