单纯的简单的mybatis有注解

同一个包下:com.com

1.IStudentDAO.java

简单的加入:

//小心Insert要大写,小写会出错的,有时后面的括号不要少写
     @Insert("insert into student(id,sname,birth,score) values(#{id},#{sname},#{birth},#{score})")
     public void insert(Student student); //插入  

 

2.IStudentDAOTest.java

少量代码:

String resource = "com/com/mybatis-config.xml";
        Reader reader = Resources.getResourceAsReader(resource);
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory factory = builder.build(reader);
       
        factory.getConfiguration().addMapper(IStudentDAO.class);//利用注解要加上这句是注册,否则不用加
        SqlSession session = factory.openSession();
        IStudentDAO iStudentDAO = session.getMapper(IStudentDAO.class);

3.Student.java

这里面的同无注解的一样

4.mybatis-config.properties

这个也同无注解的一样

5.mybatis-config.xml

这个里面的代码要把mapper删除掉,因为在DAO中注解过了,不需要调用mapper.xml

删除的代码是:<!--
 利用Annotation,固把此注释掉不用,而在IStudentDAO中注解了
 <mappers>
  <mapper resource="com/com/StudentMapper.xml" />
 </mappers>
-->

6.StudentMapper.xml

这是配置文件就可以不用了。

转载于:https://www.cnblogs.com/yyy88168/p/3201247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值