mybatis基础知识

上一篇文章介绍了mybatis的高级应用,但是似乎少了一篇基础知识的介绍,在此补上。

动手运行起来吧

1.下载依赖,jar,maven,gradle都可以。我用的是maven:

        <!--mybatis-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.3.0</version>
        </dependency>
        <!--mysql driver-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.6</version>
        </dependency>

2.核心配置文件

随便起个名字mybatis-config.xml

<?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>
    <properties resource="config/core/config.properties" />
    <environments default="development">
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${driver}"/>
                <property name="url" value="${url}"/>
                <property name="username" value="${username}"/>
                <property name="password" value="${password}"/>
            </dataSource>
        </environment>
    </environments>
    <mappers>
        <mapper resource="config/mapping/BlogMapper.xml"/>
        <mapper class="mybatis.demo.mapping.BlogMapper" />
        <mapper resource="config/mapping/AMapper.xml"/>
        <mapper resource="config/mapping/BMapper.xml"/>
        <mapper resource="config/mapping/ComplexClass.xml"/>
    </mappers>
</configuration>

我用到了properties,也可以直接在这个xml里配置好数据源的属性。

config.properties

driver=com.mysql.jdbc.Driver
url=jdbc:mysql://127.0.0.1:3306/test
username=root
password=qazwsxedc123

3.构建SqlSessionFactory

我写了一个util:

public class DB {
    private static SqlSessionFactory sqlSessionFactory = null;

    static {
        try{
            String resource = "config/core/mybatis-config.xml";
            InputStream inputStream = Resources.getResourceAsStream(resource);
            sqlSessionFactory = new SqlSessionFactoryBuilder().build(inputStream);
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    public static SqlSession getSession(){
        return sqlSessionFactory.openSession();
    }

}

4.重头戏,Mapped SQL Statements

mybatis最主要的内容都在这了。

<?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="Blog">
    <select id="selectBlog" resultType="mybatis.demo.domain.Blog" parameterType="Integer">
        select * from Blog where id = #{id}
    </select>
</mapper>

5.查询

5.1 用上面配的select来查询

SqlSession session = DB.getSession();
Blog blog = session.selectOne("Blog.selectBlog",1);

5.2 用Interface查询

要先定义一个接口:

package mybatis.demo.mapping;

import mybatis.demo.domain.Blog;
import org.apache.ibatis.annotations.Select;

/** * Created by dubby on 16/3/14. */
public interface BlogMapper {
    @Select("select * from blog where id=#{id}")
    Blog selectBlog(int id);
}

开始查询:

BlogMapper blogMapper = session.getMapper(BlogMapper.class);
Blog blog = blogMapper.selectBlog(1);

OK,查询完成了。

注意,这只是查询,如果是insert,update,delete的话,需要在执行完之后,commit一下,因为mybatis默认是不自动提交的,这和jdbc不一样。

你可能感兴趣的:(java,mybatis)