SpringWeb增删改查

模型类:

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; //单例SqlSessionFactory对象
	
	static{
		String resource = "mybatis-config.xml"; //MyBatis配置文件,位于src路径
		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(){ //返回单例SqlSessionFactory对象
		return sqlSessionFactory;
	}

}

接口类:

package mapper;

import java.util.List;

import com.Model;

public interface CrMapper {
	// 获取ID对象
	public Model selectModel(int id);
   //插入一条记录
	public void addModel(Model model);
	//删除一条记录
	public Model deletModel(int id);
	//更新Model对象
	public void updateModel(Model 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">

<!-- 映射器名字空间,即BlogMapper对象的完整路径名 -->

-<mapper namespace="mapper.CrMapper">

<!-- 新增记录,映射SQL插入语句。id为当前命名空间中唯一的标识符,可 以被用于引用该语句。当映射器接口中的某一方法与id值一致时,就会执行 该语句。parameterType属性定义方法参数类型 -->


<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>

<!-- 查询单条记录,映射SQL查询语句 -->


<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){
    		//显示model对象信息
    		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);
    	//根据id查询后修改字段
    	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);
	     //根据id删除
	     crMapper.deletModel(2);
	     //提交
	     session.commit();
	     //关闭
	     session.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值