一.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//ENsystem id : http://mybatis.org/dtd/mybatis-3-mapper.dtd--主配置文件
public id : -//mybatis.org//DTD Config 3.0//ENsystem id :http://mybatis.org/dtd/mybatis-3-config.dtd4.在myeclipse中配置mybatis的环境,即配置dtd文件,以便写代码时有提示
5.将mybatis的dtd文件配置到myeclipse中步骤如下:
二、mybatis无代理全套增删改查
2.1 创建一个javaweb项目MyBatis_Part1
2.2 在项目的src下创建Configuration.xml主配置文件
1 2 34 5 6 8 9 107 11 24 2512 13 2314 15 16 21 2217 18 19 20 26 28 29 30 3127
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 @Override42 public String toString() {43 return "Dept [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc44 + "]";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 pc10 *11 */12 public interface DeptMapper {13 /**14 * 查询所有15 * @return16 */17 public ListgetAll();18 /**19 * 根据id查询20 * @param id21 * @return22 */23 Dept findId(int deptno);24 /**25 * 修改对象26 * @param dept27 * @return28 */29 int updateDept(Dept dept);30 /**31 * 保存32 * @param dept33 * @return34 */35 int saveDept(Dept dept);36 /**37 * 删除38 * @param id39 * @return40 */41 int deleteById(int id);42 43 44 45 }
2.5 在src下的com.mapper包下创建DeptMapper.xml映射文件
1 2 3 67 8 9 12 13 14 15 18 19 20 21 update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno}22 23 24 2526 insert into dept values(#{deptno},#{dname},#{loc})27 28 29 3031 delete from dept where deptno=#{deptno}32 33
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 pc14 *15 */16 public class MyBatisUtil {17 //定义sqlsessionFactory对象18 private static SqlSessionFactory sqlSessionFactory=null;19 20 static{21 //读取xml文件22 InputStream is=MyBatisUtil.class23 .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 * @return35 */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 ListgetAll() {16 //1.获取sqlsession,执行完操作不自动提交事务17 SqlSession sqlSession=MyBatisUtil.getSqlSession(false);18 19 //2.调用方法执行操作,20 //Stirng类型的参数=映射文件的namespace的值.映射文件的id21 List list=sqlSession.selectList("test.getAll");22 //3.关闭sqlsession23 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 args13 */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 Listlist=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 }