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");

你可能感兴趣的:(mybatis,jetbrick)