MyEclipse不提供自己主动生成,这里提供mybatis文件包和开发文档 http://download.csdn.net/detail/u010026901/7489319

自己建立配置文件,

<?xml version="1.0" encoding="UTF-8" ?

>

<!DOCTYPE configuration

PUBLIC "-//mybatis.org//DTD Config 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-config.dtd">

<configuration>

<environments default="development">

<environment id="development">

<transactionManager type="JDBC" />

<dataSource type="POOLED">

<property name="driver" value="oracle.jdbc.driver.OracleDriver" />

<property name="url" value="jdbc:oracle:thin:@192.168.2.55:ORCL" />

<property name="username" value="ysk" />

<property name="password" value="123" />

</dataSource>

</environment>

</environments>

<mappers>

<!--这里填写dao接口映射的daoImpl,mybatis不是映射pojo(vo)类,而是dao类-->

<mapper resource="com/kane/dao/NewsDAOImpl.xml" />

</mappers>

</configuration>

自己配置链接的sqlsessionFactory类,相当于hibernate的sessionFactory相应一个数据库

package com.kane.dbc;





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-conf.xml";





// 用来实现连接池的,该类类似Map集合。

private static final ThreadLocal<SqlSession> threadLocal = new ThreadLocal<SqlSession>();

// MyBATIS用来读取配置文件的类

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

}

}

}

通过配置文件实现daoimpl而不是java类

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE mapper

PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

"http://mybatis.org/dtd/mybatis-3-mapper.dtd">





<mapper namespace="com.kane.dao.INewsDAO">

<insert id="doCreate" parameterType="News">

<!--jdbcType表示这个内容同意为空,为Oracle指明类型

若表中的列名与vo中列名不同,能够重命名

SELECT

id,title,content,pub_date AS pubDate,type_id AS typeId,photo,tname FROM news n,news_type nt

WHERE id = #{id} AND n.type_id = nt.tid -->

INSERT INTO NEWS(news_id,news_title,image_id,news_content,news_time) VALUES

(sys_guid(),#{news_title},#{image_id},#{news_content,jdbcType=VARCHAR},#{news_time,jdbcType=DATE})

</insert>

<delete id="doRemove" parameterType="java.lang.Integer">

DELETE FRON News WHEER news_id=#{id}

</delete>

<update id="doUpdate" parameterType="News">

UPDATE News SET news_title=#{news_title},image_id=#{image_id},news_content=#{news_content},news_time=#{news_time}

WHERE news_id=#{news_id}

</update>

<select id="findAll" resultType="News">

SELECT news_id,news_title,image_id,news_content,news_time FROM News

</select>

<select id="findById" resultType="News">

SELECT news_id,news_title,image_id,news_content,news_time FROM News=#{id}

</select>

<select id="findAllSplit" resultType="News" parameterType="java.util.Map">

SELECT temp.* FROM (SELECT news_id,news_title,image_id,news_content,news_time,ROWNUM rn 

FROM News WHERE ${column} LIKE #{keyword} AND ROWNUM &lt;=#{endNum})

temp WHERE temp.rn>#{startNum}

</select>

<select id="getAllCount" resultType="java.lang.Integer" parameterType="java.util.Map">

SELECT COUNT(*) FROM News WHERE ${column} LIKE #{keyword}

</select>

</mapper>

接着service。在serviceImpl中

package com.kane.service.impl;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import com.kane.dao.INewsDAO;

import com.kane.dbc.MyBATISSqlSessionFactory;

import com.kane.service.INewsService;

import com.kane.vo.News;





public class NewsServiceImpl implements INewsService{





public List<News> findAll(){

List<News> all=null;

try {

all=MyBATISSqlSessionFactory.getSession().getMapper(INewsDAO.class).findAll();

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

MyBATISSqlSessionFactory.closeSession();

}

return all;

}

public void insert(News news) throws Exception {

try {

MyBATISSqlSessionFactory.getSession().getMapper(INewsDAO.class).doCreate(news);

MyBATISSqlSessionFactory.getSession().commit();

} catch (Exception e) {

MyBATISSqlSessionFactory.getSession().rollback();

e.printStackTrace();

}

finally{

MyBATISSqlSessionFactory.closeSession();

}

}

public Map<String, Object> list(int pageNo, int pageSize, String column,String keyword){

Map<String,Object> map=new HashMap<String,Object>();

Map<String,Object> params=new HashMap<String, Object>();

params.put("column",column);

params.put("keyword",keyword);

params.put("endNum",pageSize*pageNo);

params.put("startNum",(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) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

MyBATISSqlSessionFactory.closeSession();

}

return map;

}

public void remove(int id) throws Exception {

// TODO Auto-generated method stub



}

public void update(News news) throws Exception {

// TODO Auto-generated method stub



}

public News findById(int id){

News news=null;

try {

news=MyBATISSqlSessionFactory.getSession().getMapper(INewsDAO.class).findById(id);

} catch (Exception e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

finally{

MyBATISSqlSessionFactory.closeSession();

}

return news;

}

}

然后junit測试

@Test

public void testList() throws Exception {

System.out.println(ServiceFactory.getNewsServiceInstance().list(1, 5,

"news_title", "12"));

}

最新文章

  1. 6. UIImageView 的使用
  2. android studio使用中遇到的问题
  3. Dubbo初探
  4. hibernate多对多映射关系实现
  5. [ZJOI3527][Zjoi2014]力
  6. 操作笔记:tomcat在正式环境
  7. 典型:Eayui项目aspx页面引用js
  8. svn配置
  9. 【转】Oracle中dual表的用途介绍
  10. Hypeiron Planning/Essbase修改规划类型名称
  11. Multidimensional Arrays
  12. HDU 1213 How Many Tables(模板——并查集)
  13. 在Spring Boot中使用数据缓存
  14. C#如何以管理员身份运行程序 转
  15. Gradle nexus 解决开发机器不连网无法下载gradle插件问题
  16. redis命令参考和redis文档中文翻译版
  17. redis map存储的注意点
  18. etcd基本操作
  19. Jackson2.1.4 序列化对象时对属性的过滤
  20. 【8086汇编-Day1】预备知识

热门文章

  1. Reverse Words in a String | LeetCode OJ | C++
  2. 转:onConfigurationChanged的作用
  3. ModelAndView
  4. android wifi讲解 wifi列表显示
  5. UVALive 6931 Can&#39;t stop playing (Regionals 2014 &gt;&gt; Europe - Central)
  6. hdu 4771 Stealing Harry Potter&amp;#39;s Precious(bfs)
  7. 【REDO】删除REDO LOG重做日志组后需要手工删除对应的日志文件(转)
  8. python gzip 压缩文件
  9. Android 系统api实现定位及使用百度提供的api来实现定位
  10. SSH2三大框架整合出错(四)