生活随笔
收集整理的這篇文章主要介紹了
使用iBATIS3.0完成增删改查
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
為什么80%的碼農都做不了架構師?>>> ??
使用iBATIS3.0完成增刪改查
iBATIS3.0和以前的版本有一些改變,不過學過以前版本的再學習3.0應該不是太難,3.0要求JDK1.5支持,因為其中增加了注解和泛型,這些都是JDK1.5才有的。好了廢話不多說,先來利用iBATIS3做下簡單的增刪改查吧。
??? 首先到Apache(http://www.apache.org/)網站下載iBATIS3的jar 包,我下載的是ibatis-3-core-3.0.0.227.zip,解壓后吧那個jar文件(ibatis-3-core-3.0.0.227.jar)添加到工程就可以了,還有一個文件(ibatis-3-core-src-3.0.0.227.zip)是源代碼,可以用來查看源代碼的,使用eclipse可以用它來關聯源代碼。
??? 在MyEclipse新建一個Java Project,結構如下圖
??? 在jdbc.properties文件是映射文件要使用的,其內容如下:
Properties代碼
driver=com.mysql.jdbc.Driver? url=jdbc\:mysql\://localhost\:3306/test? username=root? password=123456 SqlMapper.xml是iBATIS的配置文件,其代碼如下:
Xml代碼
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration? PUBLIC "-//ibatis.apache.org//DTD Config 3.0//EN"? "http://ibatis.apache.org/dtd/ibatis-3-config.dtd"> <configuration> <properties resource="jdbc.properties"/> <typeAliases> <typeAlias type="cn.ibatis3.test.Person" alias="Person"/> </typeAliases> <environments default="development"> <environment id="development"> <transactionManager type="JDBC"/> <dataSource type="POOLED"> <property name="driver" value="${driver}"/> <property name="url" value="${url}"/> <property name="username" value="${username}"/> <property name="password" value="${password}"/> </dataSource> </environment> </environments> <mappers> <mapper resource="cn/ibatis3/test/person.xml"/> </mappers> </configuration> 上面文件中的sql映射文件person.xml代碼如下:
Xml代碼
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper? PUBLIC "-//ibatis.apache.org//DTD Mapper 3.0//EN"? "http://ibatis.apache.org/dtd/ibatis-3-mapper.dtd"> <!--? --> <mapper namespace="cn.ibatis3.test.PersonMapper"> <select id="selectPerson" parameterType="java.lang.Integer" resultType="Person"> ??????? select * from person where id = #{id}? </select> <select id="selectAll" resultType="Person"> ??????? select * from person? </select> <select id="selectPersonsByName" resultType="Person" parameterType="String"> ??????? select * from person where name like #{name}? </select> <insert id="insertPerson" parameterType="Person"> ??????? insert into person(name,birthday,sex)? ??????? values(#{name},#{birthday},#{sex})? </insert> <delete id="deletePerson" parameterType="Person"> ??????? delete from person where id=#{id}? </delete> <update id="updatePerson" parameterType="Person"> ??????? update person set name=#{name},birthday=#{birthday},sex=#{sex}? ??????? where id=#{id}? </update> </mapper> ???? 注意:在iBATIS3中,屬性parameterMap是不推薦使用的,在以后的版本可能會去掉這個屬性。
Person.java的代碼如下:
Java代碼
package cn.ibatis3.test;? import java.util.Date;? public class Person {? private int id = 0;? private String name = "";? private String sex = "male";? private Date birthday = null;? public Person() {? ??? }? //省略getter 和 setter 方法 @Override public String toString() {? return "id=" + id + "\t" + "name=" + name + "\t" + "sex=" + sex + "\t" ??????????????? + "birthday=" + new java.sql.Date(birthday.getTime()).toString();? ??? }? }? ??? iBATIS官方推薦我們使用單例模式創建一個sessionFactory,我這里也提供一個sessionFactory.java,呵呵,僅供參考:
Java代碼
package cn.ibatis3.test;? import java.io.IOException;? import java.io.Reader;? import org.apache.ibatis.io.Resources;? import org.apache.ibatis.session.SqlSessionFactory;? import org.apache.ibatis.session.SqlSessionFactoryBuilder;? public final class SessionFactory {? private String resource="cn/ibatis3/test/SqlMapper.xml";? private SqlSessionFactory sqlSessionFactory=null;? private static SessionFactory sessionFactory=new SessionFactory();? private SessionFactory() {? try {? ??????????? Reader reader=Resources.getResourceAsReader(resource);? ??????????? sqlSessionFactory=new SqlSessionFactoryBuilder().build(reader);? ??????? } catch (IOException e) {? ??????????? System.out.println("#IOException happened in initialising the SessionFactory:"+e.getMessage());? throw new ExceptionInInitializerError(e);? ??????? }? ??? }? public static SessionFactory getInstance() {? return sessionFactory;? ??? }? public SqlSessionFactory getSqlSessionFactory() {? return sqlSessionFactory;? ??? }? }? ??? 基于接口的編程(還有就是iBATIS3的注解也是在接口方法上的,關于注解以后有機會再講,它也是iBATIS3的一個新特性),DAO層的接口PersonMapper.java代碼如下:
Java代碼
package cn.ibatis3.test;? import java.util.List;? public interface PersonMapper {? ??? Person selectById(Integer id);? ??? List<Person> selectAll();? ??? List<Person> selectPersonsByName(String name);? void insert(Person person);? void delete(Person person);? void update(Person person);? }? 接口的實現類PersonDao.java代碼如下:
Java代碼
package cn.ibatis3.test;? import java.util.ArrayList;? import java.util.List;? import org.apache.ibatis.session.SqlSession;? import org.apache.ibatis.session.SqlSessionFactory;? public class PersonDao implements PersonMapper {? private SqlSessionFactory sessionFactory = SessionFactory.getInstance()? ??????????? .getSqlSessionFactory();? public Person selectById(Integer id) {? ??????? Person person = new Person();? ??????? SqlSession session = null;? try {? ??????????? session = sessionFactory.openSession();? ??????????? person = (Person) session.selectOne(? "cn.ibatis3.test.PersonMapper.selectPerson", id);? ??????? } finally {? ??????????? session.close();? ??????? }? return person;? ??? }? @SuppressWarnings("unchecked")? public List<Person> selectAll() {? ??????? List<Person> persons = new ArrayList<Person>();? ??????? SqlSession session = null;? try {? ??????????? session = sessionFactory.openSession();? ??????????? persons = session? ??????????????????? .selectList("cn.ibatis3.test.PersonMapper.selectAll");? ??????? } finally {? ??????????? session.close();? ??????? }? return persons;? ??? }? public void delete(Person person) {? ??????? SqlSession session = null;? try {? ??????????? session = sessionFactory.openSession();? ??????????? session.delete("cn.ibatis3.test.PersonMapper.deletePerson", person);? ??????????? session.commit();? ??????? } finally {? ??????????? session.close();? ??????? }? ??? }? public void insert(Person person) {? ??????? SqlSession session = null;? try {? ??????????? session = sessionFactory.openSession();? ??????????? session.insert("cn.ibatis3.test.PersonMapper.insertPerson", person);? ??????????? session.commit();? ??????? } finally {? ??????????? session.close();? ??????? }? ??? }? public void update(Person person) {? ??????? SqlSession session = null;? try {? ??????????? session = sessionFactory.openSession();? ??????????? session.insert("cn.ibatis3.test.PersonMapper.updatePerson", person);? ??????????? session.commit();? ??????? } finally {? ??????????? session.close();? ??????? }? ??? }? @SuppressWarnings("unchecked")? public List<Person> selectPersonsByName(String name) {? ??????? List<Person> persons = new ArrayList<Person>();? ??????? SqlSession session = null;? try {? ??????????? session = sessionFactory.openSession();? ??????????? System.out.println(name);? ??????????? persons = session.selectList(? "cn.ibatis3.test.PersonMapper.selectPersonsByName", "%" ??????????????????????????? + name + "%");? ??????????? session.commit();? ??????? } finally {? ??????????? session.close();? ??????? }? return persons;? ??? }? }? 最后是表的創建:
Sql代碼
DROP TABLE IF EXISTS `test`.`person`;? CREATE TABLE? `test`.`person` (? ? `id` int(10) unsigned NOT NULL auto_increment,? ? `name` varchar(20) default NULL,? ? `sex` varchar(8) default NULL,? ? `birthday` datetime default NULL,? PRIMARY KEY? (`id`)? ) ENGINE=InnoDB DEFAULT CHARSET=utf8;? 好了,做為一次新技術的體驗吧,歡迎大家指出其中的錯誤或者是不恰當的地方。
17
頂
6
踩
分享到:
iBATIS3.0學習(二)使用iBATIS3.0注解完 ...
- 2010-02-03 13:00
- 瀏覽 11154
- 評論(14)
- 收藏
- 分類:企業架構
- 相關推薦
評論
14 樓 lyb520320 2012-02-01?? 引用
ibatis3沒有試驗過打印sql,ibatis2通過jdk動態代理使用log4j打印sql,估計ibatis3也差不多,看下這個連接,主要看log4j.properties配置,這個文件要放到src目錄下
http://winyee.iteye.com/blog/457216
13 樓 grandboy 2012-02-01?? 引用
這個我在網上搜索了好長時間,也試了網上說的一些辦法,就是無法把產生的sql打印出來,不知道各位有沒有成功經驗?如果有人成功,請告訴詳細方法或者給兄弟一個鏈接。
要親自實驗成功的方法。多謝。
12 樓 lyb520320 2012-01-31?? 引用
qwj528 寫道
樓主<mapper namespace="cn.ibatis3.test.PersonMapper"> namespace這個屬性有什么作用嗎? 但是必須要有namespace。 從你這個例子來看好像是個接口。
namespace僅僅是為了區分不同的xml的一個名稱,至于是什么看你自己的代碼習慣
11 樓 qwj528 2012-01-31?? 引用
樓主<mapper namespace="cn.ibatis3.test.PersonMapper"> namespace這個屬性有什么作用嗎? 但是必須要有namespace。 從你這個例子來看好像是個接口。
10 樓 fengyie007 2010-06-21?? 引用
Java代碼
public List<Person> selectAll() {??? ??????? List<Person> persons = new ArrayList<Person>();??? ??????? SqlSession session = null;??? try {??? ??????????? session = sessionFactory.openSession();??? ??????????? persons = session??? ??????????????????? .selectList("cn.ibatis3.test.PersonMapper.selectAll");??? ??????? } finally {??? ??????????? session.close();??? ??????? }??? return persons;??? ??? }??
轉載于:https://my.oschina.net/wzzz/blog/206635
總結
以上是生活随笔為你收集整理的使用iBATIS3.0完成增删改查的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。