Project_location:C:\Users\Administrator\IdeaProjects
.idea--包自动
config--资源包
lib --jar包
out --输出
src --源代码
db.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///mybatis
jdbc.username=root
jdbc.password=root
log4j.properties
# Global logging configuration
#在开发环境日志级别要设置为DEBUG、生产环境要设置为INFO或者ERROR
log4j.rootLogger=DEBUG, stdout
# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n
xml文件--SqlMapConfig.xml
src下建立--com.kjh包下
domain包写入成员类
eg. --Dept.class 自动生成成员get,set方法
快捷键:Alt+Insert或直接Ctrl+o
package com.kjdh.domain;
public class Dept {
private int deptno;
private String dname;
private String loc;
@Override
public String toString() {
return "Dept{" +
"deptno=" + deptno +
", dname='" + dname + '\'' +
", loc='" + loc + '\'' +
'}';
}
public int getDeptno() {
return deptno;
}
public void setDeptno(int deptno) {
this.deptno = deptno;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
Emp下成员
import java.math.BigDecimal;
import java.util.Date;
public class Emp {
private Integer empno;
private String ename;
private String job;
private Integer mgr;
private Date hiredate;
private BigDecimal sal;
private BigDecimal comm;
private Integer deptno;
mapper包是一个执行包,属于底层劳动力,用来写接口interface和实现.xml接口与实现名字要一致
分别写DeptMapper与DeptMapper.xml ;
EmpMapper与EmpMapper.xml
DeptMapper接口
package com.kjdh.mapper;
import com.kjdh.domain.Dept;
import java.util.List;
import java.util.Map;
public interface DeptMapper {
public Dept getDeptByID(int id) ;
public List getAllDepts();
public int deleteDeptByID(int id);
public int addDept(Dept dept);
public int updateDept(Dept dept);
public List getDeptsList(Dept dept);
public int getDeptRecorCount();
public ListgetDeptsListMap(Map params);
public List
DeptMapper.xml
dname = #{dname},
loc=#{loc},
delete from dept where deptno = #{value }
select Last_insert_id()
insert into dept(dname,loc) values(#{dname},#{loc})
update dept
where deptno=#{deptno}
EmpMapper
package com.kjdh.mapper;
import com.kjdh.domain.Emp;
public interface EmpMapper {
int deleteByPrimaryKey(Integer empno);
int insert(Emp record);
int insertSelective(Emp record);
Emp selectByPrimaryKey(Integer empno);
int updateByPrimaryKeySelective(Emp record);
int updateByPrimaryKey(Emp record);
}
EmpMapper.xml
empno, ename, job, mgr, hiredate, sal, comm, deptno
delete from emp
where empno = #{empno,jdbcType=INTEGER}
insert into emp (empno, ename, job,
mgr, hiredate, sal, comm,
deptno)
values (#{empno,jdbcType=INTEGER}, #{ename,jdbcType=VARCHAR}, #{job,jdbcType=VARCHAR},
#{mgr,jdbcType=INTEGER}, #{hiredate,jdbcType=DATE}, #{sal,jdbcType=DECIMAL}, #{comm,jdbcType=DECIMAL},
#{deptno,jdbcType=INTEGER})
insert into emp
empno,
ename,
job,
mgr,
hiredate,
sal,
comm,
deptno,
#{empno,jdbcType=INTEGER},
#{ename,jdbcType=VARCHAR},
#{job,jdbcType=VARCHAR},
#{mgr,jdbcType=INTEGER},
#{hiredate,jdbcType=DATE},
#{sal,jdbcType=DECIMAL},
#{comm,jdbcType=DECIMAL},
#{deptno,jdbcType=INTEGER},
update emp
ename = #{ename,jdbcType=VARCHAR},
job = #{job,jdbcType=VARCHAR},
mgr = #{mgr,jdbcType=INTEGER},
hiredate = #{hiredate,jdbcType=DATE},
sal = #{sal,jdbcType=DECIMAL},
comm = #{comm,jdbcType=DECIMAL},
deptno = #{deptno,jdbcType=INTEGER},
where empno = #{empno,jdbcType=INTEGER}
update emp
set ename = #{ename,jdbcType=VARCHAR},
job = #{job,jdbcType=VARCHAR},
mgr = #{mgr,jdbcType=INTEGER},
hiredate = #{hiredate,jdbcType=DATE},
sal = #{sal,jdbcType=DECIMAL},
comm = #{comm,jdbcType=DECIMAL},
deptno = #{deptno,jdbcType=INTEGER}
where empno = #{empno,jdbcType=INTEGER}
用测试类跑一跑
package com.kjdh.mapper;
import com.kjdh.domain.Dept;
import com.kjdh.domain.Emp;
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.Before;
import org.junit.Test;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class TestMybatis {
DeptMapper deptMapper =null;
SqlSession sqlSession =null;
EmpMapper empMapper=null;
@Before
public void CreateSission() throws IOException {
// write your code here
String resource = "SqlMapConfig.xml";
InputStream inputStream = Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory = sqlSessionFactoryBuilder.build(inputStream);
sqlSession = sqlSessionFactory.openSession();
//获得mapper接口对象
deptMapper = sqlSession.getMapper(DeptMapper.class);
empMapper=sqlSession.getMapper(EmpMapper.class);
}
@Test
public void testgetDeptByID() throws IOException {
Dept dept = deptMapper.getDeptByID(10);
System.out.println(dept);
}
@Test
public void testDleteDeptByID(){
int result = deptMapper.deleteDeptByID(40);
sqlSession.commit();
System.out.println(result);
}
@Test
public void testAddDeptByID(){
Dept dept =new Dept();
dept.setDname("xiaoshou");
dept.setLoc("shenyang");
int result = deptMapper.addDept(dept);
sqlSession.commit();
System.out.println(result);
System.out.println(dept.getDeptno());
}
@Test
public void testUpdateDeptByID(){
Dept dept =new Dept();
dept.setDname("6667");
dept.setLoc("sssdd");
dept.setDeptno(44);
int result = deptMapper.updateDept(dept);
sqlSession.commit();
System.out.println(result);
}
@Test
public void testGetDeptList() {
Dept dept= new Dept();
dept.setLoc("shenyang");
dept.setDname("销售");
List deptList = deptMapper.getDeptsList(dept);
System.out.println(deptList);
}
@Test
public void testGetDeptCount() {
int count = deptMapper.getDeptRecorCount();
System.out.println(count);
}
@Test
public void testGetDeptListMap(){
Map params=new HashMap<>();
params.put("loc","shenyang");
params.put("deptname","销售部");
List deptList=deptMapper.getDeptsListMap(params);
System.out.println(params);
}
@Test
public void getEmpDeptInfo(){
List
Main
package com.kjdh;
import com.kjdh.domain.Dept;
import com.kjdh.mapper.DeptMapper;
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 java.io.IOException;
import java.io.InputStream;
import java.util.List;
public class Main {
public static void main(String[] args) throws IOException {
// write your code here
String resource = "SqlMapConfig.xml";
InputStream inputStream=Resources.getResourceAsStream(resource);
SqlSessionFactoryBuilder sqlSessionFactoryBuilder=new SqlSessionFactoryBuilder();
SqlSessionFactory sqlSessionFactory=sqlSessionFactoryBuilder.build(inputStream);
SqlSession sqlSession = sqlSessionFactory.openSession();
//获得mapper接口对象
DeptMapper deptMapper = sqlSession.getMapper(DeptMapper.class);
// Dept dept = deptMapper.getDeptByID(10);
List deptList=deptMapper.getAllDepts();
System.out.println(deptList);
}
}