Mybatis(3 增删改查)

MyBatis 的真正强大在于它的语句映射,这是它的魔力所在。

创建接口

public interface UserInfoMapper {
    /**
     * 查找:
     * 查找所有用户
     * 根据id查找单个用户
     * @return
     */
    List<UserInfo> selectAllUsers();
    UserInfo selectUserById(Long userId);
    //新增一条数据
    Integer insetUserInfo(UserInfo userInfo);
    //根据id删除用户
    Integer deleteUserInfo(Long userId);
    //根据id修改用户名字
    Integer updateUserInfo( @Param("id") Long userId,@Param("name") String userName);
}

接口创建完毕后更改mapper中的命名空间

<mapper namespace="com.xxx.mapper.UserInfoMapper">

配置映射

id与接口名需对应

<?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="com.xxx.mapper.UserInfoMapper">
    <!--<resultMap id="userInfoResult" type="UserInfo">-->
        <!--<id property="userId" column="user_id" />-->
        <!--<result property="userName" column="username"/>-->
        <!--<result property="userPassword" column="user_password"/>-->
        <!--<result property="createTime" column="create_time"/>-->
        <!--<result property="updateTime" column="update_time"/>-->
    <!--</resultMap>-->
    <select id="selectAllUsers" resultType="UserInfo">
        select * from user_info
    </select>
    <select id="selectUserById" resultType="UserInfo">
        select * from user_info where user_id=#{id}
    </select>
    <insert id="insetUserInfo" parameterType="UserInfo">
        insert into user_info(username,user_password,create_time,update_time) values(#{userName},#{userPassword},now(),now())
    </insert>
    <delete id="deleteUserInfo">
        delete from user_info where user_id=#{id}
    </delete>
    <update id="updateUserInfo">
        update user_info set username=#{name},update_time=now() where user_id=#{id}
    </update>
</mapper>

注意传入多个参数情况,在接口处使用@Param(*)让其与#{*}对应

对sqlSession进行封装使得代码简洁

private static SqlSession getSqlSession() throws IOException {
        String resource = "mybatis-config.xml";
        InputStream inputStream = Resources.getResourceAsStream(resource);
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        return sqlSessionFactory.openSession();
    }

查找全部用户

public static void main( String[] args ) throws Exception{
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper=sqlSession.getMapper(UserInfoMapper.class);
        List<UserInfo> userInfos=mapper.selectAllUsers();
        for (UserInfo userInfo : userInfos) {
            System.out.println(userInfo);
        }
        sqlSession.close();
    }

根据id查找某个用户

public static void main( String[] args ) throws Exception{
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper=sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo=mapper.selectUserById(1L);
        System.out.println(userInfo);
        sqlSession.close();
    }

 public static void main( String[] args ) throws Exception{
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper=sqlSession.getMapper(UserInfoMapper.class);
        UserInfo userInfo=new UserInfo();
        userInfo.setUserName("wangwu");
        userInfo.setUserPassword("12345");
        mapper.insetUserInfo(userInfo);
        sqlSession.commit();
        sqlSession.close();
    }

  public static void main( String[] args ) throws Exception{
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper=sqlSession.getMapper(UserInfoMapper.class);
        mapper.deleteUserInfo(3L);
        sqlSession.commit();
        sqlSession.close();
    }

 public static void main( String[] args ) throws Exception{
        SqlSession sqlSession = getSqlSession();
        UserInfoMapper mapper=sqlSession.getMapper(UserInfoMapper.class);
        mapper.updateUserInfo(2L,"wangwu");
        sqlSession.commit();
        sqlSession.close();
    }

使用和指定语句的参数和返回值相匹配的接口(比如 BlogMapper.class),现在你的代码不仅更清晰,更加类型安全,还不用担心可能出错的字符串字面值以及强制类型转换。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值