SpringWeb增删改查
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); } }
以上是关于SpringWeb增删改查的主要内容,如果未能解决你的问题,请参考以下文章
Express实战 - 应用案例- realworld-API - 路由设计 - mongoose - 数据验证 - 密码加密 - 登录接口 - 身份认证 - token - 增删改查API(代码片段