4)这一步可以写mybatis的总配置文件,初始加载连接数据库。
<configuration>
<typeAliases>
<package />
</typeAliases>
<environments default="development">
<environment>
<transactionManager type="JDBC" />
<dataSource type="POOLED">
<property value="Oracle.jdbc.driver.OracleDriver" />
<property value="jdbc:oracle:thin:@localhost:1521:ORCL" />
<property value="sunxun" />
<property value="123" />
</dataSource>
</environment>
</environments>
<mappers>
<mapper resource="org/liky/dao/INewsDAO.xml" />
</mappers>
</configuration>
5)建立MyBATISSqlSessionFactory类:
import Java.io.InputStream;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBATISSqlSessionFactory {
// 配置文件的所在位置和名称
private static String CONFIG_FILE_LOCATION = "mybatis-config.xml";
// 用来实现连接池的,该类类似Map集合。
private static final ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();
// Hibernate用来读取配置文件的类
private static InputStream is;
// 用来建立连接的,该类就是连接池,使用单例设计模式
private static SqlSessionFactory sqlsessionFactory;
// 备用的配置文件位置
private static String configFile = CONFIG_FILE_LOCATION;
// 静态块,类加载时最先执行
static {
try {
// 加载配置文件到内存中
is = Resources.getResourceAsStream(configFile);
// 建立连接池以及里面的连接
sqlsessionFactory = new SqlSessionFactoryBuilder().build(is);
} catch (Exception e) {
System.err.println("%%%% Error Creating SessionFactory %%%%");
e.printStackTrace();
}
}
private MyBATISSqlSessionFactory() {
}
/**
* 取得数据库连接对象
*
* @return Session
* @throws HibernateException
*/
public static SqlSession getSession() {
// 先从ThreadLocal中取得连接。
SqlSession session = (SqlSession) threadLocal.get();
// 如果手头没有连接,则取得一个新的连接
if (session == null) {
session = sqlsessionFactory.openSession();
// 把取得出的连接记录到ThreadLocal中,以便下次使用。
threadLocal.set(session);
}
return session;
}
/**
* 连接关闭的方法
*
* @throws HibernateException
*/
public static void closeSession() {
SqlSession session = (SqlSession) threadLocal.get();
// 将ThreadLocal清空,表示当前线程已经没有连接。
threadLocal.set(null);
// 连接放回到连接池
if (session != null) {
session.close();
}
}
}
6)写service层实现的借口:
import java.util.List;
import java.util.Map;
import org.liky.vo.News;
public interface INewsService {
public void insert(News news) throws Exception;
public News findById(int id) throws Exception;
public List<News> findAll() throws Exception;
public Map<String, Object> list(int pageNo, int pageSize, String column,
String keyword) throws Exception;
}
7)解耦合,写service的工厂:
import org.liky.service.INewsService;
import org.liky.service.impl.NewsServiceImpl;
public class ServiceFactory {
public static INewsService getINewsServiceInstance() {
return new NewsServiceImpl();
}
}
8)service的实现类:
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.liky.dao.INewsDAO;
import org.liky.dbc.MyBATISSqlSessionFactory;
import org.liky.service.INewsService;
import org.liky.vo.News;
public class NewsServiceImpl implements INewsService {
public List<News> findAll() throws Exception {
List<News> all = null;
try {
all = MyBATISSqlSessionFactory.getSession().getMapper(
INewsDAO.class).findAll();
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
MyBATISSqlSessionFactory.closeSession();
}
return all;
}
public News findById(int id) throws Exception {
News news = null;
try {
news = MyBATISSqlSessionFactory.getSession().getMapper(
INewsDAO.class).findById(id);
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
MyBATISSqlSessionFactory.closeSession();
}
return news;
}
public void insert(News news) throws Exception {
try {
MyBATISSqlSessionFactory.getSession().getMapper(INewsDAO.class)
.doCreate(news);
MyBATISSqlSessionFactory.getSession().commit();
} catch (Exception e) {
e.printStackTrace();
MyBATISSqlSessionFactory.getSession().rollback();
throw e;
} finally {
MyBATISSqlSessionFactory.closeSession();
}
}
public Map<String, Object> list(int pageNo, int pageSize, String column,
String keyword) throws Exception {
Map<String, Object> map = new HashMap<String, Object>();
// 设置要向DAO中传递的参数
Map<String, Object> params = new HashMap<String, Object>();
params.put("column", column);
params.put("keyword", "%" + keyword + "%");
params.put("endNumber", pageNo * pageSize);
params.put("startNumber", (pageNo - 1) * pageSize);
try {
map.put("allNews", MyBATISSqlSessionFactory.getSession().getMapper(
INewsDAO.class).findAllSplit(params));
map.put("count", MyBATISSqlSessionFactory.getSession().getMapper(
INewsDAO.class).getAllCount(params));
} catch (Exception e) {
e.printStackTrace();
} finally {
MyBATISSqlSessionFactory.closeSession();
}
return map;
}
}