1、mybatis.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "./mybatis-3-config.dtd"> <configuration> <!-- properties配置文件中属性值,在整个配置文件中通过${}进行引用 --> <properties> <property name="driver" value="com.mysql.jdbc.Driver" /> </properties> <typeAliases> <typeAlias type="com.wh.pojo.Emp" alias="Emp"/> </typeAliases> <!-- 数据源环境信息配置 --> <environments default="development2"> <environment id="development"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!-- mysql数据源配置 --> <property name="driver" value="${driver}" /> <property name="url" value="jdbc:mysql://localhost/ys" /> <property name="username" value="root" /> <property name="password" value="admin" /> </dataSource> </environment> <environment id="development2"> <transactionManager type="JDBC" /> <dataSource type="POOLED"> <!-- oracle数据源配置 --> <property name="driver" value="oracle.jdbc.driver.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@127.0.0.1:1521:orcl" /> <property name="username" value="scott" /> <property name="password" value="tiger" /> </dataSource> </environment> </environments> <mappers> <mapper resource="com/wh/mapper/DeptMapper.xml" /> <mapper resource="com/wh/mapper/EmpMapper.xml" /> </mappers> </configuration>
2、DeptMapper.java
package com.wh.mapper; import java.util.List; import org.apache.ibatis.annotations.Param; import com.wh.pojo.Dept; import com.wh.vo.VO; public interface DeptMapper { public List<Dept> selectByName(String name); public List<Dept> selectByMore(@Param("dpt_name")String dpt_name,@Param("dpt_ioc")String dpt_ioc); public List<Dept> selectByList(@Param("ids") List<Integer> ids); public List<Dept> selectPage(VO vo); }
3、DeptMapper.xml
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "./mybatis-3-mapper.dtd"> <!--namespace 绑定接口 --> <mapper namespace="com.wh.mapper.DeptMapper"> <!-- DeptResultMap --> <resultMap id="DeptResultMap" type="com.wh.pojo.Dept2"> <id property="did" column="dpt_id" /> <result property="dname" column="dpt_name" /> <result property="dioc" column="dpt_ioc" /> </resultMap> <!-- 模糊查询 --> <select id="selectByName" parameterType="java.lang.String" resultType="com.wh.pojo.Dept"> <!-- select * from dept where dpt_name like concat('%',#{dpt_name},'%') --> select * from dept where dpt_name like '%${value}%' </select> <!-- 多重条件查询 接口要对形参注解 concat('%','销售','%') --> <select id="selectByMore" resultType="com.wh.pojo.Dept"> select * from dept where 1=1 <if test="dpt_name!=null and dpt_name!='' "> and dpt_name like concat('%',#{dpt_name,jdbcType=VARCHAR},'%') </if> <if test="dpt_ioc!=null and dpt_ioc!='' "> and dpt_ioc like concat(concat('%',#{dpt_ioc,jdbcType=VARCHAR}),'%') </if> </select> <!-- 集合查询 in --> <select id="selectByList" resultType="com.wh.pojo.Dept"> select * from dept where dpt_id in <foreach collection="ids" index="index" open="(" separator="," close=")" item="id"> #{id} </foreach> </select> <!-- 分页查询 --> <select id="selectPage" resultType="com.wh.pojo.Dept" parameterType="com.wh.vo.VO"> select * from dept limit #{pager.start},#{pager.pageSize} </select> </mapper>
4、DeptDaoImpl.java
package com.wh.mapperImpl; import java.util.List; import com.wh.mapper.DeptMapper; import com.wh.pojo.Dept; import com.wh.vo.VO; public class DeptDaoImpl extends BaseDao implements DeptMapper { @Override public List<Dept> selectByName(String name) { this.openSession(); DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class); return mapper.selectByName(name); } @Override public List<Dept> selectByMore(String dpt_name, String dpt_ioc) { this.openSession(); DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class); return mapper.selectByMore(dpt_name,dpt_ioc); } @Override public List<Dept> selectByList(List<Integer> ids) { this.openSession(); DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class); return mapper.selectByList(ids); } @Override public List<Dept> selectPage(VO vo) { this.openSession(); DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class); return mapper.selectPage(vo); } }
5、Pager.java
package com.wh.common; public class Pager { private int recordCount ;//总记录数 private int pageSize = 3;//每页个数 自定义 private int pageCount ; //总页数 private int pageNum;//当前页数 private int start;//起始记录数 public Pager(int recordCount, int pageNum) { super(); this.recordCount = recordCount; this.pageNum = pageNum; //总页数 if(recordCount % pageSize == 0){ pageCount = recordCount / pageSize; }else{ pageCount = recordCount / pageSize + 1; } //当前页码处理 if(pageNum < 1){ pageNum = 1; } if(pageNum > pageCount){ pageNum = pageCount; } //起始位置 mysql起始位置要减去1 start = (pageNum - 1) * pageSize; } public int getRecordCount() { return recordCount; } public void setRecordCount(int recordCount) { this.recordCount = recordCount; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getPageNum() { return pageNum; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public int getStart() { return start; } public void setStart(int start) { this.start = start; } }
6、Vo.java
package com.wh.vo; import com.wh.common.Pager; public class VO { private Pager pager; public VO(Pager pager) { super(); this.pager = pager; } public Pager getPager() { return pager; } public void setPager(Pager pager) { this.pager = pager; } }
TestDept.java
package com.wh.junit; /** * mybatis编写顺序 * DeptMapper.java、DeptMapper.xml、DeptDaoImpl.java、TestMyBatis.java */ import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.List; 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.junit.Test; import com.wh.common.Pager; import com.wh.mapperImpl.DeptDaoImpl; import com.wh.pojo.Dept; import com.wh.vo.VO; public class TestDept { //mybatis快速入门 @Test public void test00() throws IOException{ InputStream in = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(in); SqlSession ss=ssf.openSession(); String string = ss.toString(); System.out.println(string); } //mybatis快速入门 二 @Test public void test01() throws IOException{ InputStream in = Resources.getResourceAsStream("mybatis.xml"); SqlSessionFactory ssf=new SqlSessionFactoryBuilder().build(in); SqlSession ss=ssf.openSession(); // String string = "com.wh.mapper.DeptMapper.selectById"; // Object selectOne = ss.selectOne(string,5); // System.out.println(selectOne); String string = "com.wh.mapper.DeptMapper.sAll"; Object select = ss.selectList(string); System.out.println(select); } //模糊查询 @Test public void testselectByName() throws IOException{ DeptDaoImpl dao=new DeptDaoImpl(); List<Dept> list = dao.selectByName("销"); System.out.println(list); } //多重条件查询 @Test public void testSelectByMore() throws IOException{ DeptDaoImpl dao=new DeptDaoImpl(); List<Dept> list = dao.selectByMore("销","2"); System.out.println(list); } //集合查询 in @Test public void testSelectByList() throws IOException{ DeptDaoImpl dao=new DeptDaoImpl(); List<Integer> ids=new ArrayList<Integer>(); ids.add(1); ids.add(3); List<Dept> list = dao.selectByList(ids); System.out.println(list); } //分页查询 @Test public void testSelectPage() throws IOException{ DeptDaoImpl dao=new DeptDaoImpl(); List<Dept> list = dao.selectPage(new VO(new Pager(5,1))); System.out.println(list); } }
BaseDao
package com.wh.mapperImpl; /** * 将mybatis中事务管理这一块,用单例模式实现 */ import java.io.IOException; import java.io.InputStream; 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 BaseDao { private static SqlSessionFactory ssf; public SqlSession ss; static { String resource = "mybatis.xml"; try { // 读取配置文件 InputStream in = Resources.getResourceAsStream(resource); // 创建连接工厂 ssf = new SqlSessionFactoryBuilder().build(in); } catch (IOException e) { e.printStackTrace(); } } // 获得连接 public SqlSession openSession() { if (ss == null) { // 事务自动提交,默认是false不自动提交 true自动提交 ss = ssf.openSession(true); } return ss; } // 提交 public void commit() { if (ss != null) { ss.commit(); } } // 回滚 public void rollback() { if (ss != null) { ss.rollback(); } } // 关闭连接 public void close() { if (ss != null) { ss.close(); } } }