jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ssm
jdbc.username=root
jdbc.password=root
DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"><package name="com.atguigu.bean"/> default="mysql"> <package name="com.atguigu.mapper"/>
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">insert into emp values(null, '${ename}', '${age}', '${sex}')
package com.atguigu.mapper;
import java.util.Map;
import org.apache.ibatis.annotations.Param;
import com.atguigu.bean.Emp;
public interface ParamMapper {
//添加员工信息
void insertEmp(Emp emp);
//根据eid获取员工信息
Emp getEmpByEid(String eid);
//根据eid和ename查询员工信息
Emp getEmpByEidAndEname(String eid, String ename);
//根据map查询员工信息
Emp getEmpByMap(Map map);
//根据eid和ename查询员工信息
Emp getEmpByEidAndEnameByParam(@Param("eid")String eid, @Param("ename")String ename);
}
package com.atguigu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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.atguigu.bean.Emp;
import com.atguigu.mapper.EmpMapper;
import com.atguigu.mapper.ParamMapper;
public class TestParam {
@Test
public void testCRUD() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//SqlSession sqlSession = sqlSessionFactory.openSession();//需要手动处理事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动处理事务
ParamMapper mapper = sqlSession.getMapper(ParamMapper.class);
/*Emp emp = new Emp(null, "admin", 23, "男");*/
/*mapper.insertEmp(emp);
System.out.println(emp.getEid());*/
/*Emp emp = mapper.getEmpByEid("1");
System.out.println(emp);*/
/*Emp emp = mapper.getEmpByEidAndEname("1", "张三");
System.out.println(emp);*/
Map map = new HashMap<>();
map.put("eid", "1");
map.put("ename", "张三");
Emp emp = mapper.getEmpByMap(map);
/*Emp emp = mapper.getEmpByEidAndEnameByParam("1", "张三");*/
System.out.println(emp);
}
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
package com.atguigu.mapper;
import java.util.List;
import java.util.Map;
import org.apache.ibatis.annotations.MapKey;
import com.atguigu.bean.Emp;
public interface EmpSelectMapper {
//根据eid查询一个员工信息
Emp getEmpByEid(String eid);
//获取所有的员工的数量
Integer getCount();
//以map集合获取一个员工信息
Map getEmpMapByEid(String eid);
//以map集合获取所有员工信息
@MapKey("eid")//设置map的键,因为在查询时传出所有的员工信息,可以把员工信息作为值,但是必须设置键
Map getAllEmpMap();
}
package com.atguigu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
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.atguigu.bean.Emp;
import com.atguigu.mapper.EmpMapper;
import com.atguigu.mapper.EmpSelectMapper;
public class TestSelect {
@Test
public void testSelect() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//SqlSession sqlSession = sqlSessionFactory.openSession();//需要手动处理事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动处理事务
EmpSelectMapper mapper = sqlSession.getMapper(EmpSelectMapper.class);
//若查询出的数据有多条,则绝对不能将接口中该方法的返回值设置为Javabean
/*Emp emp = mapper.getEmpByEid("3");
System.out.println(emp);*/
//获取员工总记录数
/*Integer i = mapper.getCount();
System.out.println(i);*/
//以map获取单个emp
/*Map map = mapper.getEmpMapByEid("6");
System.out.println(map); */
//以map获取所有emp
Map map = mapper.getAllEmpMap();
System.out.println(map);
}
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">insert into emp values(null,#{ename},#{age},#{sex}) update emp set ename = #{ename}, age = #{age}, sex = #{sex} where eid = #{eid} delete from emp where eid = #{eid}
package com.atguigu.mapper;
import java.util.List;
import com.atguigu.bean.Emp;
public interface EmpMapper {
//根据eid查询一个员工信息
Emp getEmpByEid(String eid);
//获取所有的员工信息
List getAllEmp();
//添加员工信息
void addEmp(Emp emp);
//修改员工信息
void updateEmp(Emp emp);
//删除员工信息
Boolean deleteEmp(String eid);
}
package com.atguigu.test;
import java.io.IOException;
import java.io.InputStream;
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.atguigu.bean.Emp;
import com.atguigu.mapper.EmpMapper;
public class TestCRUD {
@Test
public void testCRUD() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//SqlSession sqlSession = sqlSessionFactory.openSession();//需要手动处理事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动处理事务
EmpMapper empMapper = sqlSession.getMapper(EmpMapper.class);
//测试:根据eid获取员工信息
/*Emp emp = empMapper.getEmpByEid("3");
System.out.println(emp);*/
//测试:获取所有的员工信息
/*List list = empMapper.getAllEmp();
System.out.println(list); */
//测试:添加员工信息
/*empMapper.addEmp(new Emp(null, "admin", 23, "女"));
sqlSession.commit();//提交事务*/
//测试:修改员工信息
empMapper.updateEmp(new Emp(6, "张二", 33, "女"));
//测试:删除员工信息
/*Boolean i = empMapper.deleteEmp("2");
System.out.println("result:"+i);*/
//select 字段名 from 表名 where 条件 group by 字段名 having 条件 order by 字段名 desc/asc limit index,pageSize
}
}
DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
package com.atguigu.mapper;
import java.util.List;
import com.atguigu.bean.Dept;
import com.atguigu.bean.Emp;
public interface EmpDeptMapper {
List getAllEmp();
Emp getEmpStep(String eid);
Dept getDeptEmpsByDid(String did);
Dept getOnlyDeptByDid(String did);
List getEmpListByDid(String did);
}
package com.atguigu.test;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
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.atguigu.bean.Dept;
import com.atguigu.bean.Emp;
import com.atguigu.mapper.EmpDeptMapper;
import com.atguigu.mapper.EmpMapper;
import com.atguigu.mapper.EmpSelectMapper;
public class TestEmpDept {
@Test
public void testSelect() throws IOException {
InputStream is = Resources.getResourceAsStream("mybatis-config.xml");
SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
//SqlSession sqlSession = sqlSessionFactory.openSession();//需要手动处理事务
SqlSession sqlSession = sqlSessionFactory.openSession(true);//自动处理事务
EmpDeptMapper mapper = sqlSession.getMapper(EmpDeptMapper.class);
/*List emp = mapper.getAllEmp();
System.out.println(emp); */
/*Emp emp = mapper.getEmpStep("3");
System.out.println(emp.getEname());
System.out.println("=======================");
System.out.println(emp.getDept());*/
/*Dept dept = mapper.getDeptEmpsByDid("2");
System.out.println(dept);*/
Dept dept = mapper.getOnlyDeptByDid("1");
System.out.println(dept.getDname());
System.out.println("=======================");
System.out.println(dept.getEmps());
}
}