模型类:
package com;
public class Model {
private int id;
private String name;
private String dtype;
private String hospital;
public int getId () {
return id;
}
public void setId (int id) {
this .id = id;
}
public String getName () {
return name;
}
public void setName (String name) {
this .name = name;
}
public String getDtype () {
return dtype;
}
public void setDtype (String dtype) {
this .dtype = dtype;
}
public String getHospital () {
return hospital;
}
public void setHospital (String hospital) {
this .hospital = hospital;
}
}
连接类:
package util;
import java.io.IOException;
import java.io.Reader;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
public class MyBatisUtil {
private final static SqlSessionFactory sqlSessionFactory;
static {
String resource = "mybatis-config.xml" ;
Reader reader = null ;
try {
reader = Resources.getResourceAsReader(resource);
} catch (IOException e) {
System.out.println(e.getMessage());
}
sqlSessionFactory = new SqlSessionFactoryBuilder().build(reader);
}
public static SqlSessionFactory getSqlSessionFactory () {
return sqlSessionFactory;
}
}
接口类:
package mapper;
import java.util.List;
import com.Model;
public interface CrMapper {
public Model selectModel (int id) ;
public void addModel (Model model) ;
public Model deletModel (int id) ;
public void updateModel (Model model) ;
public List<Model> allModel () ;
}
接口映射xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper SYSTEM "http://mybatis.org/dtd/mybatis-3-mapper.dtd" PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN">
-<mapper namespace ="mapper.CrMapper" >
<insert parameterType ="com.Model" id ="addModel" > insert into a(id,name,dtype,hospital) values(#{id},#{name},#{dtype},#{hospital}) </insert >
<update parameterType ="com.Model" id ="updateModel" > update a set name=#{name},dtype=#{dtype}, hospital=#{hospital} where id=#{id} </update >
<delete parameterType ="int" id ="deletModel" > delete from a where id=#{id} </delete >
<select parameterType ="int" id ="selectModel" resultType ="com.Model" > select * from a where id=#{id} </select >
<select id ="allModel" resultType ="com.Model" > select * from a </select >
</mapper >
测试类:
package test;
import java.util.List;
import mapper.CrMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import util.MyBatisUtil;
import com.Model;
public class Test {
public static void main (String[] args) {
SqlSessionFactory factory=MyBatisUtil.getSqlSessionFactory();
SqlSession session=factory.openSession();
Model model=new Model();
model.setName("cc" );
model.setDtype("33" );
model.setHospital("33" );
CrMapper crMapper = session.getMapper(CrMapper.class);
crMapper.addModel(model);
session.commit();
session.close();
}
public void testCr () {
SqlSessionFactory factory=MyBatisUtil.getSqlSessionFactory();
SqlSession session=factory.openSession();
Model model=new Model();
model.setName("cc" );
model.setDtype("33" );
model.setHospital("33" );
CrMapper crMapper = session.getMapper(CrMapper.class);
crMapper.addModel(model);
session.commit();
session.close();
}
public void testAll () {
SqlSession session =MyBatisUtil.getSqlSessionFactory().openSession();
CrMapper crMapper=session.getMapper(CrMapper.class);
List<Model> list=crMapper.allModel();
for (Model model:list){
System.out.println("id:" +model.getId()+
"\t姓名:\t" +model.getName()+
"\t类型:\t" +model.getDtype()+
"\t医生:\t" +model.getHospital());
}
session.close();
}
public void testuptate () {
SqlSession session=MyBatisUtil.getSqlSessionFactory().openSession();
CrMapper crMapper=session.getMapper(CrMapper.class);
Model model=new Model();
model=crMapper.selectModel(3 );
model.setName("王菲" );
model.setDtype("外科" );
model.setHospital("1" );
crMapper.updateModel(model);
session.commit();
session.close();
}
public void testdelete () {
SqlSession session=MyBatisUtil.getSqlSessionFactory().openSession();
CrMapper crMapper=session.getMapper(CrMapper.class);
crMapper.deletModel(2 );
session.commit();
session.close();
}
}