一.mybatis使用的准备工作
1.找到mybatis所需要的jar文件:
mybatis-3.2.3.jar
mybatis-spring-1.2.1.jar
2.解压mybatis-3.2.3.jar文件找到如下两个dtd文件
mybatis-3-config.dtd
mybatis-3-mapper.dtd
3.记住mybatis配置的关键key
mybatis配置:
--映射文件
public id : -//mybatis.org//DTD Mapper 3.0//EN
system id : http://mybatis.org/dtd/mybatis-3-mapper.dtd
--主配置文件
public id : -//mybatis.org//DTD Config 3.0//EN
system id :http://mybatis.org/dtd/mybatis-3-config.dtd
4.在myeclipse中配置mybatis的环境,即配置dtd文件,以便写代码时有提示
5.将mybatis的dtd文件配置到myeclipse中步骤如下:
二、mybatis无代理全套增删改查
2.1 创建一个javaweb项目MyBatis_Part1
2.2 在项目的src下创建Configuration.xml主配置文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd" > 3 <configuration> 4 <!-- 路径别名配置 --> 5 <typeAliases> 6 <typeAlias type="com.entity.Dept" alias="Dept"/> 7 </typeAliases> 8 9 <!-- 链接数据库的环境配置,development开发模式 --> 10 <environments default="development"> 11 <environment id="development"> 12 <!-- jdbc管理事务 --> 13 <transactionManager type="jdbc"/> 14 <!-- 数据源配置 --> 15 <dataSource type="POOLED"> 16 <property name="driver" value="oracle.jdbc.driver.OracleDriver"/> 17 <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl"/> 18 <property name="username" value="scott"/> 19 <property name="password" value="tiger"/> 20 </dataSource> 21 22 </environment> 23 </environments> 24 <!-- 注册映射文件 --> 25 <mappers> 26 <mapper resource="com/mapper/DeptMapper.xml"/> 27 </mappers> 28 29 30 31 </configuration>
2.3 在src下的com.entity包下创建Dept.java文件
1 package com.entity; 2 3 public class Dept { 4 private int deptno; 5 private String dname; 6 private String loc; 7 8 public Dept() { 9 } 10 11 public Dept(int deptno, String dname, String loc) { 12 this.deptno = deptno; 13 this.dname = dname; 14 this.loc = loc; 15 } 16 17 public int getDeptno() { 18 return deptno; 19 } 20 21 public void setDeptno(int deptno) { 22 this.deptno = deptno; 23 } 24 25 public String getDname() { 26 return dname; 27 } 28 29 public void setDname(String dname) { 30 this.dname = dname; 31 } 32 33 public String getLoc() { 34 return loc; 35 } 36 37 public void setLoc(String loc) { 38 this.loc = loc; 39 } 40 41 @Override 42 public String toString() { 43 return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc 44 + "]"; 45 } 46 47 48 49 }
2.4 在src下的com.mapper包下创建DeptMapper.java文件
1 package com.mapper; 2 3 import java.util.List; 4 5 import com.entity.Dept; 6 7 /** 8 * 映射接口 9 * @author pc 10 * 11 */ 12 public interface DeptMapper { 13 /** 14 * 查询所有 15 * @return 16 */ 17 public List<Dept> getAll(); 18 /** 19 * 根据id查询 20 * @param id 21 * @return 22 */ 23 Dept findId(int deptno); 24 /** 25 * 修改对象 26 * @param dept 27 * @return 28 */ 29 int updateDept(Dept dept); 30 /** 31 * 保存 32 * @param dept 33 * @return 34 */ 35 int saveDept(Dept dept); 36 /** 37 * 删除 38 * @param id 39 * @return 40 */ 41 int deleteById(int id); 42 43 44 45 }
2.5 在src下的com.mapper包下创建DeptMapper.xml映射文件
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > 3 <!-- namespace的值, 4 如果用有代理的模式实现增删改,那么值必须是映射接口的全路径, 5 如果用无代理模式实现增删改,那么值可以是任意的 ,但是一般情况是映射接口的全路径--> 6 <mapper namespace="test"> 7 <!-- id的值必须要和映射接口的方法名保持一致, --> 8 <!-- 查询所有 --> 9 <select id="getAll" resultType="Dept"> 10 select * from dept 11 </select> 12 13 <!--2. 根据id查询 --> 14 <!-- parameterType参数类型 --> 15 <select id="findId" resultType="Dept" parameterType="int"> 16 select * from dept where deptno=#{deptno} 17 </select> 18 19 <!-- 3.修改 --> 20 <update id="updateDept" parameterType="Dept"> 21 update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno} 22 </update> 23 24 <!-- 4.保存 --> 25 <insert id="saveDept" parameterType="Dept"> 26 insert into dept values(#{deptno},#{dname},#{loc}) 27 </insert> 28 29 <!-- 5.删除 --> 30 <delete id="deleteById" parameterType="int"> 31 delete from dept where deptno=#{deptno} 32 </delete> 33 </mapper>
2.6 在src下的com.util包下创建MyBatisUtil.java
1 package com.util; 2 3 import java.io.InputStream; 4 import java.io.Reader; 5 6 import org.apache.ibatis.io.Resources; 7 import org.apache.ibatis.session.SqlSession; 8 import org.apache.ibatis.session.SqlSessionFactory; 9 import org.apache.ibatis.session.SqlSessionFactoryBuilder; 10 11 /** 12 * 获取sqlsession工具类 13 * @author pc 14 * 15 */ 16 public class MyBatisUtil { 17 //定义sqlsessionFactory对象 18 private static SqlSessionFactory sqlSessionFactory=null; 19 20 static{ 21 //读取xml文件 22 InputStream is=MyBatisUtil.class 23 .getClassLoader() 24 .getResourceAsStream("Configuration.xml"); 25 //Reader rader=Resources.getResourceAsReader("Configuration.xml"); 26 27 //创建sqlsessionFactory对象 28 sqlSessionFactory=new SqlSessionFactoryBuilder().build(is); 29 30 } 31 /** 32 * 获取SqlSession对象的方法 33 * @param commit 如果为true表示执行完操作自动提交事务,false反之 34 * @return 35 */ 36 public static SqlSession getSqlSession(boolean iscommit){ 37 return sqlSessionFactory.openSession(iscommit); 38 } 39 40 }
2.7 在src下的com.mapper.impl包下创建DeptMapperImpl.java
1 package com.mapper.impl; 2 3 import java.util.List; 4 5 import org.apache.ibatis.session.SqlSession; 6 7 import com.entity.Dept; 8 import com.mapper.DeptMapper; 9 import com.util.MyBatisUtil; 10 11 public class DeptMapperImpl implements DeptMapper { 12 /** 13 * 1.查询所有 14 */ 15 public List<Dept> getAll() { 16 //1.获取sqlsession,执行完操作不自动提交事务 17 SqlSession sqlSession=MyBatisUtil.getSqlSession(false); 18 19 //2.调用方法执行操作, 20 //Stirng类型的参数=映射文件的namespace的值.映射文件的id 21 List<Dept> list=sqlSession.selectList("test.getAll"); 22 //3.关闭sqlsession 23 sqlSession.close(); 24 return list; 25 } 26 /** 27 * 2.查询单条 28 */ 29 public Dept findId(int deptno) { 30 //1.获取sqlsession,执行完操作不自动提交事务 31 SqlSession sqlSession=MyBatisUtil.getSqlSession(false); 32 33 Dept dept=sqlSession.selectOne("test.findId", deptno); 34 35 sqlSession.close(); 36 return dept; 37 } 38 39 /** 40 * 3.添加 41 */ 42 public int saveDept(Dept dept) { 43 //1.获取sqlsession,执行完操作不自动提交事务 44 SqlSession sqlSession=MyBatisUtil.getSqlSession(true); 45 46 int num=sqlSession.insert("test.saveDept", dept); 47 48 sqlSession.close(); 49 return num; 50 } 51 /** 52 * 4.删除 53 */ 54 public int deleteById(int id) { 55 //1.获取sqlsession,执行完操作不自动提交事务 56 SqlSession sqlSession=MyBatisUtil.getSqlSession(true); 57 58 int num=sqlSession.delete("test.deleteById", id); 59 sqlSession.close(); 60 return num; 61 } 62 63 64 65 /** 66 * 5.修改 67 */ 68 public int updateDept(Dept dept) { 69 //1.获取sqlsession,执行完操作不自动提交事务 70 SqlSession sqlSession=MyBatisUtil.getSqlSession(true); 71 72 int num=sqlSession.update("test.updateDept", dept); 73 sqlSession.close(); 74 return num; 75 } 76 77 }
2.8 在src下的com.test包下创建Test.java
1 package com.test; 2 3 import java.util.List; 4 5 import com.entity.Dept; 6 import com.mapper.DeptMapper; 7 import com.mapper.impl.DeptMapperImpl; 8 9 public class Test { 10 11 /** 12 * @param args 13 */ 14 public static void main(String[] args) { 15 DeptMapper mapper=new DeptMapperImpl(); 16 17 System.out.println("-------------3.添加--------------"); 18 /* Dept savedept=new Dept(60, "学术部", "8号楼"); 19 int num= mapper.saveDept(savedept); 20 if(num>0){ 21 System.out.println("insert success"); 22 }else{ 23 24 System.out.println("insert fail"); 25 } 26 */ 27 System.out.println("----------1.查询所有--------------"); 28 29 List<Dept> list=mapper.getAll(); 30 for (Dept dept : list) { 31 System.out.println(dept); 32 } 33 System.out.println("----------2.查询单条--------------"); 34 Dept dept= mapper.findId(10); 35 System.out.println(dept); 36 37 System.out.println("-----------4.修改--------"); 38 /* Dept updatedept=mapper.findId(60); 39 updatedept.setDname("huolly11"); 40 updatedept.setLoc("222"); 41 42 int upnum= mapper.updateDept(updatedept); 43 if(upnum>0){ 44 System.out.println("update success"); 45 }else{ 46 System.out.println("update fail"); 47 48 }*/ 49 System.out.println("------------6.删除---------------"); 50 /* Dept deldept=mapper.findId(50); 51 int delnum=mapper.deleteById(deldept.getDeptno()); 52 if(delnum>0){ 53 System.out.println("delete success"); 54 }else{ 55 System.out.println("delete fail"); 56 57 }*/ 58 } 59 60 }