~$ 存档

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  514 随笔 :: 26 文章 :: 15 评论 :: 62万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

 

这篇是承接上一篇的helloWorld程序。首先将一系列初始化的步骤再做一个工厂类进行包装,代码如下:

 

复制代码
package com.tinaluo.sun;

import java.io.InputStream;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.*;

public class FKSqlSessionFactory {
    private static SqlSessionFactory sqlSessionFactory=null;
    static{
        try
        {
            InputStream inputStream=Resources.getResourceAsStream("mybatis-config.xml");
            sqlSessionFactory=new SqlSessionFactoryBuilder().build(inputStream);    
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
    public static SqlSession getSqlSession()
    {
        return sqlSessionFactory.openSession();
    }
    public static SqlSessionFactory getSqlSessionFactory()
    {
        return sqlSessionFactory;
    }
}
复制代码

 

UserMapper.xml文件修改后:

复制代码
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="org.fkit.mapper.UserMapper">
    <!-- 增加 -->
    <insert id="saveUser" parameterType="com.tinaluo.sun.User"
        useGeneratedKeys="true">
        insert into tb_user(name,sex,age)
        values(#{name},#{sex},#{age})
    </insert>
    <!-- 查找 -->
    <select id="selectUser" parameterType="int" resultType="com.tinaluo.sun.User">
        select *
        from tb_user where id=#{id}
    </select>
    <!-- 更新 -->
    <update id="modifyUser" parameterType="com.tinaluo.sun.User">
        update tb_user set
        name=#{name},sex=#{sex},age=#{age} where id=#{id}
    </update>
    <!-- 删除 -->
    <delete id="removeUser" parameterType="int">
        delete from tb_user where
        id=#{id}
    </delete>
</mapper>
复制代码

 

目录结构如下:

 

————————————————
版权声明:本文为博客园博主「天涯」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
posted on   LuoTian  阅读(185)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示