订阅博主
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"><mapper namespace="org.mybatis.example.dao.DeptMapper"> <select id="selectOne" parameterType="int" resultType="org.mybatis.example.dao.Dept"> select * from dept where deptno=#{id} </select> <insert id="insertDept" parameterType="org.mybatis.example.dao.Dept" useGeneratedKeys="true" keyProperty="dept"> insert into dept(dname,loc) values( #{dname,jdbcType=VARCHAR}, #{loc,jdbcType=VARCHAR}) </insert> <update id="updateDept" parameterType="org.mybatis.example.dao.Dept"> update dept set dname=#{dname},loc=#{loc} where deptno=#{deptno} </update> <delete id="deleteDept" parameterType="java.lang.Integer"> delete from dept where deptno=#{id} </delete></mapper>
JAVA就业套餐课:https://edu.csdn.net/combo/detail/1230
package org.mybatis.example.dao;public interface DeptMapper { public Dept selectOne(int id); public int insertDept(Dept dept); public int updateDept(Dept dept); public int deleteDept(int id);}
import java.io.IOException;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;import org.mybatis.example.dao.Dept;import org.mybatis.example.dao.DeptMapper;public class Test2 { public static void main(String[] args) throws IOException { String resource="mybatisConfig.xml"; Reader reader=Resources.getResourceAsReader(resource); SqlSessionFactory sqlMapper=new SqlSessionFactoryBuilder().build(reader); SqlSession session=sqlMapper.openSession(); try { DeptMapper mapper=session.getMapper(DeptMapper.class); //1.查询// Dept dept=mapper.selectOne(2);// System.out.println(dept.getDname()); //2.插入操作 Dept dept=new Dept(); dept.setDname("张沅湲"); dept.setLoc("郑州"); mapper.insertDept(dept); session.commit();//实现增加 修改 删除 //3.更新操作;// Dept dept=new Dept();// dept.setDeptno(3);// dept.setDname("张沅湲");// dept.setLoc("湖南");// mapper.updateDept(dept);// session.commit(); //4.删除操作;// int i=mapper.deleteDept(3);// session.commit();// System.out.println(i); } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } }}
复杂的查询,在项目开发中,可以使用一个工具类来封装这些复杂的步骤:
package org.mybatis.example.dao;import java.io.Reader;import org.apache.ibatis.io.Resources;import org.apache.ibatis.session.SqlSession;import org.apache.ibatis.session.SqlSessionFactory;import org.apache.ibatis.session.SqlSessionFactoryBuilder;public class SqlSessionFactoryUtil { private static SqlSessionFactory factory; public SqlSessionFactoryUtil() {} static{ Reader reader=null; try { reader=Resources.getResourceAsReader("mybatisConfig.xml"); factory=new SqlSessionFactoryBuilder().build(reader); } catch (Exception e) { e.printStackTrace(); }finally{ try { if(reader!=null)reader.close(); } catch (Exception e2) { e2.printStackTrace(); } } } public static SqlSession getSqlSession(){ return factory.openSession(); }}
DeptMapper接口的完全代码:
package org.mybatis.example.dao;import java.util.List;public interface DeptMapper { public Dept selectOne(int id); public int insertDept(Dept dept); public int updateDept(Dept dept); public int deleteDept(int id); public List<Dept>selectAllDepts(); public List<Dept>selectByCriteria(String deptName);}
1.查询所有部门信息参考代码;
<select id="selectAllDepts" resultType="org.mybatis.example.dao.Dept" > select deptno,dname,loc from dept </select>
测试代码;
import java.util.List;import org.apache.ibatis.session.SqlSession;import org.mybatis.example.dao.Dept;import org.mybatis.example.dao.DeptMapper;import org.mybatis.example.dao.SqlSessionFactoryUtil;public class Test3 { public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); try { DeptMapper mapper=session.getMapper(DeptMapper.class); List<Dept>depts=mapper.selectAllDepts(); for(Dept dept:depts){ System.out.println("部门名称:"+dept.getDname()); } } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } }}
2.按条件查询,映射文件做以下修改,新增条件为模糊查询方法:
<!-- 按条件查询 --> <select id="selectByCriteria" parameterType="java.lang.String" resultType="org.mybatis.example.dao.Dept"> select deptno,dname,loc from dept where dname like '%${_parameter}%' </select>
测试代码
import java.util.List;import org.apache.ibatis.session.SqlSession;import org.mybatis.example.dao.Dept;import org.mybatis.example.dao.DeptMapper;import org.mybatis.example.dao.SqlSessionFactoryUtil;//按照条件进行查询;public class Test4 { public static void main(String[] args) { SqlSession session=SqlSessionFactoryUtil.getSqlSession(); try { DeptMapper mapper=session.getMapper(DeptMapper.class); List<Dept>depts=mapper.selectByCriteria("网络部"); for(Dept dept:depts){ System.out.println("部门名称:"+dept.getDname()); } } catch (Exception e) { e.printStackTrace(); }finally{ session.close(); } }}