博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SSM整合初级 简单的增删改查
阅读量:7094 次
发布时间:2019-06-28

本文共 4676 字,大约阅读时间需要 15 分钟。

1.jar包

  

2.mybatis-config.xm配置文件

3.dao

public interface IStudentDAO {       public int addStu(Student stu) throws IOException;      //删除   public int delStu(int id) throws IOException;   //查询所有记录   public List
findAll() throws IOException; //按照学生姓名查询学生集合 public List
findStudntByName(Student stu) throws IOException; public List
findStudntByName(String stuname) throws IOException; }

daoImpl

public class StudentDAOImpl implements IStudentDAO {    SqlSession session ;    public StudentDAOImpl() throws IOException {        session= MybatisUtil.getSession();    }    public List
findStudntByName(String stuname) throws IOException { List
list = session.selectList("findStudentByName",stuname); session.close(); return list; } /** * * 模糊查询 */ public java.util.List
findStudntByName(Student stu) throws IOException { List
list = session.selectList("findStudentByName",stu); session.close(); return list; } /** * 查询所有 */ public java.util.List
findAll() throws IOException { List
list = session.selectList("findAll"); session.close(); return list; } /** * 删除 */ public int delStu(int id) throws IOException { int result = session.delete("delStudent",id); session.commit(); session.close(); return result; } public int addStu(Student stu) throws IOException { int result = session.insert("insertStudent",stu); session.commit(); session.close(); return result; }}

dao.xml

insert into student(stuname,stuage,studate) values(#{stuname},#{stuage},#{studate})
select @@identity
delete from student where stuno=#{xxx}

MyBatisUitl工具类

  

/** * 工具类 * @author Happy * */public class MybatisUtil {    private static String config="mybatis-config.xml";    static Reader reader;    static{        try {            reader= Resources.getResourceAsReader(config);        } catch (IOException e) {            e.printStackTrace();        }    }    private static SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(reader);    //提供一个可以获取到session的方法    public static SqlSession getSession() throws IOException{                System.out.println("22222"+factory);        //弊病,就是工厂是           // 1.1 openSession到底做了什么           SqlSession session = factory.openSession();           System.out.println("3333");            return session;    }}

单测

  

public class MyTest {    IStudentDAO dao;    @Before    public void initData() throws IOException{        dao=new StudentDAOImpl();    }        /**     * 模糊查询     * @throws IOException     */        @Test    public void findStudentByName() throws IOException{                /*Student stu=new Student();        stu.setStuname("三");*/        List
list = dao.findStudntByName("三"); for (Student student : list) { System.out.println(student.getStuname()); } } /** * selectALl学生 * @throws IOException */ @Test public void findAll() throws IOException{ List
list = dao.findAll(); for (Student student : list) { System.out.println(student.getStuname()); } } /** * 删除学生 * @throws IOException */ @Test public void delStudent() throws IOException{ dao.delStu(2); System.out.println("ok"); } @Test public void testAdd() throws IOException{ Student stu=new Student(); stu.setStuname("张三"); stu.setStuage(21); stu.setStudate(new Date()); System.out.println("添加前"+stu); IStudentDAO dao=new StudentDAOImpl(); dao.addStu(stu); System.out.println("添加后"+stu); }}

 

转载于:https://www.cnblogs.com/s1294/p/6189113.html

你可能感兴趣的文章
MyEclipse安装EGit插件方法
查看>>
mobileControls与移动控件适配
查看>>
.Net消息队列的使用
查看>>
HTML5 WebAudioAPI-实例(二)
查看>>
ThinkPHP升级指导
查看>>
亲测 logminer挖掘
查看>>
常用 Git 命令清单
查看>>
往linux上传、下载
查看>>
Mybatis在xml文件中处理大于号小于号的方法
查看>>
cross apply 和 outer apply
查看>>
tomcat 下部署 php
查看>>
MyBatis学习(一)、MyBatis简介与配置MyBatis+Spring+MySql
查看>>
php 连接redis,并登录验证
查看>>
pm2使用
查看>>
端游及手游服务端的常用架构
查看>>
设计模式原则 依赖倒置
查看>>
redis设置密码
查看>>
H264编码技术[3]
查看>>
解决 docker: Error response from daemon: ... : net/http: TLS handshake timeout.
查看>>
[置顶] Android中使用Movie显示gif动态图
查看>>