SpringWeb增删改查
SpringWeb增删改查
凯哥学堂 发表于5个月前
SpringWeb增删改查
  • 发表于 5个月前
  • 阅读 2
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

模型类:

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();
    }
}
标签: Java
共有 人打赏支持
粉丝 6
博文 227
码字总数 221069
×
凯哥学堂
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: