5分钟学会-最简单的MyBatis使用方法

时间:2021-03-19 05:14:39
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。 iBATIS一词来源于“internet”和“abatis”的组合,是一个基于Java的持久层框架。iBATIS提供的持久层框架包括SQL Maps和Data Access Objects(DAO) 使用之前先下好jar包 --mybatis-3.3.0.jar 导入到eclipse的项目中
新建一个xml文件用于配置MyBatis:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<environments default="development">
<environment id="development">
<transactionManager type="JDBC" />
<!-- 配置数据库连接信息 -->
<dataSource type="POOLED">
<property name="driver" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://127.0.0.1/double?useServerPrepStmts=false&rewriteBatchedStatements=true" />
<property name="username" value="root" />
<property name="password" value="123" />
</dataSource>
</environment>
</environments>
<mappers>
        <!-- 注册NumberMapper.xml文件, NumberMapper.xml位于mapper这个包下,所以resource写成mapper/NumberMapper.xml-->
        <mapper resource="mapper/NumberMapper.xml"/>
    </mappers>

</configuration>

建立对于的mapping文档
<?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,namespace的值习惯上设置成包名+sql映射文件名,这样就能够保证namespace的值是唯一的
例如namespace="me.gacl.mapping.userMapper"就是me.gacl.mapping(包名)+userMapper(userMapper.xml文件去除后缀) -->
<mapper namespace="inter.INumberOperation">



<!-- 在select标签中编写查询的SQL语句, 设置select标签的id属性为getUser,id属性值必须是唯一的,不能够重复 使用parameterType属性指明查询时使用的参数类型,resultType属性指明查询返回的结果集类型
resultType="me.gacl.domain.User"就表示将查询结果封装成一个User类的对象返回 User类就是users表所对应的实体类 -->
<!-- sql语句可以写到xml里也可以用注解的形式写到相应的接口里 -->
<insert id="addToReal" parameterType="obj.Number"> insert into
coculatenumber_real(id, r1,r2,r3,r4,r5,r6,b1) values(#{id}, #{r1},
#{r2},
#{r3}, #{r4}, #{r5}, #{r6}, #{b1})
</insert>
</mapper>


然后再建立一个接口:
sql语句全写接口里
package inter;

import java.util.ArrayList;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Select;

import obj.Number;

public interface INumberOperation {


void addToReal(Number num);

@Select("select * from coculatenumber_real")
ArrayList<Number> getAllFormHis();

@Select("select * from coculatenumber_2000w_index where id = #{id}")
Number getNumber(int id);

@Select("select * from coculatenumber_2000w_index where r1 = #{r1} and r2=#{r2} and r3=#{r3} and r4=#{r4} and r5=#{r5} and r6=#{r6} and b1=#{b1}")
ArrayList<Number> countTimes(Number number);

@Insert("insert into coculatenumber_count(id, r1,r2,r3,r4,r5,r6,b1,info) values(#{id}, #{r1}, #{r2}, #{r3}, #{r4}, #{r5}, #{r6}, #{b1},#{info})")
void addcount(Number number);

@Insert("insert into coculatenumber_all(id, r1,r2,r3,r4,r5,r6,b1) values(#{id}, #{r1}, #{r2}, #{r3}, #{r4}, #{r5}, #{r6}, #{b1})")
void addToAll(Number number);

@Select("select * from coculatenumber_all where id >= #{min} and id <= #{max}")
getDataByID(@Param("min")int min, @Param("max")int max);//普通参数需要加@Param("xxx")来标示,否则会找不到参数


}

最后就直接调用接口的方法,最后记得提交就行了:
      
ArrayList<Number> his=new ArrayList<Number>();
String resource = "mybatis_config.xml";
Reader reader = Resources.getResourceAsReader(resource);
SqlSessionFactory sessionFactory = new SqlSessionFactoryBuilder().build(reader);
SqlSession session = sessionFactory.openSession();

INumberOperation numberOperation = session.getMapper(INumberOperation.class);
his=numberOperation.getAllFormHis();
session.commit();//更新语句需要提交