#
本文只介绍几个常用的快捷键
Ctrl+Space // Complete Code, 与输入法冲突, 改为Alt+S
Ctrl+D // 行复制, 或选定字符串复制
Ctrl+Shift+N // 打开文件
Ctrl+J // 插入代码模板
Alt+Insert // Generate, 生成Constructor, setter, getter
Ctrl+O // 覆盖方法
Ctrl+I // 实现方法
Ctrl+Alt+T // Surround With
Ctrl+/ // 行注释
Ctrl+Alt+L // 格式化代码
Alt+F7 // 查找使用情况
Shift+F6 // 重命名
F6 // 移动
Ctrl+Shift+F7 // 显示字符串使用次数
Alt+Delete // 安全删除
映射文件是的生成方式
在HB2.1.x时代, 使用的是middlegen生成hbm, 再通过hbm2java生成pojo
<middlegen appname="${name}" prefsdir="${build.gen-src.dir}" gui="${gui}" databaseurl="${database.url}" initialContextFactory="${java.naming.factory.initial}" providerURL="${java.naming.provider.url}" datasourceJNDIName="${datasource.jndi.name}" driver="${database.driver}" username="${database.userid}" password="${database.password}">
<hibernate destination="${build.gen-src.dir}" package="${name}.hibernate" genXDocletTags="false" genIntergratedCompositeKeys="false" javaTypeMapper="middlegen.plugins.hibernate.HibernateJavaTypeMapper"/></middlegen>
<target name="codegen" depends="middlegen"> <taskdef name="hbm2java" classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask" classpathref="project.classpath"/> <hbm2java output="${build.classes.dir}" classpathref="project.classpath" config="${src.dir}/config.xml"> <fileset dir="${build.gen-src.dir}/maintain/hibernate"> <include name="**/*.hbm.xml"/> </fileset> </hbm2java></target>
<!-- config.xml -->
<codegen> <generate renderer="net.sf.hibernate.tool.hbm2java.BasicRenderer"/></codegen>
到HB3.0.x时代, hbm的定义更灵活, middlegen已不更新, 使用idea手写hbm, pojo(这时已比较熟悉)
下一阶段, 使用Hibernate Annotations(处于Beta状态)
说明:
只介绍ddl --> hbm --> pojo方式
很多hibernate tools是基于eclipse的, 所以没用
Spring+Hibernate中, 集合映射如果使用lazy="true", 当PO传到View层时, 出现未初始化session已关闭的错误,只能在dao先初始化
parent.getChilds().size();
Spring提供Open Session In View来解决这个问题, 有两种方式
1. Interceptor
<!-- =========== OpenSession In View pattern ==============-->
<bean id="openSessionInViewInterceptor"
class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors" ref="openSessionInViewInterceptor"/>
<property name="mappings">
<props>
......
</props>
</property>
</bean>
2. Filter
<web-app>
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate.support.OpenSessionInViewFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>
</web-app>
说明: 个人更喜欢用Interceptor方式, filter是在web.xml中定义
有了OpenSessionInView,并不是一切就ok了。简单的crud可以,但对于复杂业务逻辑就要想点法子。
OSIV默认是request per session的, 所以即使没有显式update(po),Hibernate也会帮你保存的,dao的业务判断就无效,还好有evict()可用。
update code
// Controller
public ModelAndView update() {
p = dao.getVo(id);
bindObject(request);
dao.update(p);
}
// Dao
public Object getVo(id) {
p = getHibernateTemplate().get(clazz, id);
p.getChilds().size();
getHibernateTemplate().evict(p);
return p;
}
public void update(p) {
oldP = getVo(id);
// ++--(oldP);
getHibernateTemplate().update(p);
// ++--(p);
}
idea 5.0还是有些小bug的, 使用起来有点不爽, 5.0.1估计有不少bug fix
下载中,
http://download.jetbrains.com/idea/idea-5.0.1.exe官方的bug fix
IntelliJ IDEA 5.0.1
1. New annotation @NonNls for marking fields, etc. not presented in the UI & not needing localization.
2. The Hard coded string literals inspection/quick fix now available in UI Designer forms.
摘要: Groovy is an agile dynamic language for the Java 2 Platform that has many of the features that people like so much in languages like Python, Ruby and Smalltalk, making them available to Java developer...
阅读全文
纯JDBC操作, 对某些项目来说, 也许更好, Spring JDBC Framework让你不用关心Connection, Statement, ResultSet.
定义数据源
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName">
<value>java:/comp/env/jdbc/oracle</value>
</property>
</bean> 定义事务管理器
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
定义dao
<bean id="customerDAO" class="com.waterye.dao.impl.CustomerDAOImpl">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
</bean> demo
public class CustomerDaoImpl extends JdbcDaoSupport implements CustomerDAO {
private DataSource dataSource;
pirvate TransactionManager transactionManager;
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
public void setTransactionManager(DataSourceTransactionManager transactionManager) {
this.transactionManager = transactionManager;
}
public Map get(Integer id) throws Exception {
String querySql = "select * from customer where id = ?";
return getJdbcTemplate().queryForMap(querySql, new Object[] { id });
}
public void insert(final Map customer) throws Exception {
String seqSql = "select customer_seql.nextval from dual";
String insertSql = "insert into customer (id, code, name, status) values (?, ?, ?, ?)";
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
int id = jdbcTemplate.queryForInt(seqSql);
Object[] params = new Object[] { new Integer(id), customer.get("code"), customer.get("name"), map.get("status") };
jdbcTemplate.update(insertSql, params);
}
}
}
public void update(final Map customer) throws Exception {
//
}
public void delete(Integer id) throws Exception {
String deleteSql = "delete from customer where id = ?";
TransactionTemplate transactionTemplate = new TransactionTemplate(transactionManager);
transactionTemplate.execute(new TransactionCallbackWithoutResult() {
protected void doInTransactionWithoutResult(TransactionStatus status) {
getJdbcTemplate().update(deleteSql, new Object[] { id });
}
}
}
public List findValidCustomers() throws Exception {
String querySql = "select * from customer where status = 'valid' order by code";
return getJdbcTemplate().query(querySql, new OracleColumnMapRowMapper());
}
}
说明:
1. 没有使用声明性事务, 使用编程式事务
2. 没有使用POJO模式,使用HashMap, (
ActiveMapper还在sandbax状态)
3. OracleColumnMapRowMapper implements RowMapper, 实现oracle风格到java bean风格mapping
如: 字段customer_id 对应rowMap.get("customerId");