注解实现Mybaits操作数据库

1,概述:

我们之前讲过了通过XML的方式来注入Sql实现Mybatis操作数据库。但是其实,我们还可以通过注解的方式来实现。不过,在真实的开发中,其实很多人都还是喜欢用xml的方式。

2,注解实现映射器:

1,创建一个Mapper,名字叫做UserAnnMapper

package com.atbleuuu.mybaits.mapper;

import com.atbleuuu.mybaits.po.User;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 基于Annotation的User的Mapper
 */
public interface UserAnnMapper {
    @Select("select * from user where id=#{id}")
    User getUserById(int id);
    @Insert("insert  into user value (null,#{name},#{gender},#{age})")
    int addUser(User user);

    @Update(" update user set name =#{name}, gender =#{gender}, age=#{age} where id=#{id}")
    int updateUser(User user);

    @Delete(" delete from user where id=#{id}")
    int deleteUser(int id);
}



 2,把Mapper注入到mybatis-config.xml配置文件中

<!--注入映射器-->
    <mappers>
        <mapper class="com.atbleuuu.mybaits.mapper.UserAnnMapper"/>
    </mappers>

 3,创建一个AnnTest类 实现注解查询

package com.atbleuuu.mybaits.test;

import com.atbleuuu.mybaits.mapper.UserAnnMapper;
import com.atbleuuu.mybaits.po.User;
import com.atbleuuu.mybaits.tool.MybaitsTool;
import org.apache.ibatis.session.SqlSession;


import java.io.IOException;
import java.io.Reader;

public class AnnTest {

        public static void main(String[] args) {

            //加载对应配置文件
            SqlSession sqlSession = null;
            try {
                sqlSession = MybaitsTool.getSqlSession();
                UserAnnMapper userAnnMapper = sqlSession.getMapper(UserAnnMapper.class);
                User user = userAnnMapper.getUserById(1);
                //打印数据
                System.out.println(user);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
            //查询对应的数据
        }

        }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MyBatis框架是一个优秀的ORM(对象关系映射)框架,它提供了多种方法向数据库插入数据。以下是使用MyBatis向数据库插入数据的步骤: 1.在MyBatis中通过配置文件或注解方式定义数据源和SQL语句。配置文件是在mybatis-config.xml中定义,它设置了MyBatis框架中的全局属性。其他的数据源和数据库配置也可与此文件中连同。 2.配置Mapper文件,如mapper1.xml:mapper1.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="com.xxx.mapper.UserMapper"> <insert id="insertUser" parameterType="com.xxx.entity.User"> insert into user(username,password,job,create_time,update_time) values(#{username},#{password},#{job},#{createTime},#{updateTime}) </insert> </mapper> 其中`namespace`是Mapper接口的全路径,`insert`是定义的插入语句,`parameterType`指定了实体类对应的参数类型。需要注意的是,在parameterType中指定的实体类属性名应该与数据库表中的字段名一致。 3.使用MyBatis框架提供的SqlSession类来获取一个与数据库连接的会话。 4.然后通过SqlSession中定义的插入方法insert执行插入操作。指定插入语句和插入参数,如下: ```java SqlSession sqlSession = MyBatisUtil.getSqlSession(); UserMapper userMapper = sqlSession.getMapper(UserMapper.class); User user = new User(); user.setUserName("test"); user.setPassword("123456"); user.setJob("developer"); user.setCreateTime(new Date()); user.setUpdateTime(new Date()); // 执行插入操作 userMapper.insertUser(user); // 一定要提交,不然数据不会写入数据库 sqlSession.commit(); ``` 通过以上几个步骤就可以使用MyBatis框架向数据库插入数据了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值