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

}

MyBATIS使用CRUD的更多相关文章

  1. MyBatis:CRUD功能

    在前面已经自动生成了mapper和pojo,接下来我们实现mybatis的CRUD功能,先新建service.controller层的方法. 这里的sid是一个开源的id生成类,写完后,我们还需要在启 ...

  2. Mybatis的CRUD案例

    一.Mybatis增删改查案例 上一节<Mybatis入门和简单Demo>讲了如何Mybatis的由来,工作流程和一个简单的插入案例,本节主要继上一讲完整的展示Mybatis的CRUD操作 ...

  3. 【MyBatis】MyBatis实现CRUD操作

    1.实现基本CRUD功能 使用MyBatis对数据完整的操作,也就是CRUD功能的实现.根据之前的内容,要想实现CRUD,只需要进行映射文件的配置. 范例:修改EmpMapper.xml文件,实现CR ...

  4. SpringBoot 整合 Mybatis 进行CRUD测试开发

    今天来和大家分享下 Spring Boot 整合 MyBatis 的 CRUD 测试方法开发.因为 MyBaits 有两种开发形式,一种基于注解,一种基于 xml . SpringBoot配置文件也有 ...

  5. 基于mybatis的CRUD

    u  基于Mybatis的CRUD u  掌握MyBatis的结果类型-resultMap和resultType u  掌握MyBatis的参数类型 u  掌握#和$两种语法 1      基于myb ...

  6. 05 Mybatis的CRUD操作和Mybatis连接池

    1.CRUD的含义 CRUD是指在做计算处理时的增加(Create).读取(Retrieve)(重新得到数据).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中 ...

  7. 03 Mybatis:05.使用Mybatis完成CRUD

    mybatis框架:共四天 明确:我们在实际开发中,都是越简便越好,所以都是采用不写dao实现类的方式.不管使用XML还是注解配置. 第二天:mybatis基本使用 mybatis的单表crud操作 ...

  8. mybatis(CRUD)

    3.mybatis(CRUD) 有了mybatis,我们要对数据库进行增删改查只需要操作接口和mapper.xml文件,然后进行测试就可以了. 实例代码如下: 接口 public interface ...

  9. MyBatis之CRUD

    1 mybatis框架介绍 1.1回顾jdbc操作数据库的过程 1.2 mybatis开发步骤 A.提供一个SqlMapperConfig.xml(src目录下),该文件主要配置数据库连接,事务,二级 ...

  10. MyBatis Tutorial – CRUD Operations and Mapping Relationships – Part 1---- reference

    http://www.javacodegeeks.com/2012/11/mybatis-tutorial-crud-operations-and-mapping-relationships-part ...

随机推荐

  1. 一口一口吃掉Hibernate(八)——Hibernate中inverse的用法

    一.Inverse是hibernate双向关系中的基本概念.inverse的真正作用就是指定由哪一方来维护之间的关联关系.当一方中指定了“inverse=false”(默认),那么那一方就有责任负责之 ...

  2. sql server 深入使用 总结 part1

    1   OUTPUT  应用场景 : 1.1.对于INSERT,可以引用inserted表以查询新行的属性.         insert into [表名] (a) OUTPUT Inserted. ...

  3. 数据库神器:Navicat Premium

    Navicat premium是一款数据库管理工具.将此工具连接数据库,你可以从中看到各种数据库的详细信息.包括报错,等等.当然,你也可以通过他,登陆数据库,进行各种操作.Navicat Premiu ...

  4. Codeforces Beta Round #10 B. Cinema Cashier (树状数组)

    题目大意: n波人去k*k的电影院看电影. 要尽量往中间坐,往前坐. 直接枚举,贪心,能坐就坐,坐在离中心近期的地方. #include <cstdio> #include <ios ...

  5. MVCC浅析(转)

    在并发读写数据库时,读操作可能会不一致的数据(脏读).为了避免这种情况,需要实现数据库的并发访问控制,最简单的方式就是加锁访问.由于,加锁会将读写操作串行化,所以不会出现不一致的状态.但是,读操作会被 ...

  6. javascript 偏移量

    通过四个属性可以获得元素的偏移量: 1.offsetHeight: 元素在垂直方向上占用的空间的大小,(像素).包括元素的高度,(可见的)水平滚动条的高度,上边框高度和下边框高度. 2.offsetW ...

  7. ios基础-编程规范

    养成良好的编程习惯,是開始一门新语言的首要前提. (一)文档结构规范 1.建立Libs目录,存放第三方库 2.建立Tools目录,存放自己封装的类库 3.建立ViewControllers目录,存放全 ...

  8. eclipse导入myeclipse的web项目没法识别问题解决方法

    1.进入项目目录,找到.project文件,打开. 2.找到<natures>...</natures>代码段. 3.在第2步的代码段中加入如下标签内容并保存: <nat ...

  9. EasyUI - NumberBox组件

    效果: html代码: <input type ="text" id ="box"/> JS代码: $(function () { $('#box' ...

  10. prevPage / nextPage in jQuery Mobile | George Nixon's Blog

    prevPage / nextPage in jQuery Mobile | George Nixon's Blog ui.prevPage[0].id I finally worked this o ...