1、将支持包ibatis-2.3.0.677.jar拷贝到lib下
2、加入 sqlmapconfig.xml 核心配置文件到 src 下
3、修改核心配置文件,将数据库连接写好。
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMapConfig PUBLIC "-//ibatis.apache.org//DTD SQL Map Config 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-config-2.dtd"> <sqlMapConfig> <!-- commitRequired主要进行事务处理。 false表示自动事务处理。 --> <transactionManager type="JDBC" commitRequired="true"> <dataSource type="SIMPLE"> <property name="JDBC.Driver" value="org.gjt.mm.mysql.Driver" /> <property name="JDBC.ConnectionURL" value="jdbc:mysql://localhost:3306/test" /> <property name="JDBC.Username" value="root" /> <property name="JDBC.Password" value="mysqladmin" /> </dataSource> </transactionManager> <!-- 配置映射文件 --> <sqlMap resource="cn/mldn/ibatis/vo/News.xml" /> </sqlMapConfig>
4、修改News.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE sqlMap PUBLIC "-//ibatis.apache.org//DTD SQL Map 2.0//EN" "http://ibatis.apache.org/dtd/sql-map-2.dtd"> <sqlMap namespace="News"> <!-- 定义别名,为要映射的包.类名,定义一个别名,方便使用 --> <typeAlias alias="News" type="cn.mldn.ibatis.vo.News" /> <!-- 对返回结果集进行映射。 --> <resultMap id="NewsResult" class="News"> <result property="id" column="id" /> <result property="title" column="title" /> <result property="content" column="content" /> <result property="counts" column="counts" /> <result property="postDate" column="post_date" /> </resultMap> <!-- 所有的操作语句都必须定义在映射文件中。 查询全部 id:表示调用这个语句时的唯一标识 resultMap:表示返回多条数据,数据按照上面定义的映射来进行转换。 --> <select id="findAllNews" resultMap="NewsResult"> select * from news </select> <!-- 按id查询 parameterClass:传入的参数类型 resultClass:表示查询结果为一条数据,类型为News 当参数只有一个时,名字任意定义。 --> <select id="findNewsById" parameterClass="int" resultClass="News"> select * from news WHERE id = #id# </select> <!-- 添加操作 由于需要传递多个参数,因此参数名要与News对象中的属性对应。 --> <insert id="saveNews" parameterClass="News"> insert into news (title,content,counts,post_date) VALUES (#title#,#content#,#counts#,#postDate#) </insert> <!-- 修改 --> <update id="updateNews" parameterClass="News"> update news SET title = #title#,content = #content#,counts = #counts# WHERE id = #id# </update> <!-- 按id删除--> <delete id="deleteNewsById" parameterClass="int"> delete from news where id = #id# </delete> </sqlMap>
5、创建 SqlMapSessionFactory类
package cn.mldn.ibatis.dbc; import java.io.Reader; import java.sql.SQLException; import com.ibatis.common.resources.Resources; import com.ibatis.sqlmap.client.SqlMapClient; import com.ibatis.sqlmap.client.SqlMapClientBuilder; import com.ibatis.sqlmap.client.SqlMapSession; public class SqlMapSessionFactory { private static String CONFIG_FILE_LOCATION = "SqlMapConfig.xml"; private static final ThreadLocal<SqlMapSession> threadLocal = new ThreadLocal<SqlMapSession>(); private static SqlMapClient client; private static String configFile = CONFIG_FILE_LOCATION; static { try { Reader reader = Resources.getResourceAsReader(CONFIG_FILE_LOCATION); client = SqlMapClientBuilder.buildSqlMapClient(reader); reader.close(); } catch (Exception e) { System.err.println("%%%% Error Creating SessionFactory %%%%"); e.printStackTrace(); } } private SqlMapSessionFactory() { } public static SqlMapSession getSession() throws SQLException { SqlMapSession session = (SqlMapSession) threadLocal.get(); if (session == null || session.getCurrentConnection() == null || session.getCurrentConnection().isClosed()) { session = client.openSession(); threadLocal.set(session); } return session; } public static void closeSession() { SqlMapSession session = (SqlMapSession) threadLocal.get(); threadLocal.set(null); if (session != null) { session.close(); } }
5、编写实现类(接口就不贴了)
package cn.mldn.ibatis.dao.impl; import java.util.List; import cn.mldn.ibatis.dao.NewsDAO; import cn.mldn.ibatis.dbc.SqlMapSessionFactory; import cn.mldn.ibatis.vo.News; public class NewsDAOImpl implements NewsDAO { public boolean doCreate(News news) throws Exception { SqlMapSessionFactory.getSession().insert("saveNews", news); return true; } public boolean doDelete(int id) throws Exception { SqlMapSessionFactory.getSession().delete("deleteNewsById", id); return true; } public boolean doUpdate(News news) throws Exception { SqlMapSessionFactory.getSession().update("updateNews", news); return true; } public List<News> findAll() throws Exception { List all = SqlMapSessionFactory.getSession() .queryForList("findAllNews"); return all; } public News findById(int id) throws Exception { News news = (News) SqlMapSessionFactory.getSession().queryForObject( "findNewsById", id); return news; } }
package cn.mldn.ibatis.service.impl; import java.sql.SQLException; import java.util.List; import cn.mldn.ibatis.dao.NewsDAO; import cn.mldn.ibatis.dao.impl.NewsDAOImpl; import cn.mldn.ibatis.dbc.SqlMapSessionFactory; import cn.mldn.ibatis.service.NewsService; import cn.mldn.ibatis.vo.News; public class NewsServiceImpl implements NewsService { private NewsDAO newsdao = new NewsDAOImpl(); public boolean doCreate(News news) { boolean flag = false; try { SqlMapSessionFactory.getSession().startTransaction(); flag = this.newsdao.doCreate(news); SqlMapSessionFactory.getSession().commitTransaction(); } catch (Exception e) { e.printStackTrace(); try { SqlMapSessionFactory.getSession().endTransaction(); } catch (SQLException e1) { } } finally { SqlMapSessionFactory.closeSession(); } return flag; } public boolean doDelete(int id) { boolean flag = false; try { SqlMapSessionFactory.getSession().startTransaction(); flag = this.newsdao.doDelete(id); SqlMapSessionFactory.getSession().commitTransaction(); } catch (Exception e) { e.printStackTrace(); try { SqlMapSessionFactory.getSession().endTransaction(); } catch (SQLException e1) { } } finally { SqlMapSessionFactory.closeSession(); } return flag; } public boolean doUpdate(News news) { boolean flag = false; try { SqlMapSessionFactory.getSession().startTransaction(); flag = this.newsdao.doUpdate(news); SqlMapSessionFactory.getSession().commitTransaction(); } catch (Exception e) { e.printStackTrace(); try { SqlMapSessionFactory.getSession().endTransaction(); } catch (SQLException e1) { } } finally { SqlMapSessionFactory.closeSession(); } return flag; } public List<News> findAll() { List all = null; try { all = this.newsdao.findAll(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { SqlMapSessionFactory.closeSession(); } return all; } public News findById(int id) { News news = null; try { news = this.newsdao.findById(id); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { SqlMapSessionFactory.closeSession(); } return news; }