1、导入jar包,导入相关配置文件,均在自己博客园的文件中
编写mybatis.xml文件
编写单例模式的mybatis测试类
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();
}
}
}
2、编写Dept实体类
package com.wh.pojo;
public class Dept {
private Integer dpt_id;
private String dpt_name;
private String dpt_ioc;
public Dept() {
// TODO Auto-generated constructor stub
}
public Dept(Integer dpt_id, String dpt_name, String dpt_ioc) {
super();
this.dpt_id = dpt_id;
this.dpt_name = dpt_name;
this.dpt_ioc = dpt_ioc;
}
public Integer getDpt_id() {
return dpt_id;
}
public void setDpt_id(Integer dpt_id) {
this.dpt_id = dpt_id;
}
public String getDpt_name() {
return dpt_name;
}
public void setDpt_name(String dpt_name) {
this.dpt_name = dpt_name;
}
public String getDpt_ioc() {
return dpt_ioc;
}
public void setDpt_ioc(String dpt_ioc) {
this.dpt_ioc = dpt_ioc;
}
@Override
public String toString() {
return "Dept [dpt_id=" + dpt_id + ", dpt_name=" + dpt_name + ", dpt_ioc=" + dpt_ioc + "]";
}
}
3、编写DeptMapper接口
package com.wh.mapper;
import java.util.List;
import org.apache.ibatis.annotations.Param;
import com.wh.pojo.Dept;
public interface DeptMapper {
public void insertDept(Dept dept);
public List selectAll();
public Dept selectById(Integer id);
public void updateDept(Dept dept);
public void deleteDept(Integer id);
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);
}
4、导入mybatis-3-mapper.dtd文件
5、编写DeptMapper.xml文件
insert into dept values (#{dpt_id},#{dpt_name},#{dpt_ioc});
update dept set dpt_name=#{dpt_name},dpt_ioc=#{dpt_ioc} where dpt_id = #{dpt_id}
delete from dept where dpt_id=#{dpt_id}
6、编写DeptMapperImpl实现类
package com.wh.mapperImpl;
import java.util.List;
import com.wh.mapper.DeptMapper;
import com.wh.pojo.Dept;
public class DeptDaoImpl extends BaseDao implements DeptMapper {
@Override
public void insertDept(Dept dept) {
//获得连接
this.openSession();
//找到接口 获得bean 映射关系 绑定实体类与表列名
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
mapper.insertDept(dept);
}
@Override
public List selectAll() {
//获得连接
this.openSession();
//找到接口
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
return mapper.selectAll();
}
@Override
public Dept selectById(Integer id) {
this.openSession();
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
return mapper.selectById(id);
}
@Override
public void updateDept(Dept dept) {
this.openSession();
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
mapper.updateDept(dept);
}
@Override
public void deleteDept(Integer id) {
this.openSession();
DeptMapper mapper=(DeptMapper) ss.getMapper(DeptMapper.class);
mapper.deleteDept(id);
}
@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);
}
}
7、编写测试类
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.mapperImpl.DeptDaoImpl;
import com.wh.pojo.Dept;
public class TestMyBatis {
//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);
}
//插入
@Test
public void testInsertDept() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
dao.insertDept(new Dept(4,"技术","4楼"));
}
//查询所有
@Test
public void testSelectAll() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
List list = dao.selectAll();
System.out.println(list.size());
System.out.println(list);
}
//查询单个
@Test
public void testSelectById() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
Dept d = dao.selectById(3);
System.out.println(d);
}
//修改
@Test
public void testUpdateDept() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
dao.updateDept(new Dept(3,"情报部","xxx"));
}
//删除
@Test
public void testdeleteDept() throws IOException{
DeptDaoImpl dao=new DeptDaoImpl();
dao.deleteDept(3);
}
//模糊查询
@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);
}
}