SSM框架之Mybatis学习(IDEA)02

简单的增删改查操作

一、增:

打开映射文件UserMapper.xml。

注意:在这里,占位符由变为#{表列名}

    <insert id="insertOne" parameterType="com.myb.domain.User">
        insert into user values(#{id},#{username},#{password})
    </insert>

测试:

    @Test
    public void test2() throws IOException {
        //模拟user对象
        User user=new User();
        //user.setId();
        user.setUsername("zaholiu");
        user.setPassword("123");
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得Session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
        sqlSession.insert("userMapper.insertOne",user);
        //mybatis不自动提交,若要操作表,要提交事务。
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

二、删:

打开映射文件UserMapper.xml。

    <delete id="deleteOne" parameterType="java.lang.Integer">
        delete from user where id=#{id}
    </delete>

测试:

    @Test
    public void test3() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得Session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
        sqlSession.delete("userMapper.deleteOne",1);
        //mybatis不自动提交,若要操作表,要提交事务。
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }

三、改:

打开映射文件UserMapper.xml。

    <update id="updateOne" parameterType="com.myb.domain.User">
        update user set username=#{username},password=#{password} where id=#{id}
    </update>

测试:

    @Test
    public void test4() throws IOException {
        //模拟user对象
        User user=new User();
        user.setId(1);
        user.setUsername("zhangsan");
        user.setPassword("abc");
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得Session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
        sqlSession.update("userMapper.updateOne",user);
        //mybatis不自动提交,若要操作表,要提交事务。
        sqlSession.commit();
        //释放资源
        sqlSession.close();
    }
}

四、查:

查询所有见SSM框架之Mybatis学习(IDEA)01

打开映射文件UserMapper.xml。

    <select id="selectById" resultType="user" parameterType="int">
        select* from user where id=#{id}
    </select>

测试:

    public void test5() throws IOException {
        //获得核心配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        //获得session工厂对象
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(resourceAsStream);
        //获得Session会话对象
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //执行操作 参数:namespace+id
        User user = sqlSession.selectOne("userMapper.selectById",9);
        //打印数据
        System.out.println(user);
        //释放资源
        sqlSession.close();

    }

五、mapper配置:

加载映射的作用,有以下几种方式:

  1. 使用相对于类路径的资源引用,例如:

    <mapper resource="com/myb/mapper/UserMapper.xml"/>
    
  2. 使用完全限定资源定位符(URL),例如:

    <mapper url="file:///var/mappers/UserMapper.xml"/>
    
  3. 使用映射器接口实现类的完全限定类名,例如:

    <mapper class="org.mybatis.builder.UserMapper"/>
    
  4. 将包内的映射器接口实现全部注册为映射器,例如:

    <package name="org.mybatis.builder"/>
    

六、jdbc.properties文件

1.在resources包下创建文件jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test
jdbc.username=root
jdbc.password=123

2.在sqlMapConfig.xml中写

<!--通过properties标签加载外部properties文件-->
    <properties resource="jdbc.properties"></properties>

    <!--    配当前数据源的环境-->
    <environments default="development">
        <environment id="development"><!--自己编-->
            <transactionManager type="JDBC"/><!--事务管理器-->
            <dataSource type="POOLED">
                <!-- UNPOOLED:不使用数据库连接池,采用JDBC方式
                     JNDI:使用容器中的数据库连接池 -->
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

七、问题

  在这里遇到了一个问题,很严重,然后发现是自己在编程过程中将一些东西的启动关了,诶。

  jdbc.properties文件变成了这样,在其他项目中是亮的,还以为是设置的问题,搞了一大堆。但还是不可以,最后点开Navicat for Mysql时发现了问题。
在这里插入图片描述
在联网的情况下出现了这个。
在这里插入图片描述
最后重新启动了mysql解决了连接问题。也解决了以下问题。

org.apache.ibatis.exceptions.PersistenceException: 
### Error querying database.  Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
### The error may exist in com/myb/mapper/UserMapper.xml
### The error may involve userMapper.findAll
### The error occurred while executing a query
### Cause: com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure

The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.

八、总结

编程过程中真的一点点小操作都会有很大的影响,还是要认真要注意每一次小操作。并且真的有几种bug重启电脑可解,因为我将启动关了,不是关闭了自启动。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值