idea中web项目的mybatis框架搭建

idea中web项目的mybatis框架搭建,第1张

idea中web项目的mybatis框架搭建

准备工作,创建学生表,创建基础数据

(1)创建项目,搭建包结构

 

(2)导入mybatis相关jar包和mysql驱动包

 注意,如果你的mysql环境比较新,最好用版本高的connector-J。

(3)在src根下创建mybatis主配置文件mybatis-config.xml,搭建配置文件结构。

(4)创建mapper包结构,创建sql映射文件StudentMapper.xml

(5)创建学生类,注意,Student类中一定要有空构造函数

mybatis-config.xml文件

作用是连接数据库,以及注册sql映射文件StudentMapper.xml,注意自己映射文件的路径,自行修改




    
        
            
            
                
                
                
                //连接数据库
            
        
    
    
        //注册sql的映射文件
    

 StudentMapper.xml

解释一下,等会我们在测试类中调用这里的sql映射用的是

session.selectOne("sql.getById",1001);

这里的sql就是mapper文件namespace标签的值,getById就是select等标签的id,起到一个定位的作用。

执行sql语句返回的对象 resultType="com.domain.Student"

执行sql语句需要传递的对象 parameterType="java.lang.Integer"

 之后用到的Student student=session.selectOne("sql.getById",1001);,返回对象是Student,传递的对象是Integer




    
 select * from student
 
    
        insert into student(id,name,time) values(#{id},#{name},#{time})
    
    
        update student set name=#{name},time=#{time} where id=#{id}
    
    
        delete from student where id=#{id};
    

测试方法

public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=sqlSessionFactory.openSession();//这里的就是模板,都一样,可以封装
        Student student=session.selectOne("sql.getById",1001);
        System.out.println(student);//记住Student要重写toString方法
    }

 输出成功

 多条记录用集合接收结果

public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=sqlSessionFactory.openSession();
        List list=session.selectList("sql.getAll");
        for (Student s:list){
            System.out.println(s);
        }
    }

输出结果

 注意,如果是更改数据库的 *** 作,需要有session.commit();方法

public static void main(String[] args) throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory =
                new SqlSessionFactoryBuilder().build(inputStream);
        SqlSession session=sqlSessionFactory.openSession();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间,也可使用当前时间戳
        Student student1=new Student(1005,"孙七",date);
        session.insert("sql.add",student1);
        session.commit();
    }

插入结果

 最后记得关闭session

 session.close();

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/3986996.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-10-21
下一篇 2022-10-21

发表评论

登录后才能评论

评论列表(0条)

保存