j2ee学习
这几天学了一下ibatis,感觉ibatis的查询还是做得不错的。就是单个对象的增删改查没有hiberante用着简洁,还得自己写sql语句维护。ibatis和hibernate做为持久层orm,分别代表了两个流派,hibernate在处理多表复杂的联合查询时不方便,可以用ibatis的特长去弥补,这两种技术能够相互补充,感觉还是挺不错的。当然缓存同步还是有一定的问题,但只要能合理处理就行了。
lunzi
2008-06-21 14:18:26
阅读:281
评论:0
引用:0
官方的例子稍作小改:
package com.test.pojos;
public class Account {
private int id;
private String firstName;
private String lastName;
private String emailAddress;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getEmailAddress() {
return emailAddress;
}
public void setEmailAddress(String emailAddress) {
this.emailAddress = emailAddress;
}
}
Account.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE sqlMap
PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN"
"http://ibatis.apache.org/dtd/sql-map-2.dtd">
<sqlMap namespace="Account">
<!-- Use type aliases to avoid typing the full classname every time. -->
<typeAlias alias="Account" type="com.test.pojos.Account"/>
<typeAlias alias="PageUtil" type="com.test.ibatis.sqlutil.PageUtil"/>
<!-- Result maps describe the mapping between the columns returned
from a query, and the class properties. A result map isn't
necessary if the columns (or aliases) match to the properties
exactly. -->
<resultMap id="AccountResult" class="Account">
<result property="id" column="ACC_ID"/>
<result property="firstName" column="ACC_FIRST_NAME"/>
<result property="lastName" column="ACC_LAST_NAME"/>
<result property="emailAddress" column="ACC_EMAIL"/>
</resultMap>
<!-- Select with no parameters using the result map for Account class. -->
<select id="selectAllAccounts" resultMap="AccountResult">
select * from ACCOUNT
</select>
<!-- A simpler select example without the result map. Note the
aliases to match the properties of the target result class. -->
<select id="selectAccountById" parameterClass="int" resultClass="Account">
select
ACC_ID as id,
ACC_FIRST_NAME as firstName,
ACC_LAST_NAME as lastName,
ACC_EMAIL as emailAddress
from ACCOUNT
where ACC_ID = #id#
</select>
<sql id="selectSql">
select * from ACCOUNT
</sql>
<!-- Insert example, using the Account parameter class -->
<insert id="insertAccount" parameterClass="Account">
insert into ACCOUNT (
ACC_ID,
ACC_FIRST_NAME,
ACC_LAST_NAME,
ACC_EMAIL
)values (
#id#, #firstName#, #lastName#, #emailAddress#
)
</insert>
<!-- Update example, using the Account parameter class -->
<update id="updateAccount" parameterClass="Account">
update ACCOUNT set
ACC_FIRST_NAME = #firstName#,
ACC_LAST_NAME = #lastName#,
ACC_EMAIL = #emailAddress#
where
ACC_ID = #id#
</update>
<!-- Delete example, using an integer as the parameter class -->
<delete id="deleteAccountById" parameterClass="int">
delete from ACCOUNT where ACC_ID = #id#
</delete>
<select id="getList" parameterClass="com.test.pojos.Account" resultMap="AccountResult">
select * from ACCOUNT
<dynamic prepend="where">
<isNotNull prepend="and" property="firstName">
ACC_FIRST_NAME = #firstName#
</isNotNull>
<isNotNull prepend="and" property="lastName">
ACC_LAST_NAME = #lastName#
</isNotNull>
</dynamic>
order by ACC_ID
</select>
</sqlMap>
AccountDAO.java
void saveAccount(Account account);
List<Account> queryAll();
public List<Account> getAuthorList(Account criteria, int pageNo, int pageSize);
AccountDAOImpl.java
public void saveAccount(Account account) {
super.getSqlMapClientTemplate().insert("insertAccount", account);
}
public List<Account> queryAll() {
return super.getSqlMapClientTemplate().queryForList("selectAllAccounts");
}
public List<Account> getAuthorList(Account criteria, int pageNo,int pageSize) {
return getSqlMapClientTemplate().queryForList("getList", criteria, pageNo, pageSize);
}
测试类:
package com.test.dao.test;
import java.util.List;
import com.test.dao.AccountDAO;
import com.test.pojos.Account;
public class AccountDAOTest extends BaseDaoTestCase {
private AccountDAO accountDao;
public void setAccountDao(AccountDAO accountDao) {
this.accountDao = accountDao;
}
public void testSaveAccount(){
Account account = new Account();
account.setEmailAddress("test2");
account.setFirstName("first3");
account.setLastName("62");
accountDao.saveAccount(account);
super.assertNotNull(account);
}
public void testQueryAll(){
List<Account> all = accountDao.queryAll();
System.out.println(all.size());
super.assertNotNull(all);
}
public void testQueryList(){
Account criteria = new Account();
criteria.setFirstName("first3");
int pageNo = 0;
int pageSize = 10;
List<Account> list = accountDao.getAuthorList(criteria, pageNo, pageSize);
System.out.println("list.size():"+list.size());
super.assertNotNull(list);
}
}
lunzi
2008-06-20 01:19:53
阅读:103
评论:0
引用:0
今天开发遇到错误码如:no matching editors or conversion strategy found的提示。
从google上一搜,找到http://hi.baidu.com/kingtoon_go/blog/item/f3a44d1658f6f14e21a4e95c.html片文章原来是Java实现接口错误所致,我和他犯了同样的错误。
从google上一搜,找到http://hi.baidu.com/kingtoon_go/blog/item/f3a44d1658f6f14e21a4e95c.html片文章原来是Java实现接口错误所致,我和他犯了同样的错误。
lunzi
2008-06-19 11:48:07
阅读:107
评论:0
引用:0
If you use Eclipse 3.x:
Help -> Software updates -> Find and install....
Choose "Search for new features to install".
Click Add Update Site..., and type "FreeMarker" for Name and "http://www.freemarker.org/eclipse/update" for URL.
Check the box of the "FreeMarker" feature.
"Next"-s until it is installed...
hail from:http://freemarker.sourceforge.net/eclipse.html
Help -> Software updates -> Find and install....
Choose "Search for new features to install".
Click Add Update Site..., and type "FreeMarker" for Name and "http://www.freemarker.org/eclipse/update" for URL.
Check the box of the "FreeMarker" feature.
"Next"-s until it is installed...
hail from:http://freemarker.sourceforge.net/eclipse.html
lunzi
2008-06-05 09:19:49
阅读:101
评论:0
引用:0
Hibernate 进行多表关联查询Hibernate对多个表进行查询时,查询结果是多个表的笛卡尔积,
或者称为“交叉”连接。 例如:from Student, Book from Student as stu, Book as boo from
Student stu, Book boo注意:让查询中的Student和Book均是表student和book对应的类名,它的
名字一定要和类的名字相同,包括字母的大小写。别名应该服从首字母小写的规则是一个好习惯,
这和Java对局部变量的命名规范是一致的。
例如:
String sTest = "from tBookInfo book, BookSelection sel where book.id = sel.bookId";
Collection result = new ArrayList();
Transaction tx = null;
try {
Session session = HibernateUtil.currentSession();
tx = session.beginTransaction();
Query query = session.createQuery(sql);
result = query.list();
tx.commit();
} catch (Exception e) {
throw e;
} finally {
HibernateUtil.closeSession();
}
ArrayList sList = (ArrayList) result;
Iterator iterator1 = sList.iterator();
while (iterator1.hasNext()) {
Object[] o = (Object[]) iterator1.next();
tBookInfo bookInfo = (tBookInfo) o[0];
BookSelection bookSelect = (BookSelection) o[1];
System.out.println("BookInfo-Title: " + bookInfo.getTitle());
System.out.println("BookSelection-BookSelectionId: " + bookSelect.getId());
}
转自:http://develop.csai.cn/java/200805231428261254.htm
lunzi
2008-06-11 10:14:34
阅读:372
评论:0
引用:0
共两种方式:
1、在freemarker.properties
配置tag_syntax = square_bracket
这也是开发中最常用的方式
2、还有一种是硬编码方式
设置
Configuration cfg = new Configuration();
cfg.setTagSyntax(Configuration.SQUARE_BRACKET_TAG_SYNTAX);
//用Configuration自带的常量指定解析格式
lunzi
2008-06-04 14:49:26
阅读:201
评论:0
引用:0
http://hi.baidu.com/qq200871844
http://www.ibm.com/developerworks/cn/java/j-genericdao.html
http://www.ibm.com/developerworks/cn/java/j-genericdao.html
lunzi
2008-04-16 23:09:49
阅读:186
评论:0
引用:0
package com.test;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.FileSystemResource;
public class SpringMain {
/**
* @param args
*/
public static void main(String[] args) {
SpringBean bean = null;
XmlBeanFactory factory = new XmlBeanFactory(new ClassPathResource("applicationContext.xml"));
bean = (SpringBean)factory.getBean("test1");
bean.sayHello();
ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");
bean = (SpringBean)act.getBean("test1");
bean.sayHello();
factory = new XmlBeanFactory(new FileSystemResource("F:\\ccc\\workspace_zuhe\\Test\\src\\applicationContext.xml"));
bean = (SpringBean)factory.getBean("test1");
bean.sayHello();
}
}
lunzi
2008-04-16 02:27:04
阅读:315
评论:0
引用:0
一、设置全局使用它的
<result-types>
<result-type name="freemarker"
class="com.opensymphony.webwork.views.freemarker.FreemarkerResult"
default="true">
<param name="contentType">
text/html;charset=utf-8
</param>
</result-type>
</result-types>
二、freemarker.properties经常配置的内容
#template_update_delay=3600
tag_syntax = square_bracket
template_exception_handler = ccp.suddenattack.xtiger.XTigerTemplateExceptionHandler
number_format=#
三、action的部分配置如:
<action name="addNodeForm"
class="ccp.suddenattack.action.news.CateTreeAction" method="addNodeForm">
<result name="success" >
<param name="location">
/WEB-INF/pages/admin/tree/addNodeForm.html
</param>
</result>
</actio
lunzi
2008-04-14 00:12:31
阅读:331
评论:0
引用:0
支持最好的一种方法
一、在webwork.properties配置
webwork.objectFactory = spring
webwork.devMode = true
webwork.action.extension=dhtml
二、在web.xml中加入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context/db/datasource.xml,/WEB-INF/context/news/news.xml</param-value>
</context-param>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
//这段是处理编码用的
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
lunzi
2008-04-14 00:05:42
阅读:136
评论:0
引用:0
