1、mybatis.xml
/p>
"./mybatis-3-config.dtd">
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 selectByName(String name);
public List selectByMore(@Param("dpt_name")String dpt_name,@Param("dpt_ioc")String dpt_ioc);
public List selectByList(@Param("ids") List ids);
public List selectPage(VO vo);
}
3、DeptMapper.xml
/p>
"./mybatis-3-mapper.dtd">
select * from dept where dpt_name like '%${value}%'
select * from dept where 1=1
and dpt_name like concat('%',#{dpt_name,jdbcType=VARCHAR},'%')
and dpt_ioc like concat(concat('%',#{dpt_ioc,jdbcType=VARCHAR}),'%')
select * from dept where dpt_id in
#{id}
select * from dept limit #{pager.start},#{pager.pageSize}
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 selectByName(String name) {
this.openSession();
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
return mapper.selectByName(name);
}
@Override
public List 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 selectByList(List ids) {
this.openSession();
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
return mapper.selectByList(ids);
}
@Override
public List 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 list = dao.selectByName("销");
System.out.println(list);
}
//多重条件查询
@Test
public void testSelectByMore() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
List list = dao.selectByMore("销","2");
System.out.println(list);
}
//集合查询 in
@Test
public void testSelectByList() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
List ids=new ArrayList();
ids.add(1);
ids.add(3);
List list = dao.selectByList(ids);
System.out.println(list);
}
//分页查询
@Test
public void testSelectPage() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
List 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();
}
}
}