Tapestry5 与Ibatis3 的整合
1、大家到各自的网站上下载相应的包好了。我只罗列一下我所用到的Lib: antlr-runtime-3.1.1.jar commons-codec-1.3.jar commons-lang-2.4.jar ibatis-3-core-3.0.0.216.jar javassist.jar log4j-1.2.14.jar mysql-connector-java-5.0.5.jar slf4j-api-1.5.10.jar slf4j-log4j12-1.5.10.jar stax2-api-3.0.1.jar tapestry-core-5.1.0.5.jar tapestry-ioc-5.1.0.5.jar tapestry5-annotations-5.1.0.5.jar woodstox-core-lgpl-4.0.7.jar2、Create TableDROP TABLE IF EXISTS `test`.`user`;CREATE TABLE `test`.`user` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(45) NOT NULL, `password` varchar(45) NOT NULL, PRIMARY KEY (`id`)) ENGINE=InnoDB;3、package com.sample.model;public class User { private long id; private String name; private String password; // getter and setter ....}4、我把Configuration.xml和UserMapper.xml都放在src目录下,这样在部署的时候,就是生成在classes,也就是类路径的根目录下。Configuration.xml:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"> </properties> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="poolPingEnabled" value="${pingenable}"/> <property name="poolPingQuery" value="${pingquery}"/> <property name="poolPingConnectionsNotUsedFor" value="${pingnotusetime}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="UserMapper.xml"/> </mappers> </configuration> UserMapper.xml:<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN" "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <mapper namespace="com.sample.model.UserMapper"> <select id="selectUser" parameterType="int" resultType="com.sample.model.User"> select * from user where id = #{id} </select></mapper> jdbc.properties:jdbc.driver=com.mysql.jdbc.Driverjdbc.url=jdbc:mysql://localhost/test?autoReconnect=truejdbc.username=rootjdbc.password=rootpingenable=truepingquery=SELECT 1pingoldertime=0pingnotusetime=36000005、package com.sample.web.services;public class AppModule { public static SqlSessionFactory buildSqlSessionFactory() { try { String resource = "Configuration.xml"; Reader reader = Resources.getResourceAsReader(resource); return new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { logger.warn("failed to build SqlSessionFactory: ", e); return null; } } private static Logger logger = LoggerFactory.getLogger(AppModule.class);}package com.sample.model;public interface UserMapper { public User selectUser(int id);}package com.pc.sample.web.pages;public class Layout { @InjectService("SqlSessionFactory") private SqlSessionFactory sqlMapper; public String getUserName() { if ( sqlMapper == null ) { return "null-mapper"; } SqlSession session = sqlMapper.openSession(); try { UserMapper userMapper = session.getMapper(UserMapper.class); if ( userMapper == null ) { return "null-userMapper"; } User user = userMapper.selectUser(1); if ( user == null ) { return "null-user"; } return user.getName(); } catch (Exception e) { return "exception-" + e.getMessage(); } finally { session.close(); } }}?
几个注意事项:
1,因为我的IBatis的配置文件Configuration.xml是放在类路径的根目录下,所以在初始化SqlSessionFactory的时候,直接用String resource = "Configuration.xml";就行了,否则需要添加相应的路径,比如:把Configuration.xml与User类放在一起,也就是在 com.sample.model这个package中,那么就要写成:String resource = "com/sample/model/Configuration.xml";
同样,在Configuration.xml中,指定UserMapper.xml的规则也是这样的。
2,UserMapper的使用。Mapper的使用是IBatis3中才有的新功能,也是IBatis用户指南中推荐使用的方式。因为这样使用的话,就完全避免了类型的强制转换,实现了类型安全。
需要注意的是UserMapper只是一个接口。我们不需要提供这个接口的具体实现。IBatis3会自动生成一个具体的实例。
其中的方法名必须与UserMapper.xml中的select语句的id一样。在我的例子中是selectUser.
另外,此方法的返回值的类型必须与UserMapper.xml中配置的returnType一致。
最后要提醒的是UserMapper.xml中的namespace必须是UserMapper的全类名,在本例中就是com.sample.model.UserMapper