今天写了个ibatis3的deom,发上来权当笔记。
ibatis3下载地址 http://repo1.maven.org/maven2/org/mybatis/mybatis/3.2.2/ 下载(mybatis-3.2.2.jar)
1.项目目录
2.源码解析
Org.java pojo类
package com.ibatis3.test; public class Org { private String id; private String orgName; private String parentId; public Org(){ } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getOrgName() { return orgName; } public void setOrgName(String orgName) { this.orgName = orgName; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } }
sqlMapper.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="com.ibatis3.test.Org" alias="Org"/> </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="com/ibatis3/test/Org.xml"/> </mappers> </configuration>
Org.xml pojo类的配置文件
<?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="com.ibatis3.test.OrgMapper"> <select id="selectOrg" parameterType="String" resultType="Org"> select * from org where id = #{id} </select> <select id="selectAll" resultType="Org"> select * from org </select> <select id="selectOrgsByName" resultType="Org" parameterType="String"> select * from org where orgname like #{orgname} </select> <insert id="insertOrg" parameterType="Org"> insert into org(id,orgname,parentid) values(#{id},#{orgName},#{parentId}) </insert> <delete id="deleteOrg" parameterType="String"> delete from org where id=#{id} </delete> <update id="updateOrg" parameterType="Org"> update org set orgname=#{orgName},parentid=#{parentId} where id=#{id} </update> </mapper>
OrgMapper.java 数据层访问接口
package com.ibatis3.test; import java.util.List; public interface OrgMapper { Org selectById(String id); List<Org> selectAll(); List<Org> selectOrgsByName(String name); void insert(Org org); void delete(String id); void update(Org org); }
SessionFactory.java
package com.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 class SessionFactory { private String resource = "com/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; } }
OrgDao.java
package com.ibatis3.test; import java.util.ArrayList; import java.util.List; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; public class OrgDao implements OrgMapper { private SqlSessionFactory sessionFactory = SessionFactory.getInstance().getSqlSessionFactory(); private static String packageName = "com.ibatis3.test.OrgMapper."; @Override public Org selectById(String id) { Org org = new Org(); SqlSession session = null; try{ session = sessionFactory.openSession(); org = (Org)session.selectOne(packageName+"selectOrg",id); }catch(Exception ex){ ex.printStackTrace(); }finally{ session.close(); } return org; } @Override public List<Org> selectAll() { List<Org> orgs = new ArrayList<Org>(); SqlSession session = null; try { session = sessionFactory.openSession(); orgs = session .selectList(packageName+"selectAll"); } finally { session.close(); } return orgs; } @Override public List<Org> selectOrgsByName(String name) { List<Org> orgs = new ArrayList<Org>(); SqlSession session = null; try { session = sessionFactory.openSession(); System.out.println(name); orgs = session.selectList( packageName+"selectOrgsByName", "%" + name + "%"); session.commit(); } finally { session.close(); } return orgs; } @Override public void insert(Org org) { SqlSession session = null; try { session = sessionFactory.openSession(); session.insert(packageName+"insertOrg", org); session.commit(); } finally { session.close(); } } @Override public void delete(String id) { SqlSession session = null; try { session = sessionFactory.openSession(); session.delete(packageName+"deleteOrg", id); session.commit(); } finally { session.close(); } } @Override public void update(Org org) { SqlSession session = null; try { session = sessionFactory.openSession(); session.insert(packageName+"updateOrg", org); session.commit(); } finally { session.close(); } } }
TestOrgDao.java junit测试类
package com.ibatis3.test; import java.util.List; import org.junit.Test; public class TestOrgDao { @Test public void testSelectOne(){ OrgMapper dao = new OrgDao(); Org org = dao.selectById("root"); System.out.println(org.getOrgName()); } @Test public void TestselectAll(){ OrgMapper dao = new OrgDao(); List<Org> orgs = dao.selectAll(); for(Org org:orgs){ System.out.println(org.getOrgName()); } } @Test public void TestselectPersonsByName(){ OrgMapper dao = new OrgDao(); List<Org> orgs = dao.selectOrgsByName("产品"); for(Org org:orgs) System.out.println(org.getOrgName()); } @Test public void insert(){ OrgMapper dao = new OrgDao(); Org org = new Org(); org.setId("33"); org.setOrgName("testInsert"); org.setParentId("root"); dao.insert(org); } @Test public void delete(){ OrgMapper dao = new OrgDao(); dao.delete("33"); } @Test public void update(){ OrgMapper dao = new OrgDao(); Org org = dao.selectById("root"); org.setParentId("1"); dao.update(org); Org orgnew = dao.selectById("root"); System.out.println(orgnew.getParentId()); } }
jdbc.properties配置文件
driver=net.sourceforge.jtds.jdbc.Driver url=jdbc:jtds:sqlserver://localhost:1433/demo username=sa password=123
建表SQL:
create table org( id varchar(20) primary key, orgname varchar(50), parentid varchar(50) )
评论