国内更全面
解决程序疑难杂症

jetbrick-mvc 整合 mybatis

package com.seavision.xiche.card.test;

import javax.sql.DataSource;

import org.apache.ibatis.mapping.Environment;

import org.apache.ibatis.session.Configuration;

import org.apache.ibatis.session.SqlSession;

import org.apache.ibatis.session.SqlSessionFactory;

import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import org.apache.ibatis.transaction.TransactionFactory;

import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;

import jetbrick.ioc.annotation.Inject;

import jetbrick.ioc.annotation.IocBean;

import jetbrick.ioc.annotation.IocInit;


@IocBean

public class MyBatisUtil {
 @Inject("dataSource")

 private DataSource dataSource;
 private SqlSessionFactory sqlSessionFactory;
 @IocInit

 public void initSessionFactory() {
  TransactionFactory transactionFactory = new JdbcTransactionFactory();
  Environment environment = new Environment("development", transactionFactory, dataSource);
  Configuration configuration = new Configuration(environment);
  sqlSessionFactory = new SqlSessionFactoryBuilder().build(configuration);
 }
 public SqlSession getSession() {
  initSessionFactory();
  SqlSession session = sqlSessionFactory.openSession();
  return session;
 }
}

    使用:

    SqlMapper sqlMapper = new SqlMapper(myBatisUtil.getSession());
    List<Map<String, Object>> list = sqlMapper.selectList("select * from s_user");

企业站定制,我们更专业!

联系我们