浏览 6881 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2010-02-22
其实使用IBatis3.0注解,相等于把IBatis中的全局xml配置信息编程式开发,非全局xml配置针对实体的映射文件采用。注解开发,好了,言归正传吧。\(^o^)/~ IbatisSessionFactory 的代码重点如下: 注意看有颜色部分代码:
package com.vnvtrip.search.ibatis.utils; import java.sql.Connection; import java.util.Properties; import java.util.logging.Logger; import javax.sql.DataSource; import org.apache.commons.dbcp.BasicDataSourceFactory; import org.apache.ibatis.mapping.Environment; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.session.TransactionIsolationLevel; import org.apache.ibatis.transaction.TransactionFactory; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import com.vnvtrip.search.ibatis.blog.model.BlogMapper; /** * * * 创建类似Hibernate的HibernateSessionFactoryUtils的类 * @author longgangbai * */ @SuppressWarnings("serial") public class IbatisSessionFactory implements SqlSessionFactory { public static final Logger LOG = Logger.getLogger(IbatisSessionFactory.class.getName()); /** * 针对特殊的应用 * 读取config.properties文件,存放了数据库连接参数等信息 */ private static final Properties props = new Properties() { { try { load(IbatisSessionFactory.class.getResourceAsStream("jdbc.properties")); } catch (Exception e) { e.printStackTrace(); } } }; /** 根据参数得到某个具体的iBatis数据源工厂实例 */ public static SqlSessionFactory getInstance() { if (sqlSessionFactory == null) { try { initSqlSessionFactory(); } catch (Exception e) { e.printStackTrace(); } } return sqlSessionFactory; } /** * 采用注解配置的方式 * @throws Exception */ private static void initSqlSessionFactory() throws Exception { //获取数据源 DataSource datasource = BasicDataSourceFactory.createDataSource(props); //创建事务工厂 TransactionFactory transFactory = new JdbcTransactionFactory(); //获取上下文环境 Environment environment = new Environment("development", transFactory, datasource); //创建配置对象 Configuration config = new Configuration(environment); //添加映射的类 config.addMapper(BlogMapper.class); //构建的会话工厂 SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder(); sqlSessionFactory = builder.build(config); } // 真正“做事”的实例 private static SqlSessionFactory sqlSessionFactory = null; public Configuration getConfiguration() { return sqlSessionFactory.getConfiguration(); } public SqlSession openSession() { return sqlSessionFactory.openSession(); } public SqlSession openSession(boolean arg0) { return sqlSessionFactory.openSession(arg0); } public SqlSession openSession(Connection arg0) { return sqlSessionFactory.openSession(arg0); } public SqlSession openSession(ExecutorType arg0) { return sqlSessionFactory.openSession(arg0); } public SqlSession openSession(ExecutorType arg0, boolean arg1) { return sqlSessionFactory.openSession(arg0, arg1); } public SqlSession openSession(ExecutorType arg0, Connection arg1) { return sqlSessionFactory.openSession(arg0, arg1); } @Override public SqlSession openSession(TransactionIsolationLevel arg0) { return sqlSessionFactory.openSession(arg0); } @Override public SqlSession openSession(ExecutorType arg0, TransactionIsolationLevel arg1) { return sqlSessionFactory.openSession(arg0, arg1); } }
jdbc.properties内容如下:
driverClassName=com.mysql.jdbc.Driver url=jdbc\:mysql\://localhost\:3306/search username=root password=123456
Blog对应的映射文件Mapper文件: 针对insert,update,delete,select很简单忽略。
package com.vnvtrip.search.ibatis.blog.model; import org.apache.ibatis.annotations.Select;
BlogDAO中的代码如下: @Override
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2010-08-28
最近也在研究 ,ibatis3,不知道怎么细化ibatis3的操作,在ibatis2 中有dao框架,在ibatis3中呢?
|
|
返回顶楼 | |